透過 API 使用 Nova 的文件理解 - Amazon Nova

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

透過 API 使用 Nova 的文件理解

若要說明如何使用 Amazon Nova 進行文件 QA (問題回應) 或分析,以下是 Python 中的簡化範例。我們會使用 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,您的文件來源會使用 "s3Location" 而非 "bytes",如下所示:

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 欄位容易受到提示詞注入的影響,因為模型可能會不小心將其解譯為指示。因此,我們建議您指定中性名稱。