通过 UniGateway 使用 OpenAI Responses API
通过 UniGateway 调用 OpenAI 的有状态 Responses API,用于面向 Agent 的工作流、内置工具和服务端会话状态管理。
概述
Responses API 是 OpenAI 的下一代 Agent 交互接口,提供内置工具、结构化输出和服务端状态管理。
核心能力包括:
- 有状态会话:通过
previous_response_id由服务端保留历史记录 - 内置工具:网页搜索、代码解释器、计算机使用、文件搜索
- 推理 token:模型在最终输出前进行推理
- 结构化输出:保证输出符合指定 JSON Schema
UniGateway 暴露以下端点:
POST /v1/responses— 创建响应GET /v1/responses/{id}— 获取响应POST /v1/responses/{id}/input_items— 追加到会话
鉴权
所有请求需要 UniGateway API Key:
Authorization: Bearer <YOUR_UNIGATEWAY_API_KEY>
支持的模型
UniGateway 上支持 Responses API 的 OpenAI 模型:
| 模型 ID | 说明 | 输入 / 1M | 输出 / 1M |
|---|---|---|---|
gpt-5.4 | 通用平衡型 | $2.50 | $15.00 |
gpt-5.4-pro | 前沿推理与 Agent 型 | $30.00 | $180.00 |
gpt-5.3-codex | 代码与推理专用 | $1.75 | $14.00 |
查询实时模型列表:
curl https://api.unigateway.ai/v1/models \
-H "Authorization: Bearer $UNIGATEWAY_API_KEY"
创建响应
基本请求
curl https://api.unigateway.ai/v1/responses \
-H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"input": "用一段话解释量子计算。"
}'
带会话历史
curl https://api.unigateway.ai/v1/responses \
-H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"input": [
{"role": "user", "content": [{"type": "text", "text": "2+2 等于多少?"}]},
{"role": "assistant", "content": [{"type": "text", "text": "4"}]},
{"role": "user", "content": [{"type": "text", "text": "再乘以 10。"}]}
]
}'
有状态会话
curl https://api.unigateway.ai/v1/responses \
-H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"input": "再乘以 10。",
"previous_response_id": "resp_abc123"
}'
设置 previous_response_id 后,服务端自动管理会话历史。省略该字段时由客户端自行管理状态。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | GET /v1/models 返回的模型 ID |
input | string / array | 是 | 用户消息文本或消息项数组 |
previous_response_id | string | 否 | 服务端管理的会话状态 |
store | boolean | 否 | 服务端保存响应,默认 true |
tools | array | 否 | 启用的内置工具 |
tool_choice | string / object | 否 | "auto"、"required"、"none" 或指定工具 |
instructions | string | 否 | 系统级指令 |
temperature | number | 否 | 采样温度,如 0.2 |
max_tokens | number | 否 | 输出 token 上限 |
top_p | number | 否 | 核采样 |
response_format | object | 否 | 结构化输出 schema |
reasoning | object | 否 | 推理强度控制 |
parallel_tool_calls | boolean | 否 | 是否允许多工具并行调用 |
metadata | object | 否 | 自定义键值对 |
Input 数组格式
每项包含:
{
"role": "user",
"content": [
{"type": "text", "text": "..."},
{"type": "image_url", "image_url": {"url": "..."}}
]
}
role 支持 user、assistant、system、developer。
响应格式
{
"id": "resp_abc123",
"object": "response",
"status": "completed",
"model": "gpt-5.4",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{"type": "text", "text": "量子计算利用叠加和纠缠..."}
]
}
],
"usage": {
"input_tokens": 12,
"output_tokens": 89,
"total_tokens": 101
}
}
响应字段
| 字段 | 说明 |
|---|---|
id | 响应 ID,作为下一轮 previous_response_id |
status | "completed"、"in_progress"、"failed"、"cancelled" |
output | 输出项数组(文本、工具调用、推理) |
output[].type | "message"、"reasoning"、"tool_call" |
输出项类型
| 类型 | 说明 |
|---|---|
message | 助手消息,含 role 和 content 数组 |
reasoning | 模型推理 token,不向最终用户展示 |
tool_call | 工具调用请求,含 call_id、type、arguments |
内置工具
在 tools 数组中启用。
网页搜索
{
"tools": [
{"type": "web_search_preview"}
]
}
代码解释器
{
"tools": [
{"type": "code_interpreter"}
]
}
计算机使用
{
"tools": [
{
"type": "computer_use_preview",
"environment": "browser"
}
]
}
错误处理
| 状态码 | 原因 | 处理方式 |
|---|---|---|
400 | 请求参数错误 | 检查 JSON 结构和参数类型 |
401 | API Key 无效或缺失 | 检查 Authorization 请求头 |
404 | 模型 ID 不存在 | 重新确认 GET /v1/models |
429 | 触发限流 | 增加退避并重试 |
5xx | 服务异常 | 指数退避重试 |