GPT 结构化输出与 PDF
本页说明 GPT 系列的结构化输出与 PDF 输入写法;模型 ID 与接口配置见 GPT 系列概览。
结构化输出
通过 response_format 传入 JSON Schema,strict: true 时输出严格符合 Schema:
gpt_json_schema.py
import json
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"])
resp = client.chat.completions.create(
model="openai/gpt-5.4-mini",
response_format={
"type": "json_schema",
"json_schema": {
"name": "price",
"strict": True,
"schema": {
"type": "object",
"properties": {"price": {"type": "number"}, "currency": {"type": "string"}},
"required": ["price", "currency"],
"additionalProperties": False,
},
},
},
messages=[{"role": "user", "content": "球拍和球共 1.10 美元,球拍比球贵 1 美元,球多少钱?"}],
)
print(json.loads(resp.choices[0].message.content))更多写法见结构化输出。
PDF 输入
Chat Completions
gpt_pdf.py
import base64
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"])
pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode()
resp = client.chat.completions.create(
model="openai/gpt-5.4-mini",
messages=[{"role": "user", "content": [
{"type": "file", "file": {"filename": "invoice.pdf", "file_data": f"data:application/pdf;base64,{pdf_b64}"}},
{"type": "text", "text": "这张发票的总金额是多少?"},
]}],
)
print(resp.choices[0].message.content)其他报错见常见报错。
最后更新于