Skip to content

Commit 373ee3f

Browse files
authored
Add Gemma chat template (#5665)
* add gemma chat template * gemma: only apply system_prompt on non-model message
1 parent 4cb4d8b commit 373ee3f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

llama.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12782,6 +12782,28 @@ static int32_t llama_chat_apply_template_internal(
1278212782
if (add_ass) {
1278312783
ss << "<s>assistant\n";
1278412784
}
12785+
} else if (tmpl.find("<start_of_turn>") != std::string::npos) {
12786+
// google/gemma-7b-it
12787+
std::string system_prompt = "";
12788+
for (auto message : chat) {
12789+
std::string role(message->role);
12790+
if (role == "system") {
12791+
// there is no system message for gemma, but we will merge it with user prompt, so nothing is broken
12792+
system_prompt = trim(message->content);
12793+
continue;
12794+
}
12795+
// in gemma, "assistant" is "model"
12796+
role = role == "assistant" ? "model" : message->role;
12797+
ss << "<start_of_turn>" << role << "\n";
12798+
if (!system_prompt.empty() && role != "model") {
12799+
ss << system_prompt << "\n\n";
12800+
system_prompt = "";
12801+
}
12802+
ss << trim(message->content) << "<end_of_turn>\n";
12803+
}
12804+
if (add_ass) {
12805+
ss << "<start_of_turn>model\n";
12806+
}
1278512807
} else {
1278612808
// template not supported
1278712809
return -1;

tests/test-chat-template.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int main(void) {
2929
"{{ bos_token }}{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% elif true == true and not '<<SYS>>' in messages[0]['content'] %}{% set loop_messages = messages %}{% set system_message = 'Vous êtes Vigogne, un assistant IA créé par Zaion Lab. Vous suivez extrêmement bien les instructions. Aidez autant que vous le pouvez.' %}{% else %}{% set loop_messages = messages %}{% set system_message = false %}{% endif %}{% for message in loop_messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if loop.index0 == 0 and system_message != false %}{% set content = '<<SYS>>\\\\n' + system_message + '\\\\n<</SYS>>\\\\n\\\\n' + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + content.strip() + ' [/INST]' }}{% elif message['role'] == 'system' %}{{ '<<SYS>>\\\\n' + content.strip() + '\\\\n<</SYS>>\\\\n\\\\n' }}{% elif message['role'] == 'assistant' %}{{ ' ' + content.strip() + ' ' + eos_token }}{% endif %}{% endfor %}",
3030
// mlabonne/AlphaMonarch-7B
3131
"{% for message in messages %}{{bos_token + message['role'] + '\\n' + message['content'] + eos_token + '\\n'}}{% endfor %}{% if add_generation_prompt %}{{ bos_token + 'assistant\\n' }}{% endif %}",
32+
// google/gemma-7b-it
33+
"{% if messages[0]['role'] == 'system' %}{{ raise_exception('System role not supported') }}{% endif %}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if (message['role'] == 'assistant') %}{% set role = 'model' %}{% else %}{% set role = message['role'] %}{% endif %}{{ '<start_of_turn>' + role + '\\n' + message['content'] | trim + '<end_of_turn>\\n' }}{% endfor %}{% if add_generation_prompt %}{{'<start_of_turn>model\\n'}}{% endif %}",
3234
};
3335
std::vector<std::string> expected_output = {
3436
// teknium/OpenHermes-2.5-Mistral-7B
@@ -41,6 +43,8 @@ int main(void) {
4143
"[INST] <<SYS>>\nYou are a helpful assistant\n<</SYS>>\n\nHello [/INST] Hi there </s>[INST] Who are you [/INST] I am an assistant </s>[INST] Another question [/INST]",
4244
// mlabonne/AlphaMonarch-7B
4345
"system\nYou are a helpful assistant</s>\n<s>user\nHello</s>\n<s>assistant\nHi there</s>\n<s>user\nWho are you</s>\n<s>assistant\n I am an assistant </s>\n<s>user\nAnother question</s>\n<s>assistant\n",
46+
// google/gemma-7b-it
47+
"<start_of_turn>user\nYou are a helpful assistant\n\nHello<end_of_turn>\n<start_of_turn>model\nHi there<end_of_turn>\n<start_of_turn>user\nWho are you<end_of_turn>\n<start_of_turn>model\nI am an assistant<end_of_turn>\n<start_of_turn>user\nAnother question<end_of_turn>\n<start_of_turn>model\n",
4448
};
4549
std::vector<char> formatted_chat(1024);
4650
int32_t res;

0 commit comments

Comments
 (0)