通过 API 使用 Nova 的文档理解功能
下面是一个简化版的 Python 示例,说明了如何使用 Amazon Nova 进行文档 QA(问答)或分析。我们将使用 AWS Bedrock API(通过 Boto3 SDK)发送一份 PDF 文档和一个供模型回答的问题。
import base64 import base64 import json import boto3 # Initialize Bedrock runtime client (adjust region as needed) client = boto3.client("bedrock-runtime", region_name="us-east-1") MODEL_ID = "us.amazon.nova-lite-v1:5" # using Nova Lite model in this example # Read the document file (PDF) in binary mode with open("my_document.pdf", "rb") as file: doc_bytes = file.read() # Construct the conversation messages with document + question messages = [ { "role": "user", "content": [ { "document": { "format": "pdf", "name": "Document1", # neutral name for the document "source": { "bytes": doc_bytes # embedding the PDF content directly } } }, { "text": "Here is a question about the document: ... (your question) ... ?" } ] } ] # Set inference parameters (optional) inf_params = {"maxTokens": 4000, "topP": 0.1, "temperature": 0.3} # Invoke the model response = client.converse(modelId=MODEL_ID, messages=messages, inferenceConfig=inf_params) # Extract and print the answer answer_text = response["output"]["message"]["content"][0]["text"] print(answer_text)
如果输入文件很大(超过 25 MB 的直接上传限制)或文件很多,可将其存储在 Amazon S3 中并进行引用。这就避免了在请求中发送原始字节。使用 S3 时,需确保 Bedrock 服务有权访问存储桶/对象。例如,为了在 S3 中引用 PDF,文档源将使用“S3 位置”而不是“字节”,如下所示:
messages = [ { "role": "user", "content": [ { "document": { "format": "pdf", "name": "Report2023", "source": { "s3Location": { "uri": "s3://your-bucket/path/to/document1.pdf", "bucketOwner": "123456789012" } } } }, { "text": "Summarize the key findings from the Q3 2023 report." } ] } ]
注意
文档名称只能包含字母数字字符、连字符、圆括号和方括号。
name 字段容易受到提示注入的影响,因为模型可能会意外将其解释为指令。因此,我们建议您指定一个中性名称。