This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.
您可以詢問 Claude 關於您提供的 PDF 中的任何文字、圖片、圖表和表格。一些範例使用案例:
Claude 適用於任何標準 PDF。確保您的請求大小符合這些要求:
| 要求 | 限制 |
|---|---|
| 最大請求大小 | 32 MB (因平台而異) |
| 每個請求的最大頁數 | 600(對於具有 200k 代幣上下文視窗的模型為 100) |
| 格式 | 標準 PDF(無密碼/加密) |
兩個限制都適用於整個請求負載,包括與 PDF 一起發送的任何其他內容。對於大型 PDF,請考慮使用 Files API 上傳並按 file_id 引用以保持請求負載較小。
密集 PDF(許多小字體頁面、複雜表格或繁重圖形)可能會在達到頁面限制之前填滿上下文視窗。包含大型 PDF 的請求也可能在達到頁面限制之前失敗,即使使用 Files API 也是如此。嘗試將文件分割成多個部分;對於大型檔案,由於每個頁面都作為影像處理,縮減嵌入影像也可以幫助。
由於 PDF 支援依賴於 Claude 的視覺功能,它受到與其他視覺任務相同的 限制和注意事項。
PDF 支援目前通過直接 API 存取和 Google Vertex AI 支援。所有 活躍模型 都支援 PDF 處理。
PDF 支援現已在 Amazon Bedrock 上提供,具有以下注意事項:
通過 Amazon Bedrock 的 Converse API 使用 PDF 支援時,有兩種不同的文件處理模式:
重要: 要在 Converse API 中存取 Claude 的完整視覺 PDF 理解功能,您必須啟用引用。如果未啟用引用,API 將回退到僅基本文字提取。深入瞭解 使用引用。
Converse Document Chat(原始模式 - 僅文字提取)
Claude PDF Chat(新模式 - 完整視覺理解)
如果客戶報告在使用 Converse API 時 Claude 無法看到其 PDF 中的影像或圖表,他們可能需要啟用引用標誌。沒有它,Converse 將回退到僅基本文字提取。
這是 Converse API 的已知限制。對於需要無引用視覺 PDF 分析的應用程式,請考慮改用 InvokeModel API。
對於非 PDF 檔案(如 .csv、.xlsx、.docx、.md 或 .txt 檔案),請參閱 使用其他檔案格式。
讓我們從使用 Messages API 的簡單範例開始。您可以通過三種方式向 Claude 提供 PDF:
document 內容區塊中的 base64 編碼 PDFfile_id最簡單的方法是直接從 URL 引用 PDF:
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "url",
"url": "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf",
},
},
{"type": "text", "text": "What are the key findings in this document?"},
],
}
],
)
print(message.content)如果您需要從本地系統發送 PDF 或 URL 不可用時:
import base64
import httpx
# First, load and encode the PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(httpx.get(pdf_url).content).decode("utf-8")
# Alternative: Load from a local file
# with open("document.pdf", "rb") as f:
# pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")
# Send to Claude using base64 encoding
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{"type": "text", "text": "What are the key findings in this document?"},
],
}
],
)
print(message.content)對於您將重複使用的 PDF,或當您想避免編碼開銷時,請使用 Files API:
client = anthropic.Anthropic()
# Upload the PDF file
with open("document.pdf", "rb") as f:
file_upload = client.beta.files.upload(file=("document.pdf", f, "application/pdf"))
# Use the uploaded file in a message
message = client.beta.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
betas=["files-api-2025-04-14"],
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {"type": "file", "file_id": file_upload.id},
},
{"type": "text", "text": "What are the key findings in this document?"},
],
}
],
)
print(message.content)當您向 Claude 發送 PDF 時,會發生以下步驟:
系統提取文件的內容。
Claude 分析文字和影像以更好地理解文件。
Claude 回應,如果相關則引用 PDF 的內容。
Claude 可以在回應時引用文字和視覺內容。您可以通過整合 PDF 支援與以下內容來進一步改進效能:
PDF 檔案的代幣計數取決於從文件中提取的總文字以及頁數:
您可以使用 代幣計數 來估計您特定 PDF 的成本。
遵循這些最佳實踐以獲得最佳結果:
對於大量處理,請考慮以下方法:
快取 PDF 以改善重複查詢的效能:
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
"cache_control": {"type": "ephemeral"},
},
{"type": "text", "text": "Analyze this document."},
],
}
],
)使用訊息批次 API 進行大量工作流程:
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
requests=[
{
"custom_id": "doc1",
"params": {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{"type": "text", "text": "Summarize this document."},
],
}
],
},
}
]
)Was this page helpful?