

# API による Nova のドキュメント理解の使用
<a name="modalities-document-examples"></a>

ドキュメント QA (質問応答) または分析に Amazon Nova を使用する方法を説明するために、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 を参照する場合、ドキュメントソースは「bytes」の代わりに「s3Location」を使用します。

```
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` フィールドはプロンプトインジェクションに対して脆弱です。したがって、中立的な名前を指定することが推奨されます。