Skip to main content

PDF理解

PDF理解功能使模型能夠解析PDF文件中的文字與圖片,透過 Chat Completions、Responses API、DashScope 或 Anthropic 相容介面進行分析。

支援的模型

qwen4-max、qwen4-35b-a3b、qwen3.8-max、qwen3.8-max-0902、qwen3.8-flash、qwen3.8-27b、kimi-k3.1-pro qwen4-35b-a3b 支援透過 Chat Completions、Responses API 和 Anthropic 相容的 Messages API 進行 PDF 理解。 qwen3.8-27b 僅支援在北京地區透過 Chat Completions 或 DashScope 呼叫 PDF 理解。

快速開始

運行以下代碼,向模型傳入PDF檔案。
需要已擷取與配置 API Key並配置API Key到環境變數。
  • OpenAI 相容
  • DashScope
  • Responses API
  • Anthropic
  • Python
  • Node.js
  • HTTP

範例程式碼

from openai import OpenAI
import os

client = OpenAI(
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"(不建議),
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下為新加坡地區的URL,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的URL不同。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen4-max",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "file",
                    "file": {
                        "file_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260616/qmycjl/1506.02640v5.pdf"
                    }
                },
                {
                    "type": "text",
                    "text": "總結一下這個PDF文檔的內容"
                }
            ]
        }
    ],
    stream=True,
    stream_options={"include_usage": True},
    # 需要開啟該參數才會在 usage 中返回 x_tools.pdf_page_parser.count(PDF解析頁數)
    extra_body={"include_tool_usage": True}
)

for chunk in completion:
    if not chunk.choices:
        print(f"\nUsage: {chunk.usage}")
        continue
    delta = chunk.choices[0].delta
    if hasattr(delta, "content") and delta.content:
        print(delta.content, end="", flush=True)

使用Base64輸入

如果無法提供檔案的URL地址,也可以將PDF檔案以Base64編碼字串的形式傳入。使用 file_data 時,filename 欄位為必填項。
Base 64 編碼會使資料體積增大約 1/3,例如 150MB 的檔案編碼後約 200MB,會超出請求體大小上限。大檔案請改用 URL 方式傳入。
  • Chat Completions / DashScope
  • Anthropic
import base64
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下為新加坡地區的URL,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的URL不同。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

# 讀取並編碼PDF檔案
with open("report.pdf", "rb") as f:
    pdf_base64 = base64.b64encode(f.read()).decode("utf-8")

completion = client.chat.completions.create(
    model="qwen4-max",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "file",
                    "file": {
                        "file_data": f"data:application/pdf;base64,{pdf_base64}",
                        "filename": "report.pdf"
                    }
                },
                {
                    "type": "text",
                    "text": "這份報告的核心結論是什麼?"
                }
            ]
        }
    ]
)

print(completion.choices[0].message.content)

請求參數

以下參數表適用於 Chat Completions。Responses API 使用 type: "input_file" 和 file_url;Anthropic 相容介面使用 type: "document",並在 source 中設定 type: "base64"、media_type: "application/pdf" 和 data(不含 Data URI 前綴的 Base64 字串)。 DashScope 使用 content 陣列中包含 file_url 或 file_data 的元素傳入文件。 URL 輸入時,type 必填,固定為 document;source 為必填物件,其中 type 為必填字串,設為 url,url 為必填字串,指定 PDF 檔案 URL。
協議中 URL 部分僅支援字串輸入,不支援 list(數組)形式。
OpenAI相容協議格式:

參數

類型

是否必填

說明

file_urlstring二選一必填指定PDF檔案的下載地址,和 file_data 二選一必填。
file_datastringBase64格式的PDF檔案輸入,格式為 data:application/pdf;base64,xxx,和 file_url 二選一必填。
filenamestring條件必填檔案名稱,使用 file_data 作為入參時必填。
file_formatstring否檔案格式,選擇性參數,當前僅支援 pdf,預設 pdf。

限制說明

專案

限制

單檔案大小限制150MB
單文檔頁數限制256頁
PDF解析可能比普通文本請求耗時更長,首包逾時時間最長為300秒,建議使用流式輸出方式即時擷取結果,避免長時間等待。

計費說明

計費涉及以下方面:
  • 模型調用費用:PDF檔案解析出的文字與圖片會計入模型的輸入Token,按照模型的標準輸入價格計費。
  • 文檔解析費用:按PDF文檔解析的頁數計費,各地域單價不同:中國(北京)$0.003/頁,新加坡$0.0033/頁。全球部署模式:$0.003/頁。
各模型的輸入輸出單價請參見模型調用計費。

查看PDF解析頁數

qwen4-max 的 Responses API 使用回應頂層 tool_usage,各工具的 num_requests 為計費次數,pdf_page_parser.num_requests 為解析頁數。Qwen3.8 的 Responses 範例仍使用 usage.x_tools。 Chat Completions 與 Responses API (Qwen3.8) 可在請求主體頂層傳入 include_tool_usage: true,從 usage.x_tools.pdf_page_parser.count 查看解析頁數;DashScope 從 usage.plugins.pdf_page_parser.count 查看。Anthropic 相容介面在回應頂層 x_tools.pdf_page_parser.count 返回解析頁數。未返回計數不代表沒有產生解析費用。
// 需在請求體頂層傳入 "include_tool_usage": true 後才會返回。
// OpenAI Python SDK 請透過 extra_body={"include_tool_usage": True} 傳入,
// 直接使用 HTTP/curl 時放在請求體頂層,不要嵌套到 extra_body 或 stream_options。
{
    "usage": {
        "x_tools": {
            "pdf_page_parser": {
                "count": 10,
                "strategy": "normal"
            }
        }
    }
}