外观
Chat Responses
创建对话响应,是 OpenAI Responses API 的兼容实现, 也意味着本平台支持接入Codex智能体。 请求体使用 input 描述上下文,instructions 描述系统指令,支持流式(SSE)、非流式与工具调用。内部会转换为 Chat Completions 格式后走同一条转发链路。
- 方法:
POST /v1/responses - 鉴权:必须使用 API Key(
agent = "api",即sk-开头的 Key)
请求体
对应结构体 RelayResponsesForm(manager/responses.go):
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 模型标识,如 gpt-4o |
input | string | InputItem[] | ✅ | 输入内容,字符串或输入项数组 |
instructions | string | ❌ | 系统指令,会转换为 system 消息 |
max_output_tokens | int | ❌ | 最大生成 token 数 |
temperature | float32 | ❌ | 采样温度 |
top_p | float32 | ❌ | 核采样 |
stream | bool | ❌ | 是否流式返回,默认 false |
tools | ResponsesTool[] | ❌ | 工具定义(见下方说明) |
tool_choice | any | ❌ | 工具选择策略 |
输入项结构(InputItem)
当 input 为数组时,每项对应 ResponsesInputItem(adapter/common/responses.go):
| 字段 | 类型 | 说明 |
|---|---|---|
type | string | 输入项类型:message / function_call / function_call_output |
role | string | 仅 message,system / user / assistant 等 |
content | string | ContentPart[] | 仅 message,文本或内容分片数组 |
name | string | 仅 message,可选,消息来源名称 |
call_id | string | 仅 function_call / function_call_output,调用 ID |
arguments | string | 仅 function_call,调用参数 JSON 字符串 |
output | string | 仅 function_call_output,函数执行结果 |
内容分片(ContentPart)支持 input_text 与 output_text 两种类型:
json
{ "type": "input_text", "text": "北京今天天气怎么样?" }工具结构(ResponsesTool)
| 字段 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 "function" |
name | string | 函数名 |
description | string | 函数描述 |
parameters | object | JSON Schema 参数定义 |
非流式响应
响应示例
json
{
"id": "resp-9a1b2c3d",
"object": "response",
"created_at": 1700000000,
"status": "completed",
"model": "gpt-4o",
"output": [
{
"id": "msg_9a1b2c3d",
"type": "message",
"status": "completed",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "你好!我是壹通提供的 AI 助手。",
"annotations": []
}
]
}
],
"usage": {
"input_tokens": 12,
"output_tokens": 18,
"total_tokens": 30
}
}响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 本次响应 ID,前缀 resp- |
object | string | 固定为 "response" |
created_at | int64 | 时间戳 |
status | string | "completed" |
model | string | 实际使用的模型 |
output | OutputItem[] | 输出项数组,含 message 与 function_call 两种 |
usage | ResponsesUsage | token 用量(input_tokens / output_tokens / total_tokens) |
非流式请求示例
bash
curl https://app.1in2.top/api/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "gpt-4o",
"instructions": "你是一个乐于助人的助手。",
"input": "用一句话介绍你自己",
"max_output_tokens": 200
}'python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key-here",
base_url="https://app.1in2.top/api/v1",
)
response = client.responses.create(
model="gpt-4o",
instructions="你是一个乐于助人的助手。",
input="用一句话介绍你自己",
)
print(response.output_text)流式响应(SSE)
设置 stream: true 后,服务端以 text/event-stream 返回,每个事件为一行 event: <type> + data: <json>,事件按顺序包含 sequence_number 序号。
event: response.created
data: {"type":"response.created","sequence_number":0,"response":{...}}
event: response.in_progress
data: {"type":"response.in_progress","sequence_number":1,"response":{...}}
event: response.output_text.delta
data: {"type":"response.output_text.delta","sequence_number":4,"delta":"你好",...}
event: response.completed
data: {"type":"response.completed","sequence_number":8,"response":{...}}流式事件序列
| 事件 | 说明 |
|---|---|
response.created | 响应已创建,status = "in_progress" |
response.in_progress | 响应进行中 |
response.output_item.added | 新增输出项(文本或工具调用) |
response.content_part.added | 新增内容分片 |
response.output_text.delta | 文本增量 |
response.function_call_arguments.delta | 工具调用参数增量 |
response.output_text.done | 文本完成 |
response.content_part.done | 内容分片完成 |
response.output_item.done | 输出项完成 |
response.function_call_arguments.done | 工具调用参数完成 |
response.completed | 响应完成,携带完整 output 与 usage |
response.failed | 响应失败,error 含错误信息 |
流式请求示例
bash
curl -N https://app.1in2.top/api/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "gpt-4o",
"input": "讲一个笑话",
"stream": true
}'python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key-here",
base_url="https://app.1in2.top/api/v1",
)
stream = client.responses.create(
model="gpt-4o",
input="讲一个笑话",
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)工具调用
使用 tools 声明工具,模型会返回 function_call 类型的输出项。
请求示例(工具调用)
bash
curl https://app.1in2.top/api/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "gpt-4o",
"input": "北京今天天气怎么样?",
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名" }
},
"required": ["city"]
}
}
],
"tool_choice": "auto"
}'响应(工具调用被触发)
此时 output 中包含 function_call 类型的输出项:
json
{
"id": "resp-9a1b2c3d",
"object": "response",
"status": "completed",
"model": "gpt-4o",
"output": [
{
"id": "call_abc123",
"type": "function_call",
"name": "get_weather",
"call_id": "call_abc123",
"arguments": "{\"city\":\"北京\"}",
"status": "completed"
}
],
"usage": { "input_tokens": 15, "output_tokens": 10, "total_tokens": 25 }
}将函数执行结果以 function_call_output 输入项回传,携带对应的 call_id,即可继续多轮对话:
json
{
"model": "gpt-4o",
"input": [
{ "type": "message", "role": "user", "content": "北京今天天气怎么样?" },
{
"type": "function_call",
"call_id": "call_abc123",
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
},
{
"type": "function_call_output",
"call_id": "call_abc123",
"output": "{\"temperature\": \"18°C\", \"condition\": \"晴\"}"
}
]
}常见错误
| 错误类型 | 触发场景 |
|---|---|
access_denied_error | 中继被关闭(CloseRelay) |
authentication_error | 未携带 Key 或非 api 身份 |
invalid_request_error | 请求体非法、缺少 model / input,或输入项类型不支持 |
access_denied_error | 模型不在 API Key 白名单 |
quota_exceeded_error | 额度不足或套餐不可用 |
