chat: add thinking/reasoning support for Kimi K2

Pass enable_thinking to the Kimi K2 chat template via additional_context
and handle <think> tags at the end of the generated prompt:
- When thinking is disabled, immediately close the tag with </think>
- When thinking is enabled, set thinking_forced_open = true

This allows proper reasoning mode support for Kimi 2.5 / K2 models.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Francesco Menegoni 2026-01-31 19:52:28 +01:00
parent 41ea26144e
commit c7fa677314
1 changed files with 14 additions and 1 deletions

View File

@ -1882,9 +1882,22 @@ static common_chat_params common_chat_params_init_kimi_k2(const common_chat_temp
common_chat_params data;
data.grammar_lazy = params.tools.is_array() && !params.tools.empty() && params.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
data.prompt = apply(tmpl, params);
json additional_context = {
{"thinking", params.enable_thinking},
};
data.prompt = apply(tmpl, params, /* messages_override= */ std::nullopt, /* tools_override= */ std::nullopt, additional_context);
data.format = COMMON_CHAT_FORMAT_KIMI_K2;
// Handle thinking tags based on prompt ending
if (string_ends_with(data.prompt, "<think>\n") || string_ends_with(data.prompt, "<think>")) {
if (!params.enable_thinking) {
data.prompt += "</think>";
} else {
data.thinking_forced_open = true;
}
}
data.preserved_tokens = {
"<think>",
"</think>",