

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

# 理解文件
<a name="modalities-document"></a>

**注意**  
本文件適用於 Amazon Nova 第 1 版。如需 Amazon Nova 2 文件理解指南，請造訪[文件理解](https://docs.aws.amazon.com/nova/latest/nova2-userguide/using-multimodal-models.html#document-understanding)。

Amazon Nova 的文件理解功能可讓您在提示中包含整份文件 (PDFs、Word 檔案、試算表等），並針對其內容提出問題或請求。Nova 的多模式理解模型 (Lite、Pro、Premier) 可以解譯這些文件中的文字和視覺元素 （例如圖表或資料表）。這可啟用例如問題回答、摘要和分析冗長報告或掃描文件等使用案例。主要功能包括適用於長文件的非常大型內容視窗 (1-2M 字符），以及能夠在單一查詢中處理多個文件。

Amazon Nova 區分兩種類型的文件輸入：
+ **文字型文件類型** （例如 TXT、CSV、Markdown、HTML、DOC)：這些主要針對其文字內容進行處理。Nova 將專注於了解這些文件中的文字並從中擷取資訊。
+ 以**媒體為基礎的文件類型** （例如 PDF、DOCX)：這些檔案可能包含複雜的配置、影像、圖表或內嵌圖形。對於以媒體為基礎的文件，Nova 會同時處理視覺和文字元素。Nova 採用以視覺為基礎的理解來解譯視覺化內容，例如圖表、資料表、圖表或螢幕擷取畫面，以及文件的文字。

  Amazon Nova 中的 PDF 檔案不支援 JPEG2000 和 JBIG2。

支援的檔案格式包括常見的文件類型：純文字和結構化文字檔案 (CSV、TXT)、試算表 (XLS/XLSX)、HTML/Markdown、Word 文件 (DOC/DOCX) 和 PDF 檔案。對於 文件內的影像，會處理標準影像格式 (PNG、JPG、GIF、WebP)，但不支援包含特定影像編碼 (CYMK、SVG) 的 PDFs。


**文件大小限制和使用準則**  

| 限制條件 | 限制 | 
| --- | --- | 
|  文件數量上限  |  每個請求最多 5 個文件 （適用於直接上傳和 Amazon S3)  | 
|  文字型文件大小  |  每個文字文件 （例如 .txt、.csv、.md、.html、.doc) 必須 ≤ 4.5 MB  | 
|  以媒體為基礎的文件大小  |  對於 .pdf 和 .docx 檔案，沒有個別檔案大小限制，但： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/nova/latest/userguide/modalities-document.html)  | 
|  不支援的 PDF 內容  |  不支援包含 CMYK 顏色描述檔或 SVG 影像PDFs   | 

# 透過 API 使用 Nova 的文件理解
<a name="modalities-document-examples"></a>

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