Responses API
OpenAI 新一代对话接口:输入更简洁,输出按条目返回,推理、工具和结构化输出的参数更统一。
POST /openai/v1/responses需要 Gateway Key支持流式更新于 2026-11-20快速上手
- 接口:
POST /openai/v1/responses,与 OpenAI Responses API 格式相同,使用 OpenAI 官方 SDK 的client.responses.create。 - 必填:
model和input;input可以是一段文字,也可以是消息数组。 - 计费:与 Chat Completions 相同,按实际 Token 用量计费。
- 关键约束:每次请求都要带上需要的历史消息,见多轮对话。
from openai import OpenAI
client = OpenAI(
base_url="https://llmapi.memene.cn/openai/v1",
api_key="gw_memene_<keyId>_<secret>",
)
res = client.responses.create(
model="gpt-5.5",
instructions="你是一名简洁的中文助手。",
input="用一句话解释什么是向量数据库。",
)
print(res.output_text)
print(res.usage)响应的 output 是条目数组,可能包含推理摘要、文本消息和工具调用;只需要文字时,SDK 的 output_text 会把所有文本拼接好。用量在 usage.input_tokens 和 usage.output_tokens 中。
{
"id": "resp-abc123",
"object": "response",
"created_at": 1700000000,
"model": "gpt-5.5",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "向量数据库是专门存储和检索向量的数据库,能按语义相似度快速找到最相关的内容。"
}
]
}
],
"usage": {
"input_tokens": 31,
"output_tokens": 32,
"total_tokens": 63
}
}与 Chat Completions 的区别
| Chat Completions | Responses | |
|---|---|---|
| 输入 | messages 数组 | input(文字或消息数组)+ instructions |
| 输出 | choices[0].message | output 条目数组,output_text 便捷读取 |
| 推理档位 | reasoning_effort | reasoning.effort |
| 输出上限 | max_completion_tokens | max_output_tokens |
| 结构化输出 | response_format | text.format |
| 函数定义 | {type, function: {name, parameters}} | {type, name, parameters}(扁平) |
| 流式事件 | chat.completion.chunk | 带类型的语义事件,如 response.output_text.delta |
可用模型与 Chat Completions 相同,见 Chat Completions 可用模型。参数以 OpenAI 模型的支持最完整;其他厂商的模型也可以调用,但部分 OpenAI 专有参数可能不生效。
多轮对话
把历史消息按顺序放进 input 数组即可。同一模型的多次请求可能由不同的上游渠道处理,previous_response_id 等依赖厂商侧会话存储的参数不保证可用,建议始终由客户端携带历史。
history = [
{"role": "user", "content": "我叫小林。"},
{"role": "assistant", "content": "你好,小林!"},
{"role": "user", "content": "我叫什么名字?"},
]
res = client.responses.create(model="gpt-5.5", input=history)
print(res.output_text)流式输出
设置 stream: true 后以 SSE 返回带类型的事件:文本增量是 response.output_text.delta,结束时的 response.completed 事件包含完整响应和 usage。超时规则与 Chat Completions 相同:单次请求最长约 30 分钟;流式请求连续 30 秒没有新数据视为中断。
stream = client.responses.create(
model="gpt-5.5",
input="写一首关于秋天的四行短诗。",
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type == "response.completed":
print("\n", event.response.usage)推理
用 reasoning.effort 设置推理档位,取值与 Chat Completions 的 reasoning_effort 相同,模型支持的额外档位见可用模型。推理 Token 计入 usage.output_tokens。
res = client.responses.create(
model="gpt-5.5",
reasoning={"effort": "high"},
input="证明:任意两个奇数之和是偶数。",
)
print(res.output_text)
print(res.usage.output_tokens_details) # 含推理 Token 数工具调用与结构化输出
函数工具的定义是扁平结构,模型发起的调用以 function_call 条目出现在 output 中;执行后把结果作为 function_call_output 条目(带 call_id)加入下一次请求的 input。结构化输出通过 text.format 指定 JSON Schema。
# 函数工具:Responses 的工具定义是扁平结构
tools = [{
"type": "function",
"name": "get_weather",
"description": "查询城市当前天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}]
res = client.responses.create(model="gpt-5.5", input="上海现在天气怎么样?", tools=tools)
calls = [item for item in res.output if item.type == "function_call"]
# 结构化输出:通过 text.format 指定 JSON Schema
res = client.responses.create(
model="gpt-5.5",
input="从这句话提取人名和城市:张伟下周去杭州出差。",
text={"format": {
"type": "json_schema",
"name": "extraction",
"strict": True,
"schema": {
"type": "object",
"properties": {"name": {"type": "string"}, "city": {"type": "string"}},
"required": ["name", "city"],
"additionalProperties": False,
},
}},
)OpenAI 的内置工具(如网页搜索、文件检索、代码解释器)依赖厂商侧的配套资源和单独计价,本平台不保证可用。需要联网能力时请使用 DeepSeek 联网搜索与阅读链接。
请求参数
下表为常用参数,其余参数与 OpenAI Responses API 定义一致,是否生效取决于模型。
Content-Type:application/json
| 参数 | 必填 | 说明 |
|---|---|---|
| model string | 必填 | 公开模型 ID,与 Chat Completions 的可用模型相同。 |
| input string | object[] | 必填 | 输入内容,可以是纯文本或消息数组。 |
| stream boolean | 可选 | 是否启用流式输出,默认 false。流式返回带类型的 SSE 事件,用量在 response.completed 事件中。 |
| temperature number | 可选 | 采样温度,范围 0~2;推理模型通常不支持。 |
| max_output_tokens number | 可选 | 输出(含推理 Token)的最大数量,不能超过模型最大输出。 |
| instructions string | 可选 | 系统指令,作用相当于 system 消息。 |
| reasoning object | 可选 | 推理设置,如 {effort: "high"};档位取值与 Chat Completions 的 reasoning_effort 相同。 |
| tools object[] | 可选 | 函数工具列表,每项为扁平结构 {type: "function", name, description, parameters}。 |
| text object | 可选 | 文本输出设置,结构化输出使用 {format: {type: "json_schema", name, schema}}。 |
常见问题
新项目应该用 Responses 还是 Chat Completions?
主要使用 OpenAI 模型、希望用到推理摘要等新特性时选 Responses;需要在多个厂商的模型之间切换、或依赖的框架只支持 Chat Completions 时,选 Chat Completions 兼容性更好。
出错时如何排查?
本平台的错误码与 Chat Completions 一致,见 常见错误码。上游返回的 4xx 会原样透传,错误信息中通常说明了不被支持的参数。