Appearance
本页是 Kimi API Chat Completions 端点的完整参考,覆盖请求参数(messages/thinking/tools/stream 等)、响应格式(非流式/流式),以及图片和视频的多模态输入方式。适合需要精细控制 API 调用的开发者。
创建对话补全(Chat Completions)
POST https://api.moonshot.cn/v1/chat/completions为聊天消息创建补全结果。支持标准聊天、Partial Mode、Tool Use 和多模态输入。
请求参数
核心参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 模型 ID,如 kimi-k2.6、moonshot-v1-128k |
messages | array | ✅ | 对话消息列表,每条含 role 和 content |
stream | boolean | 是否流式返回,默认 false | |
max_completion_tokens | integer | 最大输出 token 数,默认 1024 | |
stop | string/array | 停用词,最多 5 个,每个不超过 32 字节 |
messages 格式
role 支持 system、user、assistant。content 支持两种形式:
纯文本:
json
{"role": "user", "content": "你好"}多模态数组(图片/视频):
json
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}},
{"type": "text", "text": "请描述这张图片"}
]
}支持的 type:text、image_url、video_url。图片/视频 URL 支持 base64 编码(data:image/png;base64,...)或文件 ID(ms://<file_id>)。
thinking 参数(kimi-k2.6 / kimi-k2.5 专用)
控制是否启用思考模式:
json
"thinking": {"type": "enabled"} // 默认,启用思考
"thinking": {"type": "disabled"} // 禁用思考保留历史对话的思考内容(Preserved Thinking):
json
"thinking": {"type": "enabled", "keep": "all"}注意:启用 thinking 时,
tool_choice只能是"auto"或"none";工具调用结果中的reasoning_content必须原样保留在下一轮 messages 中。
模型专有参数限制
kimi-k2.6 / kimi-k2.5 系列的以下参数有固定值,设置其他值会报错:
| 参数 | 固定值 |
|---|---|
temperature | thinking 模式: 1.0;非 thinking 模式: 0.6 |
top_p | 0.95 |
n | 1 |
presence_penalty | 0.0 |
frequency_penalty | 0.0 |
建议不要手动设置这些字段,使用默认值。moonshot-v1 系列支持自定义这些参数。
response_format(输出格式控制)
json
{"type": "text"} // 默认,纯文本
{"type": "json_object"} // JSON 模式,保证输出为合法 JSON
{"type": "json_schema", "json_schema": {"name": "...", "schema": {...}}} // 结构化输出tools(工具调用)
json
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}
]详细工具调用流程见工具调用指南。
prompt_cache_key
用于提高缓存命中率,Agent 和 Coding 场景推荐设置:
json
"prompt_cache_key": "session-id-or-task-id"响应格式
非流式响应
json
{
"id": "cmpl-04ea926191a14749b7f2c7a48a68abc6",
"object": "chat.completion",
"created": 1698999496,
"model": "kimi-k2.6",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1=2。",
"reasoning_content": "这是一个简单的加法运算..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 19,
"completion_tokens": 21,
"total_tokens": 40,
"cached_tokens": 10
}
}finish_reason 取值:
"stop":正常结束"length":达到max_completion_tokens限制"tool_calls":模型发起了工具调用
流式响应(stream: true)
每个 chunk 格式:
json
data: {"id":"cmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"你好"},"finish_reason":null}]}最后一个 chunk:
json
data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop","usage":{...}}]}
data: [DONE]常见问题
Q: kimi-k2.6 能设置 temperature 吗?
A: 不能手动设置,模型在 thinking 模式下强制使用 1.0,非 thinking 模式使用 0.6。如需自定义 temperature,改用 moonshot-v1 或 kimi-k2-0905-preview 系列。
Q: 工具调用时 reasoning_content 必须保留吗?
A: 是的,启用 thinking 模式进行多轮工具调用时,每一轮 assistant 消息中的 reasoning_content 必须原样传回,否则会报错。这是 Kimi 思考模式与 OpenAI o1 的一个差异点。
Q: cached_tokens 是什么?
A: 使用了 KV Cache 的 token 数量,命中缓存的 token 不会重复计费,可以用 prompt_cache_key 提高命中率。
POST /v1/chat/completions 是 Kimi API 的核心接口,支持文本对话、多模态输入(图片/视频)、工具调用、流式输出、JSON Mode 和 Partial Mode。本页是完整的参数参考,不同模型的参数支持有差异。
创建对话补全
POST https://api.moonshot.cn/v1/chat/completions请求参数
基础参数(所有模型通用)
| 参数名 | 类型 | 必须 | 说明 |
|---|---|---|---|
model | string | ✅ | 模型 ID,如 kimi-k2.6、moonshot-v1-128k |
messages | array | ✅ | 对话消息列表 |
stream | boolean | 流式输出,默认 false | |
max_completion_tokens | integer | 最大输出 token 数,默认 1024 | |
response_format | object | 输出格式,见下文 | |
tools | array | 工具定义,最多 128 个 | |
stop | string/array | 停用词,最多 5 个,每个不超过 32 字节 | |
stream_options | object | 流式选项,{include_usage: true} 获取 token 统计 | |
prompt_cache_key | string | 缓存键,用于提高 KV 缓存命中率 |
已废弃:
max_tokens已替换为max_completion_tokens,功能相同但使用新名称。
kimi-k2.6 / kimi-k2.5 专有参数
typescript
{
model: "kimi-k2.6",
thinking: {
type: "enabled" | "disabled", // 思考模式开关,默认 enabled
keep: "all" | null, // Preserved Thinking,默认 null
},
// temperature 只能是 1.0(思考)或 0.6(非思考),不要显式设置
}moonshot-v1 系列额外支持的参数
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
temperature | float | 0.0 | 采样温度,范围 [0, 1] |
top_p | float | 1.0 | 核采样,范围 [0, 1] |
n | integer | 1 | 每次请求返回的回复数,最大 5 |
presence_penalty | float | 0 | 存在惩罚,范围 [-2, 2] |
frequency_penalty | float | 0 | 频率惩罚,范围 [-2, 2] |
messages 格式
文本消息
json
{"role": "system", "content": "你是 Kimi。"},
{"role": "user", "content": "你好!"},
{"role": "assistant", "content": "你好!有什么可以帮你的?"}多模态消息(图片/视频)
json
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,..."
}
},
{
"type": "text",
"text": "请描述这张图片。"
}
]
}支持的 content 类型:
| type | 说明 | url 格式 |
|---|---|---|
text | 文本内容 | — |
image_url | 图片(PNG/JPEG/WebP/GIF) | data:image/png;base64,... 或 ms://<file_id> |
video_url | 视频(MP4/MPEG/MOV 等) | data:video/mp4;base64,... 或 ms://<file_id> |
Partial Mode 消息
json
{
"role": "assistant",
"content": "尊敬的用户您好,",
"partial": true
}response_format 参数
| 值 | 说明 |
|---|---|
{"type": "text"} | 默认,纯文本输出 |
{"type": "json_object"} | 强制输出合法 JSON 对象 |
{"type": "json_schema", "json_schema": {...}} | 按指定 JSON Schema 约束输出结构 |
响应格式
非流式响应
json
{
"id": "cmpl-04ea926191a14749b7f2c7a48a68abc6",
"object": "chat.completion",
"created": 1698999496,
"model": "kimi-k2.6",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么可以帮你的?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 19,
"completion_tokens": 21,
"total_tokens": 40,
"cached_tokens": 10
}
}流式响应(SSE)
data: {"id":"cmpl-xxx","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"cmpl-xxx","choices":[{"index":0,"delta":{"content":"你好"},"finish_reason":null}]}
data: {"id":"cmpl-xxx","choices":[{"index":0,"delta":{},"finish_reason":"stop","usage":{"prompt_tokens":19,"completion_tokens":13,"total_tokens":32}}]}
data: [DONE]finish_reason 说明
| 值 | 含义 |
|---|---|
stop | 正常完成 |
length | 触发 max_completion_tokens 限制,输出被截断 |
tool_calls | 模型请求执行工具调用 |
代码示例
基础文本对话
typescript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.MOONSHOT_API_KEY,
baseURL: "https://api.moonshot.cn/v1",
});
const response = await client.chat.completions.create({
model: "kimi-k2.6",
messages: [
{ role: "user", content: "1+1 等于多少?" },
],
});
console.log(response.choices[0].message.content);cURL
bash
curl https://api.moonshot.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MOONSHOT_API_KEY" \
-d '{
"model": "kimi-k2.6",
"messages": [
{"role": "user", "content": "你好!"}
]
}'常见问题
Q: max_tokens 和 max_completion_tokens 有什么区别?
A: 功能相同,max_tokens 是旧名称已废弃,请统一使用 max_completion_tokens。
Q: kimi-k2.6 不支持 n>1 吗?
A: kimi-k2.6/k2.5 不支持 n>1,如需多个回复选项,可以用 moonshot-v1 系列(支持 n 最大 5)。
Q: 如何获取每次请求消耗的 token 数?
A: 非流式请求在响应的 usage 字段直接获取;流式请求需要等最后一个数据块(包含 finish_reason),其中会有 usage 字段,或通过 stream_options: {include_usage: true} 参数额外获取。