Responses API
OpenAI 新一代有状态接口,支持工具、网页搜索、文件检索与多步任务,逐步替代 Chat Completions。
创建 Response
POST /v1/responses请求REQUEST
POST
https://test.kailin.com.cn/v1/responses响应RESPONSE
填写参数并发送请求,响应会显示在这里
请求
curl https://test.kailin.com.cn/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的令牌" \
-d '{
"model": "gpt-4o-mini",
"input": "用一句话介绍 Responses API"
}'
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
input | string / array | 是 | 输入文本,或消息/内容块数组 |
stream | boolean | 否 | 流式输出(事件类型更丰富) |
instructions | string | 否 | 系统级指令 |
previous_response_id | string | 否 | 传入上一次的 Response ID 实现多轮衔接,无需重发历史 |
tools | array | 否 | 工具列表,支持 function、web_search_preview 等 |
temperature / top_p | number | 否 | 采样参数 |
max_output_tokens | number | 否 | 最大输出 token 数 |
metadata | object | 否 | 自定义键值对(16 组) |
store | boolean | 否 | 是否保存结果供 previous_response_id 引用 |
input 数组形式示例(含图片):
{
"model": "gpt-4o-mini",
"input": [
{
"role": "user",
"content": [
{ "type": "input_text", "text": "描述这张图" },
{ "type": "input_image", "image_url": "https://example.com/cat.jpg" }
]
}
]
}
响应
{
"id": "resp_abc123",
"object": "response",
"created_at": 1730000000,
"status": "completed",
"model": "gpt-4o-mini",
"output": [
{
"type": "message",
"id": "msg_abc123",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Responses API 是 OpenAI 的新一代有状态接口……",
"annotations": []
}
]
}
],
"usage": {
"input_tokens": 12,
"output_tokens": 30,
"total_tokens": 42
}
}
提取文本可遍历 output 中 type: "message" 项的 content[].text;SDK 提供 resp.output_text 快捷属性。
查询 Response
GET /v1/responses/{response_id}curl https://test.kailin.com.cn/v1/responses/resp_abc123 \
-H "Authorization: Bearer sk-你的令牌"
返回完整 Response 对象;创建时 store: false 的记录无法查询。
压缩上下文
POST /v1/responses/compact将长会话上下文压缩为摘要,用于 Agent 长任务场景,减少后续请求的 token 消耗:
curl https://test.kailin.com.cn/v1/responses/compact \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的令牌" \
-d '{
"model": "gpt-4o-mini",
"input": [ ...长对话消息数组... ]
}'
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 用于压缩的模型 |
input | array | 是 | 需要压缩的消息数组 |
instructions | string | 否 | 压缩摘要的定制指令 |
返回压缩后的摘要对象,可放入下一次请求的 input 中。
备注
WebSocket 方式调用(wss://test.kailin.com.cn/v1/responses,response.create 事件)依赖网关对 WS 的透传支持,客户端接入仍以 REST + SSE 为主,此处不展开。