@@ -35,6 +35,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
3535 { " mistral-v3-tekken" , LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN },
3636 { " mistral-v7" , LLM_CHAT_TEMPLATE_MISTRAL_V7 },
3737 { " phi3" , LLM_CHAT_TEMPLATE_PHI_3 },
38+ { " falcon3" , LLM_CHAT_TEMPLATE_FALCON_3 },
3839 { " zephyr" , LLM_CHAT_TEMPLATE_ZEPHYR },
3940 { " monarch" , LLM_CHAT_TEMPLATE_MONARCH },
4041 { " gemma" , LLM_CHAT_TEMPLATE_GEMMA },
@@ -53,6 +54,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
5354 { " rwkv-world" , LLM_CHAT_TEMPLATE_RWKV_WORLD },
5455 { " granite" , LLM_CHAT_TEMPLATE_GRANITE },
5556 { " gigachat" , LLM_CHAT_TEMPLATE_GIGACHAT },
57+ { " megrez" , LLM_CHAT_TEMPLATE_MEGREZ },
5658};
5759
5860llm_chat_template llm_chat_template_from_str (const std::string & name) {
@@ -108,6 +110,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
108110 }
109111 } else if (tmpl_contains (" <|assistant|>" ) && tmpl_contains (" <|end|>" )) {
110112 return LLM_CHAT_TEMPLATE_PHI_3;
113+ } else if (tmpl_contains (" <|assistant|>" ) && tmpl_contains (" <|user|>" )) {
114+ return LLM_CHAT_TEMPLATE_FALCON_3;
111115 } else if (tmpl_contains (" <|user|>" ) && tmpl_contains (" <|endoftext|>" )) {
112116 return LLM_CHAT_TEMPLATE_ZEPHYR;
113117 } else if (tmpl_contains (" bos_token + message['role']" )) {
@@ -154,6 +158,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
154158 return LLM_CHAT_TEMPLATE_GRANITE;
155159 } else if (tmpl_contains (" message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1]" )) {
156160 return LLM_CHAT_TEMPLATE_GIGACHAT;
161+ } else if (tmpl_contains (" <|role_start|>" )) {
162+ return LLM_CHAT_TEMPLATE_MEGREZ;
157163 }
158164 return LLM_CHAT_TEMPLATE_UNKNOWN;
159165}
@@ -260,6 +266,15 @@ int32_t llm_chat_apply_template(
260266 if (add_ass) {
261267 ss << " <|assistant|>\n " ;
262268 }
269+ } else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
270+ // Falcon 3
271+ for (auto message : chat) {
272+ std::string role (message->role );
273+ ss << " <|" << role << " |>\n " << message->content << " \n " ;
274+ }
275+ if (add_ass) {
276+ ss << " <|assistant|>\n " ;
277+ }
263278 } else if (tmpl == LLM_CHAT_TEMPLATE_ZEPHYR) {
264279 // zephyr template
265280 for (auto message : chat) {
@@ -503,6 +518,16 @@ int32_t llm_chat_apply_template(
503518 if (add_ass) {
504519 ss << " assistant<|role_sep|>" ;
505520 }
521+ } else if (tmpl == LLM_CHAT_TEMPLATE_MEGREZ) {
522+ // Megrez template
523+ for (auto message : chat) {
524+ std::string role (message->role );
525+ ss << " <|role_start|>" << role << " <|role_end|>" << message->content << " <|turn_end|>" ;
526+ }
527+
528+ if (add_ass) {
529+ ss << " <|role_start|>assistant<|role_end|>" ;
530+ }
506531 } else {
507532 // template not supported
508533 return -1 ;
0 commit comments