跳转到主要内容

Gemini 文档理解

POST /v1beta/models/{model}:generateContent
截至 2026 年 3 月 23 日,Crazyrouter 生产环境已验证 gemini-3-pro-preview 支持:
  • 通过 inlineData 读取 PDF
  • 结合 responseMimeType + responseSchema 输出可解析 JSON

PDF 摘要

下面的请求在生产环境成功读取 PDF,并返回摘要文本:
curl "https://crazyrouter.com/v1beta/models/gemini-3-pro-preview:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {"text": "Summarize the attached PDF in one sentence."},
          {
            "inlineData": {
              "mimeType": "application/pdf",
              "data": "JVBERi0xLjQK..."
            }
          }
        ]
      }
    ]
  }'
本次生产环境测试中,模型成功返回了对 PDF 内容的单句摘要。

PDF + 结构化输出

下面的请求在生产环境成功返回了可直接 json.loads() 的 JSON 文本:
curl "https://crazyrouter.com/v1beta/models/gemini-3-pro-preview:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {"text": "Extract the key topic and whether the document mentions gamma."},
          {
            "inlineData": {
              "mimeType": "application/pdf",
              "data": "JVBERi0xLjQK..."
            }
          }
        ]
      }
    ],
    "generationConfig": {
      "responseMimeType": "application/json",
      "responseSchema": {
        "type": "object",
        "properties": {
          "topic": {"type": "string"},
          "mentions_gamma": {"type": "boolean"}
        },
        "required": ["topic", "mentions_gamma"]
      }
    }
  }'
生产环境实际返回示例:
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "{\n  \"topic\": \"Crazyrouter PDF test document\",\n  \"mentions_gamma\": false\n}"
          }
        ]
      }
    }
  ]
}
对应代码里直接解析即可:
import json

text = response["candidates"][0]["content"]["parts"][0]["text"]
data = json.loads(text)
当前页面只确认了 PDF 输入和 JSON 结构化输出这两条链路。更复杂的表格抽取、长文档对比等场景,建议先用你自己的真实样本再做一次生产验证。
大型 PDF 会显著增加输入 Token。正式接入前,建议先用小文档验证字段和提示词,再逐步扩大输入规模。