Responses
Responses API 是 零派智能API 提供的对话接口,支持更灵活的对话编排与状态管理,适用于需要多轮交互和工具调用的场景。
Endpoint
POST /v1/responsesMethod
POST
Authentication
需要 Bearer Token 认证:
Authorization: Bearer $API_KEYRequest Headers
| Header | 是否必需 | 说明 |
|---|---|---|
Authorization | 是 | Bearer <API_KEY> |
Content-Type | 是 | application/json |
Request Body
| 参数 | 类型 | 是否必需 | 说明 |
|---|---|---|---|
model | string | 是 | 模型标识,如 gpt-4o |
input | string | array | 是 | 输入内容,可以是字符串或消息数组 |
instructions | string | 否 | 系统指令,设定助手行为 |
temperature | number | 否 | 采样温度,范围 0-2,默认 1 |
max_output_tokens | integer | 否 | 输出 token 数量上限 |
stream | boolean | 否 | 是否流式返回,默认 false |
tools | array | 否 | 工具定义,支持函数调用 |
previous_response_id | string | 否 | 上一轮响应 ID,用于延续对话 |
请求示例
{ "model": "gpt-4o", "input": "用一句话介绍 零派智能API。", "instructions": "你是一个有帮助的助手。", "temperature": 0.7, "max_output_tokens": 1000}Response
{ "id": "resp_abc123", "object": "response", "created_at": 1699999999, "model": "gpt-4o", "output": [ { "type": "message", "id": "msg_abc123", "status": "completed", "role": "assistant", "content": [ { "type": "output_text", "text": "零派智能API 是一个兼容 OpenAI 格式的 AI 模型聚合平台。" } ] } ], "usage": { "input_tokens": 25, "output_tokens": 20, "total_tokens": 45 }, "status": "completed"}响应字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 响应 ID,可用于后续对话延续 |
object | string | 固定为 response |
created_at | integer | 创建时间(Unix 时间戳) |
model | string | 实际使用的模型标识 |
output | array | 输出内容数组 |
usage | object | token 用量统计 |
status | string | 响应状态:completed、in_progress、failed |
与 Chat Completions 的区别
| 特性 | Chat Completions | Responses |
|---|---|---|
| 输入格式 | messages 数组 | input 字符串或数组 |
| 对话状态 | 需自行维护完整上下文 | 可通过 previous_response_id 延续 |
| 系统指令 | messages 中的 system 角色 | 独立的 instructions 字段 |
| 输出结构 | choices[].message.content | output[].content[].text |
| 适用场景 | 简单单轮/多轮对话 | 复杂编排、工具调用、状态管理 |
Streaming
将 stream 设置为 true 即可启用流式响应。响应以 SSE 格式逐步返回。详见 Streaming。
错误码
| 状态码 | 说明 |
|---|---|
| 400 | 请求参数错误 |
| 401 | API Key 无效或缺失 |
| 403 | 无权限访问该模型 |
| 404 | 模型不存在 |
| 429 | 请求频率超限 |
| 500 | 服务器内部错误 |
详见 Errors。
代码示例
import { Tabs, TabItem } from ‘@astrojs/starlight/components’;
curl "$BASE_URL/v1/responses" \ -H "Authorization: Bearer $API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "'"$MODEL"'", "input": "用一句话介绍 零派智能API。", "instructions": "你是一个有帮助的助手。", "temperature": 0.7, "max_output_tokens": 1000 }'import osimport requests
base_url = os.environ.get("BASE_URL")api_key = os.environ.get("API_KEY")model = os.environ.get("MODEL", "gpt-4o")
response = requests.post( f"{base_url}/v1/responses", headers={ "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", }, json={ "model": model, "input": "用一句话介绍 零派智能API。", "instructions": "你是一个有帮助的助手。", "temperature": 0.7, "max_output_tokens": 1000, },)
result = response.json()print(result["output"][0]["content"][0]["text"])const baseURL = process.env.BASE_URL;const apiKey = process.env.API_KEY;const model = process.env.MODEL || "gpt-4o";
const response = await fetch(`${baseURL}/v1/responses`, { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify({ model, input: "用一句话介绍 零派智能API。", instructions: "你是一个有帮助的助手。", temperature: 0.7, max_output_tokens: 1000, }),});
const result = await response.json();console.log(result.output[0].content[0].text);package main
import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os")
func main() { baseURL := os.Getenv("BASE_URL") apiKey := os.Getenv("API_KEY") model := os.Getenv("MODEL") if model == "" { model = "gpt-4o" }
payload, _ := json.Marshal(map[string]interface{}{ "model": model, "input": "用一句话介绍 零派智能API。", "instructions": "你是一个有帮助的助手。", "temperature": 0.7, "max_output_tokens": 1000, })
req, _ := http.NewRequest("POST", baseURL+"/v1/responses", bytes.NewReader(payload)) req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result struct { Output []struct { Content []struct { Text string `json:"text"` } `json:"content"` } `json:"output"` } json.Unmarshal(body, &result)
fmt.Println(result.Output[0].Content[0].Text)}