Skip to content

Commit bb6bc4a

Browse files
committed
feature: Add Command R chat template
Signed-off-by: XingXing Qiao <[email protected]>
1 parent d143140 commit bb6bc4a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

llama.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -17784,6 +17784,19 @@ static int32_t llama_chat_apply_template_internal(
1778417784
if (add_ass) {
1778517785
ss << "<|assistant|>\n";
1778617786
}
17787+
} else if (tmpl == "chatglm3" ||
17788+
(tmpl.find("add_generation_prompt") != std::string::npos &&
17789+
tmpl.find("for message in messages") != std::string::npos &&
17790+
tmpl.find("loop.first") != std::string::npos)) {
17791+
// chatglm3-6b
17792+
ss << "[gMASK]" << "sop";
17793+
for (auto message : chat) {
17794+
std::string role(message->role);
17795+
ss << "<|" << role << "|>" << "\n " << message->content;
17796+
}
17797+
if (add_ass) {
17798+
ss << "<|assistant|>";
17799+
}
1778717800
} else {
1778817801
// template not supported
1778917802
return -1;

tests/test-chat-template.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ int main(void) {
5050
// Llama-3
5151
"{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}",
5252
// Phi-3
53-
"{{ bos_token }}{% for message in messages %}{{'<|' + message['role'] + '|>' + ' ' + message['content'] + '<|end|> ' }}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|> ' }}{% else %}{{ eos_token }}{% endif %}"
53+
"{{ bos_token }}{% for message in messages %}{{'<|' + message['role'] + '|>' + ' ' + message['content'] + '<|end|> ' }}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|> ' }}{% else %}{{ eos_token }}{% endif %}",
54+
// ChatGLM3
55+
"{% for message in messages %}{% if loop.first %}[gMASK]sop<|{{ message['role'] }}|>\n {{ message['content'] }}{% else %}<|{{ message['role'] }}|>\n {{ message['content'] }}{% endif %}{% endfor %}{% if add_generation_prompt %}<|assistant|>{% endif %}",
5456
};
5557
std::vector<std::string> expected_output = {
5658
// teknium/OpenHermes-2.5-Mistral-7B
@@ -81,6 +83,8 @@ int main(void) {
8183
"<|start_header_id|>system<|end_header_id|>\n\nYou are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nHello<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\nHi there<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nWho are you<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\nI am an assistant<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nAnother question<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n",
8284
// Phi 3
8385
"<|system|>\nYou are a helpful assistant<|end|>\n<|user|>\nHello<|end|>\n<|assistant|>\nHi there<|end|>\n<|user|>\nWho are you<|end|>\n<|assistant|>\nI am an assistant<|end|>\n<|user|>\nAnother question<|end|>\n<|assistant|>\n",
86+
// ChatGLM3
87+
"[gMASK]sop<|system|>\n You are a helpful assistant<|user|>\n Hello<|assistant|>\n Hi there<|user|>\n Who are you<|assistant|>\n I am an assistant <|user|>\n Another question<|assistant|>",
8488
};
8589
std::vector<char> formatted_chat(1024);
8690
int32_t res;

0 commit comments

Comments
 (0)