Skip to content

Commit 6cf16aa

Browse files
authored
add built in chat template
1 parent b7675ea commit 6cf16aa

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/llama-chat.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7576
llm_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;

src/llama-chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ enum llm_chat_template {
5050
LLM_CHAT_TEMPLATE_HUNYUAN_DENSE,
5151
LLM_CHAT_TEMPLATE_KIMI_K2,
5252
LLM_CHAT_TEMPLATE_SEED_OSS,
53+
LLM_CHAT_TEMPLATE_GROK_2,
5354
LLM_CHAT_TEMPLATE_UNKNOWN,
5455
};
5556

0 commit comments

Comments
 (0)