跳到主要內容

聊天補全

POST /v1/chat/completions

核心對話端點,相容 OpenAI Chat Completions 格式。支援多模態輸入、串流輸出、工具呼叫與推理模型。

請求​

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
}'
请求REQUEST
POSThttps://test.kailin.com.cn/v1/chat/completions
响应RESPONSE
填写参数并发送请求,响应会显示在这里

請求主體參數​

參數類型必填說明
modelstring是模型名稱,見 列出模型
messagesarray是對話訊息陣列,見下方訊息結構
streamboolean否串流輸出(SSE),預設 false
temperaturenumber否取樣溫度 0–2,預設 1——越低越穩定
top_pnumber否核心取樣;與 temperature 擇一調整即可
max_tokensnumber否最大生成 token 數(較新的模型改用 max_completion_tokens)
stopstring / array否停止序列,最多 4 個
toolsarray否工具(函式)定義,見工具呼叫
tool_choicestring / object否auto / none / required / 指定工具
response_formatobject否{"type": "json_object"} 或 {"type": "json_schema", ...}
seedinteger否隨機種子,用於重現輸出
frequency_penaltynumber否-2.0–2.0,降低重複
presence_penaltynumber否-2.0–2.0,鼓勵新話題
reasoning_effortstring否推理模型的推理強度:low / medium / high
userstring否終端使用者識別碼,便於風控監控

訊息結構​

欄位類型說明
rolestringsystem / user / assistant / tool
contentstring / array字串,或多模態內容陣列(見多模態輸入)
namestring選填,參與者名稱
tool_callsarrayassistant 訊息中模型發起的工具呼叫
tool_call_idstringtool 訊息必填——對應正在回覆的呼叫

回應(非串流)​

{
"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
}
}
欄位說明
choices[].message.content模型的回覆
choices[].finish_reasonstop 正常結束 / length 達到 token 上限 / tool_calls 需要執行工具
usagetoken 用量——計費依據
usage.completion_tokens_details.reasoning_tokens推理模型回傳的思考 token 數

串流回應​

設定 "stream": true 時,回應是 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]

增量內容在 choices[].delta.content,最後以 data: [DONE] 結束。最後一個 chunk 是否攜帶 usage 取決於上游。

多模態輸入​

把 content 傳成陣列,即可混合文字與圖片/音訊。圖片支援 URL 或 Base64 Data URL:

{
"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"
}
}
]
}
]
}
內容塊類型欄位說明
texttext文字
image_urlimage_url.url圖片 URL 或 data:image/jpeg;base64,...
input_audioinput_audio.data / input_audio.formatBase64 音訊(wav / mp3),需模型支援

工具呼叫​

  1. 透過 tools 宣告工具,模型會以 tool_calls 回應。
  2. 執行工具後,把結果以 role: "tool" 訊息附加,再呼叫一次。
{
"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"]
}
}
}
]
}

模型回應:

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

執行完成後,把結果回傳:

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

使用 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 ?? '');
}
備註

推理類模型(o 系列、DeepSeek-R1 等)的思考過程可能在 reasoning_content 或 delta.reasoning_content 回傳,取決於上游渠道。