文件处理接口
通过 GPT Responses 接口传入文件 URL,让模型读取并分析 PDF 等文件内容
文件处理接口用于让模型读取并分析文件内容,例如 PDF 解析、题目提取、文档总结、合同审阅、表格信息抽取等。调用时使用 Responses 接口,并在 input 的 content 数组中同时传入文本指令和文件对象。
将示例中的 YOUR_API_KEY 替换为您自己的 API Key。文件需使用模型可访问的公网 URL;如果文件有访问权限限制,请先上传到可公网读取的位置。
GPT Responses 文件处理
GPT 模型推荐使用 Responses 接口处理文件。文本要求使用 input_text,文件 URL 使用 input_file。
POSTEndpoint
https://api.xty.app/v1/responses模型示例:gpt-5-mini
请求示例
curl https://api.xty.app/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-5-mini",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze this PDF and extract all questions in JSON format. Return only valid JSON."
},
{
"type": "input_file",
"file_url": "https://example.com/path/to/document.pdf"
}
]
}
]
}'请求体
{
"model": "gpt-5-mini",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze this PDF and extract all questions in JSON format. Return only valid JSON."
},
{
"type": "input_file",
"file_url": "https://example.com/path/to/document.pdf"
}
]
}
]
}Python 示例
import json
import requests
XTY_API_KEY = "YOUR_API_KEY"
RESPONSES_URL = "https://api.xty.app/v1/responses"
PDF_URL = "https://example.com/path/to/document.pdf"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {XTY_API_KEY}",
}
payload = {
"model": "gpt-5-mini",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze this PDF and extract all questions in JSON format. Return only valid JSON.",
},
{
"type": "input_file",
"file_url": PDF_URL,
},
],
}
],
}
response = requests.post(RESPONSES_URL, headers=headers, json=payload, timeout=60)
print(response.status_code)
print(json.dumps(response.json(), indent=2, ensure_ascii=False))响应示例
{
"id": "resp_abc123",
"object": "response",
"status": "completed",
"model": "gpt-5-mini",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "[{\"question\":\"...\",\"answer\":\"...\"}]"
}
]
}
],
"usage": {
"input_tokens": 1200,
"output_tokens": 300,
"total_tokens": 1500
}
}读取输出
Responses 接口的最终文本通常位于:
output[].content[].text实际业务中可以筛选 type 为 message 的对象,再读取其 content 中 type 为 output_text 的文本。
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 使用的模型,例如 gpt-5-mini |
input | array | 是 | 用户输入消息数组 |
input[].role | string | 是 | 消息角色,通常为 user |
content[].type | string | 是 | 内容类型,文本为 input_text,文件为 input_file |
content[].text | string | 文本内容必填 | 给模型的处理指令 |
content[].file_url | string | 文件内容必填 | 文件公网 URL |
注意事项
- 文件地址必须可被服务端访问,建议使用 HTTPS 公网 URL。
- PDF、文档、图片等文件处理效果取决于模型能力与文件内容质量。
- 如果需要结构化结果,可以在
input_text中明确要求返回 JSON,并说明字段格式。 - 大文件可能需要更长处理时间,建议客户端设置合理的请求超时时间。