文本
创建响应
使用 OpenAI Responses API 格式创建响应
模型详情中的 accepted_request_formats 包含 openai_responses 时,可以使用此接口。请求和响应使用 Responses API 格式,不做格式转换:不带该格式的模型在此返回 400,请改用聊天补全。可选字段及其取值取决于所选模型。
请求体
要使用的模型 ID。可用选项请参见 Models。
输入文本或输入项列表。使用可复用 prompt,或通过 previous_response_id 继续已保存的响应时,可以省略。
每个项是带 type 的对象。常见类型:
message: 带有角色和内容的对话消息function_call: 一个函数调用请求function_call_output: 来自函数调用的输出reasoning: 先前响应里的思考项,原样传回
其他项类型取决于所选模型。
对于多模态输入,message.content 可以是普通字符串,也可以是内容块数组。对于支持图像的模型,请将图像作为 input_image 块传递,而不是将 URL 或 Base64 字符串直接嵌入普通文本中。
示例内容块:
{ "type": "input_text", "text": "Describe this image" }{ "type": "input_image", "image_url": "https://example.com/image.jpg" }{ "type": "input_image", "image_url": "data:image/png;base64,..." }
提供给模型的系统指令(等同于 system 消息)。
生成的最大 token 数量。
采样温度。支持的取值和默认值取决于所选模型。
模型可以调用的工具。支持的类型和组合取决于所选模型。
false如果为 true,则返回事件流。见流式传输。
用于从某个先前响应继续对话的响应 ID。该响应必须属于你的账号且仍处于已保存状态,否则请求返回 404,错误码为 response_not_found。请使用生成该响应的同一模型继续。如果生成该响应的服务暂时不可用,请求可能返回 503。
true是否存储响应,以便后续检索和用作 previous_response_id。设为 false 时,TokenLab 不保存该响应,GET /v1/responses/{id} 也无法返回它。
false请求异步执行,是否支持取决于所选模型。创建调用会立即返回,通常带 status 为 queued 或 in_progress。请用获取响应轮询,直到 status 为 completed、incomplete、failed 或 cancelled。后台处理暂时不可用时,后台请求可能返回 503。
工具选择策略,例如 auto、none、required,或指定某个工具的对象。支持的取值取决于所选模型。
需要额外返回的输出部分,所选模型支持时有效。
流式选项。所选模型支持时,会转发 include_obfuscation。
长对话的截断策略,所选模型支持时有效。
TokenLab 以标准服务档位处理并计费 Responses 请求。接受 auto 和 default。其他取值(如 flex、priority)会从请求中移除,请求按标准价格计费。
可复用提示词模板及变量的引用。
与响应一起保存的自定义信息。
文本输出设置。是否支持 text.format 取决于所选模型。
是否允许并行进行多个工具调用,所选模型支持时有效。
Nucleus 采样参数(0-1)。
推理选项(包括 effort)取决于所选模型。
响应
响应的唯一标识符。
始终为 response。
响应创建时的 Unix 时间戳。
响应状态:queued、in_progress、completed、incomplete、failed 或 cancelled。即使 HTTP 请求成功,也要检查此字段。
模型生成的输出项列表。
token 使用统计。
相关接口
GET /v1/responses/{id}获取已保存的响应。DELETE /v1/responses/{id}删除已保存的响应,不会取消生成。POST /v1/responses/compact压缩对话并返回response.compaction对象。wss://api.tokenlab.sh/v1/responses通过 WebSocket 创建响应。见流式传输。- 目前不提供取消、input-items 或 input-tokens 接口。
请求
curl -X POST "https://api.tokenlab.sh/v1/responses" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6-terra",
"input": [
{"type": "message", "role": "user", "content": "Hello!"}
],
"max_output_tokens": 1000
}'from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.tokenlab.sh/v1"
)
response = client.responses.create(
model="gpt-5.6-terra",
input=[
{"type": "message", "role": "user", "content": "Hello!"}
],
max_output_tokens=1000
)
print(response.output)import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-your-api-key',
baseURL: 'https://api.tokenlab.sh/v1'
});
const response = await client.responses.create({
model: 'gpt-5.6-terra',
input: [
{ type: 'message', role: 'user', content: 'Hello!' }
],
max_output_tokens: 1000
});
console.log(response.output);package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
payload := map[string]interface{}{
"model": "gpt-5.6-terra",
"input": []map[string]interface{}{
{"type": "message", "role": "user", "content": "Hello!"},
},
"max_output_tokens": 1000,
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", "https://api.tokenlab.sh/v1/responses", bytes.NewBuffer(body))
req.Header.Set("Authorization", "Bearer sk-your-api-key")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
fmt.Println(result["output"])
}<?php
$ch = curl_init('https://api.tokenlab.sh/v1/responses');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer sk-your-api-key'
],
CURLOPT_POSTFIELDS => json_encode([
'model' => 'gpt-5.6-terra',
'input' => [
['type' => 'message', 'role' => 'user', 'content' => 'Hello!']
],
'max_output_tokens' => 1000
])
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
print_r($data['output']);视觉输入示例
通过将图像放入 message.content 中作为 input_image 块来使用支持图像的模型。image_url 值可以是公开 URL 或 Base64 数据 URL。
{
"model": "gpt-5.6-terra",
"input": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": "Please describe this image."
},
{
"type": "input_image",
"image_url": "https://example.com/demo.jpg"
}
]
}
]
}{
"model": "gpt-5.6-terra",
"input": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": "Please describe this image."
},
{
"type": "input_image",
"image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..."
}
]
}
]
}响应
{
"id": "resp_abc123",
"object": "response",
"created_at": 1706000000,
"status": "completed",
"model": "gpt-5.6-terra",
"output": [
{
"id": "msg_abc123",
"type": "message",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Hello! How can I help you today?", "annotations": []}
]
}
],
"usage": {
"input_tokens": 10,
"output_tokens": 12,
"total_tokens": 22
}
}错误
错误使用 OpenAI 风格的 error 对象。常见情况:400 表示字段无效或模型不带 Responses 格式,401 表示密钥无效,402 表示余额不足,404 表示 previous_response_id 未知,429 表示触发速率限制,503 表示模型暂时无法处理该请求。见处理 API 错误。
授权
BearerAuth API Key 身份验证。在 Dashboard > API > API Keys 中创建或管理 API Key。
位置: header
请求头
单次请求的交付策略。覆盖 API 密钥和 Workspace 的默认设置。自动优先尝试 TokenLab Verified,并在输出、请求接受或持久资源创建之前,可能会切换一次至仅限 Official。
可选值
- "auto"
- "verified"
- "official"
用于停止同一请求第二次运行的可选键。为每个请求发送一个新的唯一值(例如 UUID),并在超时重试时保留它。使用相同键的重试不会再次运行:它会返回 409 和 idempotency_replay_unavailable,或者在主体发生变化时通常返回 idempotency_conflict。例外:当非流式结果在客户端断开连接后完成时,在 24 小时内使用相同键和主体的重试将返回该结果(最大 1 MB),并带有请求头 Idempotent-Replayed: true 且不进行二次收费。超过 255 个字符的键将被忽略,此时请求不具备幂等性保护。当 background: true 时,使用相同键和主体的重试将返回原始响应对象,而不会创建或收取第二次响应费用;不同的主体将被拒绝,且其状态码不保证。请参阅 使用幂等键安全重试。
1 <= length <= 255请求体
application/json
响应
application/json
application/json
application/json
application/json
application/json