Chuyển đến nội dung chính

Responses API

Endpoint có trạng thái (stateful) thế hệ mới của OpenAI, hỗ trợ sẵn tools, tìm kiếm web, tìm kiếm tệp và các tác vụ nhiều bước — đang dần thay thế Chat Completions.

Tạo một Response​

POST /v1/responses
RequestREQUEST
POSThttps://test.kailin.com.cn/v1/responses
ResponseRESPONSE
Fill in the request and hit Send — the response shows up here

Yêu cầu​

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"
}'

Thân yêu cầu​

Tham sốKiểuBắt buộcMô tả
modelstringCóTên mô hình
inputstring / arrayCóVăn bản đầu vào, hoặc một mảng các tin nhắn/phần nội dung
streambooleanKhôngXuất dữ liệu dạng stream (nhiều loại sự kiện hơn)
instructionsstringKhôngChỉ dẫn ở cấp hệ thống
previous_response_idstringKhôngTruyền ID của Response trước đó để duy trì hội thoại nhiều lượt mà không cần gửi lại toàn bộ lịch sử
toolsarrayKhôngTools: function, web_search_preview, v.v.
temperature / top_pnumberKhôngCác tham số lấy mẫu
max_output_tokensnumberKhôngSố token đầu ra tối đa
metadataobjectKhôngCác cặp key-value tùy chỉnh (tối đa 16)
storebooleanKhôngCó lưu kết quả lại để tham chiếu qua previous_response_id hay không

Ví dụ với input dạng mảng (bao gồm cả hình ảnh):

{
"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" }
]
}
]
}

Phản hồi​

{
"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
}
}

Để trích xuất văn bản, hãy duyệt qua các phần tử output có type: "message" và đọc content[].text; SDK cũng cung cấp lối tắt resp.output_text.

Truy vấn một Response​

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

Trả về đối tượng Response đầy đủ. Các Response được tạo với store: false không thể truy vấn lại.

Nén ngữ cảnh (Compact Context)​

POST /v1/responses/compact

Nén một hội thoại dài thành bản tóm tắt — hữu ích cho các agent chạy dài nhằm giảm lượng token sử dụng trong các yêu cầu tiếp theo:

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... ]
}'
Tham sốKiểuBắt buộcMô tả
modelstringCóMô hình dùng để nén
inputarrayCóMảng tin nhắn cần nén
instructionsstringKhôngChỉ dẫn tùy chỉnh cho bản tóm tắt

Trả về đối tượng bản tóm tắt đã nén, có thể đặt vào input của yêu cầu tiếp theo.

Ghi chú

Việc truy cập qua WebSocket (wss://.../v1/responses với các sự kiện response.create) phụ thuộc vào khả năng hỗ trợ chuyển tiếp WS của gateway — REST + SSE vẫn là đường tích hợp client chính và không được đề cập ở đây.