@@ -65,6 +65,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
6565 { " llama4" , LLM_CHAT_TEMPLATE_LLAMA4 },
6666 { " smolvlm" , LLM_CHAT_TEMPLATE_SMOLVLM },
6767 { " hunyuan-moe" , LLM_CHAT_TEMPLATE_HUNYUAN_MOE },
68+ { " kimi-k2" , LLM_CHAT_TEMPLATE_KIMI_K2 },
6869};
6970
7071llm_chat_template llm_chat_template_from_str (const std::string & name) {
@@ -188,6 +189,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
188189 return LLM_CHAT_TEMPLATE_DOTS1;
189190 } else if (tmpl_contains (" <|startoftext|>" ) && tmpl_contains (" <|extra_4|>" )) {
190191 return LLM_CHAT_TEMPLATE_HUNYUAN_MOE;
192+ } else if (tmpl_contains (" <|im_assistant|>assistant<|im_middle|>" )) {
193+ return LLM_CHAT_TEMPLATE_KIMI_K2;
191194 }
192195 return LLM_CHAT_TEMPLATE_UNKNOWN;
193196}
@@ -680,6 +683,22 @@ int32_t llm_chat_apply_template(
680683 ss << " <|startoftext|>" << message->content << " <|extra_0|>" ;
681684 }
682685 }
686+ } else if (tmpl == LLM_CHAT_TEMPLATE_KIMI_K2) {
687+ // moonshotai/Kimi-K2-Instruct
688+ for (auto message : chat) {
689+ std::string role (message->role );
690+ if (role == " system" ) {
691+ ss << " <|im_system|>system<|im_middle|>" ;
692+ } else if (role == " user" ) {
693+ ss << " <|im_user|>user<|im_middle|>" ;
694+ } else if (role == " assistant" ) {
695+ ss << " <|im_assistant|>assistant<|im_middle|>" ;
696+ } else if (role == " tool" ) {
697+ ss << " <|im_system|>tool<|im_middle|>" ;
698+ }
699+
700+ ss << message->content << " <|im_end|>" ;
701+ }
683702 } else {
684703 // template not supported
685704 return -1 ;
0 commit comments