Skip to content

Commit 45f9798

Browse files
kaizautybalex
authored andcommitted
Add OpenChat, Alpaca, Vicuna chat templates (ggml-org#6397)
* Add openchat chat template * Add chat template test for openchat * Add chat template for vicuna * Add chat template for orca-vicuna * Add EOS for vicuna templates * Combine vicuna chat templates * Add tests for openchat and vicuna chat templates * Add chat template for alpaca * Add separate template name for vicuna-orca * Remove alpaca, match deepseek with jinja output * Regenerate chat template test with add_generation_prompt * Separate deepseek bos from system message * Match openchat template with jinja output * Remove BOS token from templates, unprefix openchat
1 parent 1df542b commit 45f9798

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

llama.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -15837,6 +15837,55 @@ static int32_t llama_chat_apply_template_internal(
1583715837
ss << message->content << "</s>";
1583815838
}
1583915839
}
15840+
} else if (tmpl == "openchat" || tmpl.find("GPT4 Correct ") != std::string::npos) {
15841+
// openchat/openchat-3.5-0106,
15842+
for (auto message : chat) {
15843+
std::string role(message->role);
15844+
if (role == "system") {
15845+
ss << message->content << "<|end_of_turn|>";
15846+
} else {
15847+
role[0] = toupper(role[0]);
15848+
ss << "GPT4 Correct " << role << ": " << message->content << "<|end_of_turn|>";
15849+
}
15850+
}
15851+
if (add_ass) {
15852+
ss << "GPT4 Correct Assistant:";
15853+
}
15854+
} else if (tmpl == "vicuna" || tmpl == "vicuna-orca" || (tmpl.find("USER: ") != std::string::npos && tmpl.find("ASSISTANT: ") != std::string::npos)) {
15855+
// eachadea/vicuna-13b-1.1 (and Orca variant)
15856+
for (auto message : chat) {
15857+
std::string role(message->role);
15858+
if (role == "system") {
15859+
// Orca-Vicuna variant uses a system prefix
15860+
if (tmpl == "vicuna-orca" || tmpl.find("SYSTEM: ") != std::string::npos) {
15861+
ss << "SYSTEM: " << message->content << "\n";
15862+
} else {
15863+
ss << message->content << "\n\n";
15864+
}
15865+
} else if (role == "user") {
15866+
ss << "USER: " << message->content << "\n";
15867+
} else if (role == "assistant") {
15868+
ss << "ASSISTANT: " << message->content << "</s>\n";
15869+
}
15870+
}
15871+
if (add_ass) {
15872+
ss << "ASSISTANT:";
15873+
}
15874+
} else if (tmpl == "deepseek" || (tmpl.find("### Instruction:") != std::string::npos && tmpl.find("<|EOT|>") != std::string::npos)) {
15875+
// deepseek-ai/deepseek-coder-33b-instruct
15876+
for (auto message : chat) {
15877+
std::string role(message->role);
15878+
if (role == "system") {
15879+
ss << message->content;
15880+
} else if (role == "user") {
15881+
ss << "### Instruction:\n" << message->content << "\n";
15882+
} else if (role == "assistant") {
15883+
ss << "### Response:\n" << message->content << "\n<|EOT|>\n";
15884+
}
15885+
}
15886+
if (add_ass) {
15887+
ss << "### Response:\n";
15888+
}
1584015889
} else {
1584115890
// template not supported
1584215891
return -1;

tests/test-chat-template.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ int main(void) {
3333
"{% 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 %}",
3434
// OrionStarAI/Orion-14B-Chat
3535
"{% for message in messages %}{% if loop.first %}{{ bos_token }}{% endif %}{% if message['role'] == 'user' %}{{ 'Human: ' + message['content'] + '\\n\\nAssistant: ' + eos_token }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token }}{% endif %}{% endfor %}",
36+
// openchat/openchat-3.5-0106
37+
// The included chat_template differs from the author's suggestions here: https://huggingface.co/openchat/openchat_3.5/discussions/5#65448109b4a3f3a2f486fd9d
38+
// So we match against the included template but implement the suggested version.
39+
"{{ bos_token }}{% for message in messages %}{{ 'GPT4 Correct ' + message['role'].title() + ': ' + message['content'] + '<|end_of_turn|>'}}{% endfor %}{% if add_generation_prompt %}{{ 'GPT4 Correct Assistant:' }}{% endif %}",
40+
// deepseek-ai/deepseek-coder-33b-instruct
41+
"{% if not add_generation_prompt is defined %}\n{% set add_generation_prompt = false %}\n{% endif %}\n{%- set ns = namespace(found=false) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'system' -%}\n {%- set ns.found = true -%}\n {%- endif -%}\n{%- endfor -%}\n{{bos_token}}{%- if not ns.found -%}\n{{'You are an AI programming assistant, utilizing the Deepseek Coder model, developed by Deepseek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer\\n'}}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'system' %}\n{{ message['content'] }}\n {%- else %}\n {%- if message['role'] == 'user' %}\n{{'### Instruction:\\n' + message['content'] + '\\n'}}\n {%- else %}\n{{'### Response:\\n' + message['content'] + '\\n<|EOT|>\\n'}}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{% if add_generation_prompt %}\n{{'### Response:'}}\n{% endif %}",
42+
// eachadea/vicuna-13b-1.1
43+
// No template included in tokenizer_config.json, so this template likely needs to be manually set.
44+
"{%- for message in messages %}{%- if message['role'] == 'system' -%}{{- '' + message['content'] + '\n\n' -}}{%- else -%}{%- if message['role'] == 'user' -%}{{-'USER: ' + message['content'] + '\n'-}}{%- else -%}{{-'ASSISTANT: ' + message['content'] + '</s>\n' -}}{%- endif -%}{%- endif -%}{%- endfor -%}{%- if add_generation_prompt -%}{{-'ASSISTANT:'-}}{%- endif -%}",
45+
// Orca-Vicuna
46+
// No template included in tokenizer_config.json, so this template likely needs to be manually set.
47+
"{%- for message in messages %}{%- if message['role'] == 'system' -%}{{-'SYSTEM: ' + message['content'] + '\n' -}}{%- else -%}{%- if message['role'] == 'user' -%}{{-'USER: ' + message['content'] + '\n'-}}{%- else -%}{{-'ASSISTANT: ' + message['content'] + '</s>\n' -}}{%- endif -%}{%- endif -%}{%- endfor -%}{%- if add_generation_prompt -%}{{-'ASSISTANT:'-}}{%- endif -%}",
3648
};
3749
std::vector<std::string> expected_output = {
3850
// teknium/OpenHermes-2.5-Mistral-7B
@@ -49,6 +61,14 @@ int main(void) {
4961
"<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",
5062
// OrionStarAI/Orion-14B-Chat
5163
"Human: You are a helpful assistant\n\nHello\n\nAssistant: </s>Hi there</s>Human: Who are you\n\nAssistant: </s> I am an assistant </s>Human: Another question\n\nAssistant: </s>",
64+
// openchat/openchat-3.5-0106
65+
"You are a helpful assistant<|end_of_turn|>GPT4 Correct User: Hello<|end_of_turn|>GPT4 Correct Assistant: Hi there<|end_of_turn|>GPT4 Correct User: Who are you<|end_of_turn|>GPT4 Correct Assistant: I am an assistant <|end_of_turn|>GPT4 Correct User: Another question<|end_of_turn|>GPT4 Correct Assistant:",
66+
// deepseek-ai/deepseek-coder-33b-instruct
67+
"You are a helpful assistant### Instruction:\nHello\n### Response:\nHi there\n<|EOT|>\n### Instruction:\nWho are you\n### Response:\n I am an assistant \n<|EOT|>\n### Instruction:\nAnother question\n### Response:\n",
68+
// eachadea/vicuna-13b-1.1
69+
"You are a helpful assistant\n\nUSER: Hello\nASSISTANT: Hi there</s>\nUSER: Who are you\nASSISTANT: I am an assistant </s>\nUSER: Another question\nASSISTANT:",
70+
// Orca-Vicuna
71+
"SYSTEM: You are a helpful assistant\nUSER: Hello\nASSISTANT: Hi there</s>\nUSER: Who are you\nASSISTANT: I am an assistant </s>\nUSER: Another question\nASSISTANT:",
5272
};
5373
std::vector<char> formatted_chat(1024);
5474
int32_t res;

0 commit comments

Comments
 (0)