Skip to content

Chat Responses ​

创建对话响应,是 OpenAI Responses API 的兼容实现, 也意味着本平台支持接入Codex智能体。 请求体使用 input 描述上下文,instructions 描述系统指令,支持流式(SSE)、非流式与工具调用。内部会转换为 Chat Completions 格式后走同一条转发链路。

  • 方法:POST /v1/responses
  • 鉴权:必须使用 API Key(agent = "api",即 sk- 开头的 Key)

请求体 ​

对应结构体 RelayResponsesForm(manager/responses.go):

字段类型必填说明
modelstring✅模型标识,如 gpt-4o
inputstring | InputItem[]✅输入内容,字符串或输入项数组
instructionsstring❌系统指令,会转换为 system 消息
max_output_tokensint❌最大生成 token 数
temperaturefloat32❌采样温度
top_pfloat32❌核采样
streambool❌是否流式返回,默认 false
toolsResponsesTool[]❌工具定义(见下方说明)
tool_choiceany❌工具选择策略

输入项结构(InputItem) ​

当 input 为数组时,每项对应 ResponsesInputItem(adapter/common/responses.go):

字段类型说明
typestring输入项类型:message / function_call / function_call_output
rolestring仅 message,system / user / assistant 等
contentstring | ContentPart[]仅 message,文本或内容分片数组
namestring仅 message,可选,消息来源名称
call_idstring仅 function_call / function_call_output,调用 ID
argumentsstring仅 function_call,调用参数 JSON 字符串
outputstring仅 function_call_output,函数执行结果

内容分片(ContentPart)支持 input_text 与 output_text 两种类型:

json
{ "type": "input_text", "text": "北京今天天气怎么样?" }

工具结构(ResponsesTool) ​

字段类型说明
typestring固定为 "function"
namestring函数名
descriptionstring函数描述
parametersobjectJSON 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
  }
}

响应字段 ​

字段类型说明
idstring本次响应 ID,前缀 resp-
objectstring固定为 "response"
created_atint64时间戳
statusstring"completed"
modelstring实际使用的模型
outputOutputItem[]输出项数组,含 message 与 function_call 两种
usageResponsesUsagetoken 用量(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额度不足或套餐不可用