在 Chat 或 Messages 请求中引用上传文件、PDF 与多模态内容的完整流程。

典型流程(OpenAI 兼容)

1. POST /files 上传 → 获得 file_id
2. POST /chat/completions,messages 中使用 input_file + file_id
3. (可选)GET /files/:id/content 下载
4. DELETE /files/:id 清理

API 细节见 文件Chat Completions

在消息中引用文件

用户消息 content 为数组时,使用 input_file part(三选一):

模式字段
已上传file_id
公网 URLfile_url(须 http/https)
内联file_data + filename
{
  "role": "user",
  "content": [
    { "type": "text", "text": "总结附件要点" },
    { "type": "input_file", "file_id": "file-xxxxxxxx" }
  ]
}

同一 part 不能混用多种模式,否则 400。

上传限制

  • 默认单文件上限约 32 MB(以网关部署配置为准)。
  • 上传字段名须为 file(multipart),purpose 默认 assistants
  • 文件与 创建它的 API Key 绑定;换 Key 可能无法访问他人上传的文件。

PDF 预处理(网关扩展)

在 chat 请求体设置 pdf_preprocess

{
  "pdf_preprocess": {
    "engine": "ocr",
    "max_pages": 20
  }
}
engine说明
native不展开,原样转发
ocrOCR 后送上游
markdown提取 Markdown

Anthropic Messages 同样支持 pdf_preprocess(见 Messages API)。

图片

使用 image_url part,URL 须为 http/https。部分模型不支持视觉模态 — 选模型时参考目录中的 input_modalities

清理与合规

  • 定期 DELETE 不再需要的 file_id
  • 勿在文件中包含未脱敏的敏感信息;日志中避免记录文件内容。

相关