@@ -70,6 +70,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
7070 { " hunyuan-dense" , LLM_CHAT_TEMPLATE_HUNYUAN_DENSE },
7171 { " kimi-k2" , LLM_CHAT_TEMPLATE_KIMI_K2 },
7272 { " seed_oss" , LLM_CHAT_TEMPLATE_SEED_OSS },
73+ { " grok-2" , LLM_CHAT_TEMPLATE_GROK_2 },
7374};
7475
7576llm_chat_template llm_chat_template_from_str (const std::string & name) {
@@ -204,6 +205,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
204205 return LLM_CHAT_TEMPLATE_KIMI_K2;
205206 } else if (tmpl_contains (" <seed:bos>" )) {
206207 return LLM_CHAT_TEMPLATE_SEED_OSS;
208+ } else if (tmpl_contains (" 'Assistant: ' + message['content'] + '<|separator|>" )) {
209+ return LLM_CHAT_TEMPLATE_GROK_2;
207210 }
208211 return LLM_CHAT_TEMPLATE_UNKNOWN;
209212}
@@ -763,6 +766,20 @@ int32_t llm_chat_apply_template(
763766 if (add_ass) {
764767 ss << " <seed:bos>assistant\n " ;
765768 }
769+ } else if (tmpl == LLM_CHAT_TEMPLATE_GROK_2) {
770+ for (auto message : chat) {
771+ std::string role (message->role );
772+ if (role == " system" ) {
773+ ss << " System: " << trim (message->content ) << " <|separator|>\n\n " ;
774+ } else if (role == " user" ) {
775+ ss << " Human: " << trim (message->content ) << " <|separator|>\n\n " ;
776+ } else if (role == " assistant" ) {
777+ ss << " Assistant: " << message->content << " <|separator|>\n\n " ;
778+ }
779+ }
780+ if (add_ass) {
781+ ss << " Assistant:" ;
782+ }
766783 } else {
767784 // template not supported
768785 return -1 ;
0 commit comments