聊天補全
核心對話端點,相容 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
POST
https://test.kailin.com.cn/v1/chat/completions响应RESPONSE
填写参数并发送请求,响应会显示在这里
請求主體參數
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 模型名稱,見 列出模型 |
messages | array | 是 | 對話訊息陣列,見下方訊息結構 |
stream | boolean | 否 | 串流輸出(SSE),預設 false |
temperature | number | 否 | 取樣溫度 0–2,預設 1——越低越穩定 |
top_p | number | 否 | 核心取樣;與 temperature 擇一調整即可 |
max_tokens | number | 否 | 最大生成 token 數(較新的模型改用 max_completion_tokens) |
stop | string / array | 否 | 停止序列,最多 4 個 |
tools | array | 否 | 工具(函式)定義,見工具呼叫 |
tool_choice | string / object | 否 | auto / none / required / 指定工具 |
response_format | object | 否 | {"type": "json_object"} 或 {"type": "json_schema", ...} |
seed | integer | 否 | 隨機種子,用於重現輸出 |
frequency_penalty | number | 否 | -2.0–2.0,降低重複 |
presence_penalty | number | 否 | -2.0–2.0,鼓勵新話題 |
reasoning_effort | string | 否 | 推理模型的推理強度:low / medium / high |
user | string | 否 | 終端使用者識別碼,便於風控監控 |
訊息結構
| 欄位 | 類型 | 說明 |
|---|---|---|
role | string | system / user / assistant / tool |
content | string / array | 字串,或多模態內容陣列(見多模態輸入) |
name | string | 選填,參與者名稱 |
tool_calls | array | assistant 訊息中模型發起的工具呼叫 |
tool_call_id | string | tool 訊息必填——對應正在回覆的呼叫 |
回應(非串流)
{
"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_reason | stop 正常結束 / length 達到 token 上限 / tool_calls 需要執行工具 |
usage | token 用量——計費依據 |
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"
}
}
]
}
]
}
| 內容塊類型 | 欄位 | 說明 |
|---|---|---|
text | text | 文字 |
image_url | image_url.url | 圖 片 URL 或 data:image/jpeg;base64,... |
input_audio | input_audio.data / input_audio.format | Base64 音訊(wav / mp3),需模型支援 |
工具呼叫
- 透過
tools宣告工具,模型會以tool_calls回應。 - 執行工具後,把結果以
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 回傳,取決於上游渠道。