Skip to content

Commit a518c11

Browse files
committed
chat template
1 parent edbaaf4 commit a518c11

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/llama-chat.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

6667
llm_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
}

src/llama-chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum llm_chat_template {
4040
LLM_CHAT_TEMPLATE_MEGREZ,
4141
LLM_CHAT_TEMPLATE_YANDEX,
4242
LLM_CHAT_TEMPLATE_BAILING,
43+
LLM_CHAT_TEMPLATE_LLAMA4,
4344
LLM_CHAT_TEMPLATE_UNKNOWN,
4445
};
4546

0 commit comments

Comments
 (0)