多模态

PDF 输入

OpenRoute 通过 /api/v1/chat/completions API 支持 PDF 处理。PDF 可以作为直接 URL 或base64 编码的数据 URL 通过文件内容类型在消息数组中发送。此功能适用于 OpenRoute 上的任何模型。

URL 支持:直接发送公开可访问的 PDF,无需下载或编码
Base64 支持:本地文件或非公开访问的私有文档需要此方式

PDF 也可以在聊天室中进行交互式测试。

当模型原生支持文件输入时,PDF 直接传递给模型。当模型不原生支持文件输入时,OpenRoute 将解析文件并将解析结果传递给请求的模型。

您可以在同一请求中发送 PDF 和其他文件类型。

插件配置

要配置 PDF 处理,请在请求中使用 plugins 参数。OpenRoute 提供多种具有不同功能和定价的 PDF 处理引擎:

{
  plugins: [
    {
      id: 'file-parser',
      pdf: {
        engine: 'pdf-text', // or 'mistral-ocr' or 'native'
      },
    },
  ],
}

定价

OpenRoute 提供多种 PDF 处理引擎:

  1. "mistral-ocr":最适合扫描文档或包含图像的 PDF(每 1,000 页 $0.50)。
  2. "pdf-text":最适合结构良好的 PDF,具有清晰的文本内容(免费)。
  3. "native":仅适用于原生支持文件输入的模型(按输入 token 收费)。

如果您没有明确指定引擎,OpenRoute 将首先默认为模型的原生文件处理功能,如果不可用,我们将使用 "pdf-text" 引擎。

使用 PDF URL

对于公开可访问的 PDF,您可以直接发送 URL,无需下载和编码文件:

import requests
import json

url = "https://openroute.cn/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What are the main points in this document?"
            },
            {
                "type": "file",
                "file": {
                    "filename": "document.pdf",
                    "file_data": "https://bitcoin.org/bitcoin.pdf"
                }
            },
        ]
    }
]

# Optional: Configure PDF processing engine
plugins = [
    {
        "id": "file-parser",
        "pdf": {
            "engine": "mistral-ocr"
        }
    }
]

payload = {
    "model": "anthropic/claude-sonnet-4",
    "messages": messages,
    "plugins": plugins
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

PDF URL 适用于所有处理引擎。对于 Mistral OCR,URL 直接传递给服务。对于其他引擎,OpenRoute 获取 PDF 并在内部处理。

使用 Base64 编码的 PDF

对于本地 PDF 文件或需要直接发送 PDF 内容时,您可以对文件进行 base64 编码:

import requests
import json
import base64
from pathlib import Path

def encode_pdf_to_base64(pdf_path):
    with open(pdf_path, "rb") as pdf_file:
        return base64.b64encode(pdf_file.read()).decode('utf-8')

url = "https://openroute.cn/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

# Read and encode the PDF
pdf_path = "path/to/your/document.pdf"
base64_pdf = encode_pdf_to_base64(pdf_path)
data_url = f"data:application/pdf;base64,{base64_pdf}"

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What are the main points in this document?"
            },
            {
                "type": "file",
                "file": {
                    "filename": "document.pdf",
                    "file_data": data_url
                }
            },
        ]
    }
]

# Optional: Configure PDF processing engine
# PDF parsing will still work even if the plugin is not explicitly set
plugins = [
    {
        "id": "file-parser",
        "pdf": {
            "engine": "pdf-text"  # defaults to "pdf-text". See Pricing above
        }
    }
]

payload = {
    "model": "google/gemma-3-27b-it",
    "messages": messages,
    "plugins": plugins
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

跳过解析成本

当您向 API 发送 PDF 时,响应可能包含助手消息中的文件注释。这些注释包含有关已解析 PDF 文档的结构化信息。通过在后续请求中发送这些注释,您可以避免多次重新解析同一个 PDF 文档,从而节省处理时间和成本。

以下是重用文件注释的方法:

import requests
import json
import base64
from pathlib import Path

# First, encode and send the PDF
def encode_pdf_to_base64(pdf_path):
    with open(pdf_path, "rb") as pdf_file:
        return base64.b64encode(pdf_file.read()).decode('utf-8')

url = "https://openroute.cn/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

# Read and encode the PDF
pdf_path = "path/to/your/document.pdf"
base64_pdf = encode_pdf_to_base64(pdf_path)
data_url = f"data:application/pdf;base64,{base64_pdf}"

# Initial request with the PDF
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What are the main points in this document?"
            },
            {
                "type": "file",
                "file": {
                    "filename": "document.pdf",
                    "file_data": data_url
                }
            },
        ]
    }
]

payload = {
    "model": "google/gemma-3-27b-it",
    "messages": messages
}

response = requests.post(url, headers=headers, json=payload)
response_data = response.json()

# Store the annotations from the response
file_annotations = None
if response_data.get("choices") and len(response_data["choices"]) > 0:
    if "annotations" in response_data["choices"][0]["message"]:
        file_annotations = response_data["choices"][0]["message"]["annotations"]

# Follow-up request using the annotations (without sending the PDF again)
if file_annotations:
    follow_up_messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "What are the main points in this document?"
                },
                {
                    "type": "file",
                    "file": {
                        "filename": "document.pdf",
                        "file_data": data_url
                    }
                }
            ]
        },
        {
            "role": "assistant",
            "content": "The document contains information about...",
            "annotations": file_annotations
        },
        {
            "role": "user",
            "content": "Can you elaborate on the second point?"
        }
    ]

    follow_up_payload = {
        "model": "google/gemma-3-27b-it",
        "messages": follow_up_messages
    }

    follow_up_response = requests.post(url, headers=headers, json=follow_up_payload)
    print(follow_up_response.json())

当您在后续请求中包含来自先前响应的文件注释时,OpenRoute 将使用此预解析信息而不是重新解析 PDF,从而节省处理时间和成本。这对于大型文档或使用产生额外成本的 mistral-ocr 引擎特别有益。

响应格式

API 将返回以下格式的响应:

{
  "id": "gen-1234567890",
  "provider": "DeepInfra",
  "model": "google/gemma-3-27b-it",
  "object": "chat.completion",
  "created": 1234567890,
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "The document discusses..."
      }
    }
  ],
  "usage": {
    "prompt_tokens": 1000,
    "completion_tokens": 100,
    "total_tokens": 1100
  }
}

On this page