Responses API 是 OpenAI 較新的有狀態對話 API。TokenLab 以進階可選路徑的方式支援這種格式,適用於相容模型;除非您明確需要 Responses 特定行為,否則請使用 POST /v1/chat/completions 作為預設的相容 OpenAI 路由。
請求主體
由對話組成的輸入項目清單。每個項目可以是:
message: 含有 role 與 content 的對話訊息
function_call: 一個函式呼叫請求
function_call_output: 函式呼叫的輸出
對於多模態輸入,message.content 可以是純字串或內容區塊的陣列。對於支援影像的模型(例如 GPT-5.4 變體),請將影像作為 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 message)。
模型可能呼叫的工具清單。對於使用預設圖片工具模型,或明確設定 model: "gpt-image-2" 的 hosted image_generation 工具,TokenLab 會在轉發請求前移除不支援的 input_fidelity,因為 GPT Image 2 已經以高保真處理圖片輸入。請不要對這個工具傳入 background: "transparent";TokenLab 不會靜默移除它,因為這會改變輸出語義。
文字生成的設定選項。text.format 的行為取決於所選模型與路由路徑;各模型之間不保證一致。
針對支援推理的模型(例如 GPT-5 系列變體)的推理設定。
effort (string): 推理努力等級(low, medium, high)
curl -X POST "https://api.tokenlab.sh/v1/responses" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"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-4o",
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-4o',
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-4o",
"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-4o',
'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.4",
"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.4",
"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": 1706000000,
"model": "gpt-4o",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{"type": "text", "text": "Hello! How can I help you today?"}
]
}
],
"usage": {
"input_tokens": 10,
"output_tokens": 12,
"total_tokens": 22
}
}