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

Chat Completions

POST /v1/chat/completions

Endpoint hội thoại cốt lõi, tương thích với định dạng OpenAI Chat Completions. Hỗ trợ đầu vào đa phương thức, phát trực tuyến, gọi tool và các mô hình reasoning.

Yêu cầu​

curl https://test.kailin.com.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-token" \
-d '{
"model": "deepseek-v4-1-flash-261017",
"messages": [
{ "role": "system", "content": "You are a concise assistant" },
{ "role": "user", "content": "Hello" }
],
"stream": false
}'
RequestREQUEST
POSThttps://test.kailin.com.cn/v1/chat/completions
ResponseRESPONSE
Fill in the request and hit Send — the response shows up here

Thân yêu cầu​

Tham sốKiểuBắt buộcMô tả
modelstringCóTên mô hình, xem Liệt kê mô hình
messagesarrayCóCác tin nhắn hội thoại, xem cấu trúc tin nhắn ở dưới
streambooleanKhôngXuất dữ liệu dạng stream (SSE), mặc định false
temperaturenumberKhôngNhiệt độ lấy mẫu 0–2, mặc định 1 — càng thấp càng ổn định
top_pnumberKhôngLấy mẫu nucleus; chỉ điều chỉnh tham số này hoặc temperature, không điều chỉnh cả hai
max_tokensnumberKhôngSố token tối đa được sinh ra (các mô hình mới dùng max_completion_tokens)
stopstring / arrayKhôngChuỗi dừng, tối đa 4
toolsarrayKhôngĐịnh nghĩa tool (hàm), xem tool calling
tool_choicestring / objectKhôngauto / none / required / một tool cụ thể
response_formatobjectKhông{"type": "json_object"} hoặc {"type": "json_schema", ...}
seedintegerKhôngSeed ngẫu nhiên để tái lập kết quả
frequency_penaltynumberKhông-2.0–2.0, giảm lặp lại
presence_penaltynumberKhông-2.0–2.0, khuyến khích chủ đề mới
reasoning_effortstringKhôngMức độ suy luận cho các mô hình reasoning: low / medium / high
userstringKhôngĐịnh danh người dùng cuối, hữu ích cho việc giám sát lạm dụng

Cấu trúc tin nhắn​

TrườngKiểuMô tả
rolestringsystem / user / assistant / tool
contentstring / arrayMột chuỗi, hoặc một mảng các phần đa phương thức (xem đầu vào đa phương thức)
namestringTên người tham gia (tùy chọn)
tool_callsarrayCác lệnh gọi tool do mô hình khởi tạo, nằm trong tin nhắn assistant
tool_call_idstringBắt buộc trong tin nhắn tool — lệnh gọi đang được phản hồi

Phản hồi (không stream)​

{
"id": "chatcmpl-B3xK1abc123",
"object": "chat.completion",
"created": 1730000000,
"model": "deepseek-v4-1-flash-261017",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 12,
"total_tokens": 32
}
}
TrườngMô tả
choices[].message.contentCâu trả lời của mô hình
choices[].finish_reasonstop hoàn tất / length chạm giới hạn token / tool_calls cần thực thi tool
usageLượng token sử dụng — cơ sở tính phí
usage.completion_tokens_details.reasoning_tokensSố token suy nghĩ trả về từ các mô hình reasoning

Phản hồi streaming​

Khi đặt "stream": true, phản hồi là một luồng sự kiện SSE:

data: {"id":"chatcmpl-xxx","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

Nội dung tăng dần nằm trong choices[].delta.content, kết thúc bằng data: [DONE]. Chunk cuối cùng có thể kèm usage tùy vào phía upstream.

Đầu vào đa phương thức​

Truyền content dưới dạng mảng để trộn văn bản với hình ảnh/âm thanh. Hình ảnh chấp nhận một URL hoặc một data URL Base64:

{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "What's in this image?" },
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg"
}
}
]
}
]
}
Loại phần nội dungCác trườngMô tả
texttextVăn bản
image_urlimage_url.urlURL hình ảnh hoặc data:image/jpeg;base64,...
input_audioinput_audio.data / input_audio.formatÂm thanh Base64 (wav / mp3), cần mô hình có hỗ trợ

Tool Calling​

  1. Khai báo các tool qua tools; mô hình phản hồi bằng tool_calls.
  2. Thực thi các tool, thêm kết quả vào dưới dạng tin nhắn role: "tool", rồi gọi lại.
{
"model": "deepseek-v4-1-flash-261017",
"messages": [{ "role": "user", "content": "What's the weather in Shanghai today?" }],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name" }
},
"required": ["city"]
}
}
}
]
}

Phản hồi của mô hình:

{
"choices": [
{
"message": {
"role": "assistant",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Shanghai\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}

Sau khi thực thi xong, gửi kết quả trở lại:

{
"model": "deepseek-v4-1-flash-261017",
"messages": [
{ "role": "user", "content": "What's the weather in Shanghai today?" },
{
"role": "assistant",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\": \"Shanghai\"}" }
}
]
},
{ "role": "tool", "tool_call_id": "call_abc123", "content": "Sunny, 26°C" }
]
}

Sử dụng SDK​

from openai import OpenAI

client = OpenAI(
api_key="sk-your-token",
base_url="https://test.kailin.com.cn/v1",
)

stream = client.chat.completions.create(
model="deepseek-v4-1-flash-261017",
messages=[{"role": "user", "content": "Write a short poem about autumn"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="", flush=True)
import OpenAI from 'openai';

const client = new OpenAI({
apiKey: 'sk-your-token',
baseURL: 'https://test.kailin.com.cn/v1',
});

const stream = await client.chat.completions.create({
model: 'deepseek-v4-1-flash-261017',
messages: [{ role: 'user', content: 'Write a short poem about autumn' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}
Ghi chú

Các mô hình reasoning (o-series, DeepSeek-R1, v.v.) có thể trả về phần suy nghĩ trong reasoning_content hoặc delta.reasoning_content, tùy vào kênh upstream.