跳到主要内容

聊天补全

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-你的令牌" \
-d '{
"model": "deepseek-v4-1-flash-261017",
"messages": [
{ "role": "system", "content": "你是一个简洁的助手" },
{ "role": "user", "content": "你好" }
],
"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否终端用户标识,便于风控

messages 消息结构​

字段类型说明
rolestringsystem / user / assistant / tool
contentstring / array字符串,或多模态内容数组(见多模态输入)
namestring可选,参与者名称
tool_callsarrayassistant 消息中模型发起的工具调用
tool_call_idstringtool 角色消息必填,对应被响应的调用 ID

响应(非流式)​

{
"id": "chatcmpl-B3xK1abc123",
"object": "chat.completion",
"created": 1730000000,
"model": "deepseek-v4-1-flash-261017",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么可以帮你的吗?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 12,
"total_tokens": 32
}
}
字段说明
choices[].message.content模型回复文本
choices[].finish_reasonstop 正常结束 / length 达到上限 / 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":"你好"},"finish_reason":null}]}

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

data: [DONE]

增量内容在 choices[].delta.content,最后以 data: [DONE] 结束。最后一个 chunk 的 usage 字段可能携带 token 用量(取决于上游)。

多模态输入​

content 传数组即可混合文本与图片/音频。图片支持 URL 或 Base64 Data URL:

{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "这张图里有什么?" },
{
"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": "上海今天天气怎么样?" }],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市当前天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名" }
},
"required": ["city"]
}
}
}
]
}

模型返回:

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

执行后把结果回传:

{
"model": "deepseek-v4-1-flash-261017",
"messages": [
{ "role": "user", "content": "上海今天天气怎么样?" },
{
"role": "assistant",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\": \"上海\"}" }
}
]
},
{ "role": "tool", "tool_call_id": "call_abc123", "content": "晴,26°C" }
]
}

使用 SDK​

from openai import OpenAI

client = OpenAI(
api_key="sk-你的令牌",
base_url="https://test.kailin.com.cn/v1",
)

stream = client.chat.completions.create(
model="deepseek-v4-1-flash-261017",
messages=[{"role": "user", "content": "写一首关于秋天的短诗"}],
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-你的令牌',
baseURL: 'https://test.kailin.com.cn/v1',
});

const stream = await client.chat.completions.create({
model: 'deepseek-v4-1-flash-261017',
messages: [{ role: 'user', content: '写一首关于秋天的短诗' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}
备注

推理类模型(如 o 系列、DeepSeek-R1)的思考过程可能在 reasoning_content 或 delta.reasoning_content 字段返回,具体取决于上游渠道。