跳到主要內容

Responses API

OpenAI 的新一代有狀態端點,內建工具、網頁搜尋、檔案搜尋與多步驟任務支援——逐步取代 Chat Completions。

建立 Response​

POST /v1/responses
请求REQUEST
POSThttps://test.kailin.com.cn/v1/responses
响应RESPONSE
填写参数并发送请求,响应会显示在这里

請求​

curl https://test.kailin.com.cn/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-token" \
-d '{
"model": "gpt-4o-mini",
"input": "Describe the Responses API in one sentence"
}'

請求主體參數​

參數類型必填說明
modelstring是模型名稱
inputstring / array是輸入文字,或訊息/內容塊陣列
streamboolean否串流輸出(事件類型更豐富)
instructionsstring否系統層級指令
previous_response_idstring否傳入上一次的 Response ID 即可延續多輪對話,無需重送歷史
toolsarray否工具:function、web_search_preview 等
temperature / top_pnumber否取樣參數
max_output_tokensnumber否最大輸出 token 數
metadataobject否自訂鍵值組(最多 16 組)
storeboolean否是否保存結果,供 previous_response_id 引用

input 為陣列的範例(含圖片):

{
"model": "gpt-4o-mini",
"input": [
{
"role": "user",
"content": [
{ "type": "input_text", "text": "Describe this image" },
{ "type": "input_image", "image_url": "https://example.com/cat.jpg" }
]
}
]
}

回應​

{
"id": "resp_abc123",
"object": "response",
"created_at": 1730000000,
"status": "completed",
"model": "gpt-4o-mini",
"output": [
{
"type": "message",
"id": "msg_abc123",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "The Responses API is OpenAI's next-generation stateful endpoint…",
"annotations": []
}
]
}
],
"usage": {
"input_tokens": 12,
"output_tokens": 30,
"total_tokens": 42
}
}

要取出文字,可迭代 output 中 type: "message" 的項目並讀取 content[].text;SDK 也提供 resp.output_text 捷徑屬性。

查詢 Response​

GET /v1/responses/{response_id}
curl https://test.kailin.com.cn/v1/responses/resp_abc123 \
-H "Authorization: Bearer sk-your-token"

回傳完整的 Response 物件。以 store: false 建立的回應無法查詢。

壓縮上下文​

POST /v1/responses/compact

把長對話壓縮成摘要——適合長時間執行的 Agent,可減少後續請求的 token 消耗:

curl https://test.kailin.com.cn/v1/responses/compact \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-token" \
-d '{
"model": "gpt-4o-mini",
"input": [ ...long message array... ]
}'
參數類型必填說明
modelstring是用於壓縮的模型
inputarray是要壓縮的訊息陣列
instructionsstring否摘要的自訂指令

回傳壓縮後的摘要物件,可放進下一次請求的 input。

備註

WebSocket 存取方式(wss://.../v1/responses 搭配 response.create 事件)取決於閘道對 WS 的轉傳支援——REST + SSE 仍是用戶端整合的主要路徑,此處不另說明。