支持 OpenAI 兼容调用,当前提供 /v1/responses、/v1/chat/completions 两类核心端点。
http://127.0.0.1:8082(按你的部署端口替换)
请求头携带 Authorization: Bearer sk-xxxx
以 GET /v1/models 的实时返回为准。
无 /v1 前缀同样可用:/responses、/chat/completions、/models
/v1/responses 作为主对话端点,或使用 /v1/chat/completions 进行 OpenAI 兼容调用。| 端点 | 定位 | 文本对话 | 流式 | tools/function_call | 多模态输入 |
|---|---|---|---|---|---|
POST /v1/responses |
原生全功能路径 | 支持 | 支持 | 支持 | 支持 |
POST /v1/chat/completions |
OpenAI 兼容路径 | 支持 | 支持 | 支持(基础到中等复杂) | 基础兼容(常见 image_url 场景) |
| 字段 | /v1/responses |
说明 |
|---|---|---|
stream |
支持(true/false) | 传 false 时网关内部仍强制 stream,响应透明转换。 |
tools / tool_choice |
支持 | 支持原生 function_call,也会自动从历史补全工具定义。 |
include / parallel_tool_calls |
支持 | 默认补齐 reasoning.encrypted_content。 |
instructions |
支持 | 网关会自动补默认值:You are a helpful assistant. |
input[].role=system |
兼容 | 会自动归一化为 developer。 |
input_text/output_text |
兼容 | 会按角色自动归一化,避免类型错误。 |
model=gpt-5.4 时,网关会固定使用 reasoning.effort=xhigh。获取可用模型:GET /v1/models
curl -s http://127.0.0.1:8082/v1/models \
-H "Authorization: Bearer sk-你的Key"
模型会随上游调整,建议始终以 /v1/models 的实时返回为准。
| 场景 | 建议入口 | 说明 |
|---|---|---|
| 新项目接入 | POST /v1/responses |
能力更完整,适合作为主对话端点。 |
| 兼容现有 OpenAI 代码 | POST /v1/chat/completions |
便于低成本迁移既有 SDK 或历史项目。 |
| 查看可用模型 | GET /v1/models |
模型清单会随上游调整,建议以实时返回为准。 |
| 查看使用信息 | 用户门户 | 适合持有 API Key 的用户查看当前使用情况与相关说明。 |
/v1/responses 更接近上游能力,建议新项目优先用这个端点。
curl -s http://127.0.0.1:8082/v1/responses \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.1-codex-mini",
"stream":false,
"input":[
{"role":"user","content":[{"type":"input_text","text":"你好,做个自我介绍"}]}
]
}'
curl -N http://127.0.0.1:8082/v1/responses \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.1-codex-mini",
"stream":true,
"input":[
{"role":"user","content":[{"type":"input_text","text":"给我三条 Go 调试建议"}]}
]
}'
/v1/responses 请求头启用透传开关。curl -N http://127.0.0.1:8082/v1/responses \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-H "X-Codex-Mode: raw" \
-d '{
"model":"gpt-5.3-codex",
"stream":true,
"input":[
{"role":"user","content":[{"type":"input_text","text":"你好"}]}
]
}'
等价开关:X-Codex-Raw-Passthrough: true。
注意:透传模式下不再做自动兼容修正(例如强制 stream、XML tool 协议文本清洗等),调用方需自行处理上游原生行为。
第一轮请求(触发函数调用):
curl -s http://127.0.0.1:8082/v1/responses \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.3-codex",
"stream":false,
"input":[
{"role":"user","content":[{"type":"input_text","text":"请先调用 list(limit=20, offset=0),再回答我看到多少个工具。"}]}
],
"tools":[
{
"type":"function",
"name":"list",
"description":"列出可用工具",
"parameters":{
"type":"object",
"properties":{"limit":{"type":"integer"},"offset":{"type":"integer"}},
"required":["limit","offset"]
}
}
],
"tool_choice":{"type":"function","name":"list"}
}'
第二轮请求(携带 function_call_output 续传):
{
"model":"gpt-5.3-codex",
"stream":false,
"input":[
{"type":"message","role":"user","content":[{"type":"input_text","text":"请先调用 list(limit=20, offset=0),再回答我看到多少个工具。"}]},
{"type":"function_call","call_id":"call_xxx","name":"list","arguments":"{\"limit\":20,\"offset\":0}"},
{"type":"function_call_output","call_id":"call_xxx","output":"{\"total\":4,\"tools\":[...]}"}
]
}
通用客户端(文本、流式、基础 tools)可直接使用 /v1/chat/completions。
curl -s http://127.0.0.1:8082/v1/chat/completions \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.1-codex-mini",
"stream":false,
"messages":[
{"role":"user","content":"写一个 Go 读取 JSON 的示例"}
]
}'
curl -N http://127.0.0.1:8082/v1/chat/completions \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.1-codex-mini",
"stream":true,
"messages":[
{"role":"user","content":"给我一段快速排序伪代码"}
]
}'
curl -s http://127.0.0.1:8082/v1/chat/completions \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.1-codex-mini",
"stream":false,
"messages":[{"role":"user","content":"帮我查上海天气"}],
"tools":[
{
"type":"function",
"function":{
"name":"get_weather",
"description":"查询天气",
"parameters":{
"type":"object",
"properties":{"city":{"type":"string"}},
"required":["city"]
}
}
}
],
"tool_choice":"auto"
}'
返回里会包含 choices[0].message.tool_calls(若模型触发了函数调用)。
curl -s http://127.0.0.1:8082/v1/chat/completions \
-H "Authorization: Bearer sk-你的Key" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5.1-codex-mini",
"messages":[
{
"role":"user",
"content":[
{"type":"text","text":"描述这张图片"},
{"type":"image_url","image_url":{"url":"https://example.com/demo.png"}}
]
}
]
}'
| 报错 | 原因 | 处理 |
|---|---|---|
Stream must be set to true |
上游 codex responses 强制流式 | 网关已自动处理,无需手工改客户端。 |
Invalid value: input_text |
assistant 历史消息类型不匹配 | 网关已做角色映射:assistant 转 output_text。 |
401 Unauthorized |
API Key 无效或未携带 | 请在请求头添加 Authorization: Bearer sk-xxxx。 |
回复直接出现 <tool_use>...</tool_use> 文本 |
请求未携带可执行 tools,模型只能输出文本格式工具指令 |
传入标准 tools 和 tool_choice,按函数调用链路续传结果。 |
所有 API 请求需在 Authorization 头携带 Bearer sk-xxxx。网关会依次检查 Key 状态、有效期、模型权限、配额、速率和并发。
| HTTP 状态码 | error.code | 含义 |
|---|---|---|
| 401 | missing_api_key | 请求未携带 API Key |
| 401 | invalid_api_key | API Key 不存在 |
| 401 | api_key_expired | API Key 已过期 |
| 403 | api_key_disabled | API Key 已被禁用(状态非 active) |
| 403 | model_not_allowed | 请求的模型不在白名单内或在黑名单中 |
| 429 | quota_exceeded | 配额超限(余额/Token 用完) |
| 429 | rate_limit_rpm | 每分钟请求数(RPM)超限 |
| 429 | rate_limit_rpd | 每日请求数(RPD)超限 |
| 429 | concurrent_limit | 并发请求数超限 |
| 400 | max_tokens_exceeded | 请求中的 max_tokens / max_output_tokens 超过 Key 允许的上限 |
错误响应格式:
{
"error": {
"message": "Rate limit exceeded: too many requests per minute",
"type": "invalid_request_error",
"code": "rate_limit_rpm"
}
}
限流策略说明:
max_tokens 和 max_output_tokens,取较大值与限制比较