使用 Kimi 的 thinking 和 preserved thinking
Kimi 系列在 OpenAI-compatible Chat Completions 路径上使用 thinking.type 控制当前请求是否思考。可切换 Kimi 模型使用 thinking.keep 控制是否保留历史推理内容;kimi-k2.7-code 的 thinking 和 preserved thinking 均默认开启且不可关闭。Anthropic-compatible 路径属于不同协议形态,不要直接套用本页的 reasoning_content 回传规则。
区分强制思考和可切换模型
先判断目标模型能否关闭思考。强制思考模型不应把关闭思考作为常规业务路径。
kimi-k2.7-code按强制思考和强制 preserved thinking 模型处理。默认请求和thinking.type: "enabled"均可返回可见推理;thinking.type: "disabled"会返回 400。不要把关闭思考或关闭 preserved thinking 作为常规业务路径,也不要依赖enable_thinking改变该模型行为。kimi-k2.6、kimi-k2.5默认启用思考,可显式关闭。简单任务可关闭,复杂任务保持开启。
如果模型 ID 带有明确的 thinking 后缀,先按强制思考模型处理。
用 thinking.type 控制当前请求
当前请求只需要关闭思考时,设置 thinking.type: "disabled"。开启思考时使用 "enabled"。以下 Python 示例使用 OpenAI SDK。
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["API_KEY"],
base_url="https://neogpu.com/maas/v1",
)
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "What is 2 + 2? Give only the final answer."}],
max_tokens=256,
extra_body={"thinking": {"type": "disabled"}},
)如果关闭后仍看到推理字段,先确认目标模型是否属于强制思考模型。
用 curl 验证 thinking.type 请求体
先用 curl 确认可切换 Kimi 模型是否接受 thinking.type: "disabled"。运行前先在当前终端设置 API_KEY 环境变量。以下 curl 命令适用于 bash/zsh 等 POSIX 风格 Shell(macOS/Linux、WSL、Git Bash)。如果使用 Windows PowerShell 或 CMD,请按对应 Shell 的语法调整命令。
curl --request POST \
--url "https://neogpu.com/maas/v1/chat/completions" \
--header "Accept: application/json, text/event-stream" \
--header "Authorization: Bearer $API_KEY" \
--header "Content-Type: application/json" \
--data-raw '{
"model": "kimi-k2.6",
"messages": [
{
"role": "user",
"content": "What is 2 + 2? Give only the final answer."
}
],
"max_tokens": 256,
"thinking": {
"type": "disabled"
}
}'终端录制
用 curl 验证 Kimi thinking.type
适用于验证 Kimi 推理开关字段是否按预期传入。
Kimi K2.7 Code 行为
接入 kimi-k2.7-code 时,按强制思考和强制 preserved thinking 模型处理请求形态。
- 默认请求和
thinking.type: "enabled"均接受,非流式响应读取message.reasoning_content,流式响应读取delta.reasoning_content。 thinking.type: "disabled"会返回 400,错误信息表示该模型的thinking.type必须为"enabled"。- preserved thinking 默认开启且不可关闭。不要用
thinking.keep尝试开启或关闭kimi-k2.7-code的 preserved thinking。 enable_thinking不作为kimi-k2.7-code的思考开关。生产代码优先使用 Kimi 原生thinking参数。- 图片输入可在流式响应中返回推理字段。视频输入需使用服务端可访问的媒体地址;接入前请用业务实际使用的视频地址确认请求。
用 thinking.keep 保留历史推理
对可切换 Kimi 模型,thinking.keep 用于历史保留,不用于开启当前请求思考。需要在工具调用或 agent 流程中保留推理连续性时,和当前请求的 thinking.type 一起设置。
extra_body = {
"thinking": {
"type": "enabled",
"keep": "all",
}
}对可切换 Kimi 模型,只关闭当前请求思考时,设置 thinking.type 即可。thinking.keep 留给需要历史推理连续性的工具调用或 agent 流程。
对 kimi-k2.7-code,preserved thinking 已默认开启。工具调用后的历史回传可在非流式和流式请求中继续使用。保守做法是回传模型原始返回的 assistant 消息,包括 reasoning_content 和 tool_calls,再追加工具结果。
读取 Kimi 的 reasoning_content
Kimi 推理内容通过 reasoning_content 返回。OpenAI SDK 的类型定义可能没有这个字段,应用代码应使用 getattr 或 SDK 提供的扩展字段访问方式。
message = response.choices[0].message
reasoning = getattr(message, "reasoning_content", None)
if reasoning:
print(reasoning)
print(message.content)流式响应中同样读取 delta.reasoning_content。不要假设每个 chunk 都有该字段。
回传发起工具调用的 assistant 消息
当 Kimi 在思考后发起工具调用时,assistant 消息通常同时包含 reasoning_content 和 tool_calls。继续对话时,回传这条 assistant 消息,再追加工具结果。
assistant_message = {
"role": "assistant",
"content": response.choices[0].message.content or "",
"tool_calls": [
{
"id": tool_call.id,
"type": "function",
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments,
},
}
for tool_call in response.choices[0].message.tool_calls
],
}
reasoning = getattr(response.choices[0].message, "reasoning_content", None)
if reasoning:
assistant_message["reasoning_content"] = reasoning
messages.append(assistant_message)
messages.append(
{
"role": "tool",
"tool_call_id": assistant_message["tool_calls"][0]["id"],
"content": '{"weather": "Sunny"}',
}
)如果历史中有一轮关闭了思考,该轮 assistant 没有 reasoning_content 是正常状态。保留真实历史,不要补写,也不要用空字符串或摘要替代模型原始返回。
kimi-k2.7-code 是强制思考模型。接入工具调用时,不要把 tool_choice: "required" 或对象形式的强制工具选择当成稳定前提;如果遇到 tool_choice 在 thinking mode 中不支持的 400 错误,改为省略 tool_choice 或使用 "auto",并在应用侧校验返回的工具名。
分开检查 thinking.type 和 thinking.keep
排查 Kimi 请求时,把当前轮和历史保留分开检查。
- 当前轮是否思考:只看
thinking.type。 - 历史推理是否保留:可切换 Kimi 模型看
thinking.keep和回传的 assistant 消息;kimi-k2.7-code默认保留,重点检查是否原样回传 assistant 消息。 - 工具调用是否能继续:检查
tool_calls、tool_call_id和reasoning_content是否来自同一条原始 assistant 响应。
如果请求返回 400,先移除人工拼接的推理内容,再用模型原始返回的 assistant 消息重试。