@@ -61,6 +61,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
6161 { " megrez" , LLM_CHAT_TEMPLATE_MEGREZ },
6262 { " yandex" , LLM_CHAT_TEMPLATE_YANDEX },
6363 { " bailing" , LLM_CHAT_TEMPLATE_BAILING },
64+ { " llama4" , LLM_CHAT_TEMPLATE_LLAMA4 },
6465};
6566
6667llm_chat_template llm_chat_template_from_str (const std::string & name) {
@@ -174,6 +175,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
174175 return LLM_CHAT_TEMPLATE_YANDEX;
175176 } else if (tmpl_contains (" <role>ASSISTANT</role>" ) && tmpl_contains (" 'HUMAN'" )) {
176177 return LLM_CHAT_TEMPLATE_BAILING;
178+ } else if (tmpl_contains (" <|header_start|>" ) && tmpl_contains (" <|header_end|>" )) {
179+ return LLM_CHAT_TEMPLATE_LLAMA4;
177180 }
178181 return LLM_CHAT_TEMPLATE_UNKNOWN;
179182}
@@ -608,7 +611,16 @@ int32_t llm_chat_apply_template(
608611 if (add_ass) {
609612 ss << " <role>ASSISTANT</role>" ;
610613 }
611- } else {
614+ } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA4) {
615+ // Llama 4
616+ for (auto message : chat) {
617+ std::string role (message->role );
618+ ss << " <|header_start|>" << role << " <|header_end|>\n\n " << trim (message->content ) << " <|eot|>" ;
619+ }
620+ if (add_ass) {
621+ ss << " <|header_start|>assistant<|header_end|>\n\n " ;
622+ }
623+ } else {
612624 // template not supported
613625 return -1 ;
614626 }
0 commit comments