Skip to content

Commit 3647039

Browse files
ngxsonmglambda
authored andcommitted
llama-chat : add phi 4 template (ggml-org#11148)
1 parent 98c97b2 commit 3647039

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/llama-chat.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
3535
{ "mistral-v3-tekken", LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN },
3636
{ "mistral-v7", LLM_CHAT_TEMPLATE_MISTRAL_V7 },
3737
{ "phi3", LLM_CHAT_TEMPLATE_PHI_3 },
38+
{ "phi4", LLM_CHAT_TEMPLATE_PHI_4 },
3839
{ "falcon3", LLM_CHAT_TEMPLATE_FALCON_3 },
3940
{ "zephyr", LLM_CHAT_TEMPLATE_ZEPHYR },
4041
{ "monarch", LLM_CHAT_TEMPLATE_MONARCH },
@@ -73,7 +74,9 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
7374
return tmpl.find(haystack) != std::string::npos;
7475
};
7576
if (tmpl_contains("<|im_start|>")) {
76-
return LLM_CHAT_TEMPLATE_CHATML;
77+
return tmpl_contains("<|im_sep|>")
78+
? LLM_CHAT_TEMPLATE_PHI_4
79+
: LLM_CHAT_TEMPLATE_CHATML;
7780
} else if (tmpl.find("mistral") == 0 || tmpl_contains("[INST]")) {
7881
if (tmpl_contains("[SYSTEM_PROMPT]")) {
7982
return LLM_CHAT_TEMPLATE_MISTRAL_V7;
@@ -269,6 +272,14 @@ int32_t llm_chat_apply_template(
269272
if (add_ass) {
270273
ss << "<|assistant|>\n";
271274
}
275+
} else if (tmpl == LLM_CHAT_TEMPLATE_PHI_4) {
276+
// chatml template
277+
for (auto message : chat) {
278+
ss << "<|im_start|>" << message->role << "<|im_sep|>" << message->content << "<|im_end|>";
279+
}
280+
if (add_ass) {
281+
ss << "<|im_start|>assistant<|im_sep|>";
282+
}
272283
} else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
273284
// Falcon 3
274285
for (auto message : chat) {

src/llama-chat.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum llm_chat_template {
1515
LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN,
1616
LLM_CHAT_TEMPLATE_MISTRAL_V7,
1717
LLM_CHAT_TEMPLATE_PHI_3,
18+
LLM_CHAT_TEMPLATE_PHI_4,
1819
LLM_CHAT_TEMPLATE_FALCON_3,
1920
LLM_CHAT_TEMPLATE_ZEPHYR,
2021
LLM_CHAT_TEMPLATE_MONARCH,

tests/test-chat-template.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ int main(void) {
7878
// ai-sage/GigaChat-20B-A3B-instruct
7979
"{% if messages[0]['role'] == 'system' -%}\n {%- set loop_messages = messages[1:] -%}\n {%- set system_message = bos_token + messages[0]['content'] + additional_special_tokens[1] -%}\n{%- else -%}\n {%- set loop_messages = messages -%}\n {%- set system_message = bos_token + '' -%}\n{%- endif -%}\n{%- for message in loop_messages %}\n {% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}\n {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}\n {% endif %}\n \n {%- if loop.index0 == 0 -%}\n {{ system_message -}}\n {%- endif -%}\n {%- if message['role'] == 'user' -%}\n {{ message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1] -}}\n {{ 'available functions' + additional_special_tokens[0] + additional_special_tokens[2] + additional_special_tokens[3] + additional_special_tokens[1] -}}\n {%- endif -%}\n {%- if message['role'] == 'assistant' -%}\n {{ message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1] -}}\n {%- endif -%}\n {%- if loop.last and add_generation_prompt -%}\n {{ 'assistant' + additional_special_tokens[0] -}}\n {%- endif -%}\n{%- endfor %}",
8080
// Infinigence/Megrez-3B-Instruct
81-
u8"{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|role_start|>system<|role_end|>你是Megrez-3B-Instruct,将针对用户的问题给出详细的、积极的回答。<|turn_end|>' }}{% endif %}{{ '<|role_start|>' + message['role'] + '<|role_end|>' + message['content'] + '<|turn_end|>' }}{% endfor %}{% if add_generation_prompt %}{{ '<|role_start|>assistant<|role_end|>' }}{% endif %}"
81+
u8"{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|role_start|>system<|role_end|>你是Megrez-3B-Instruct,将针对用户的问题给出详细的、积极的回答。<|turn_end|>' }}{% endif %}{{ '<|role_start|>' + message['role'] + '<|role_end|>' + message['content'] + '<|turn_end|>' }}{% endfor %}{% if add_generation_prompt %}{{ '<|role_start|>assistant<|role_end|>' }}{% endif %}",
82+
// phi-4
83+
"{% for message in messages %}{% if (message['role'] == 'system') %}{{'<|im_start|>system<|im_sep|>' + message['content'] + '<|im_end|>'}}{% elif (message['role'] == 'user') %}{{'<|im_start|>user<|im_sep|>' + message['content'] + '<|im_end|><|im_start|>assistant<|im_sep|>'}}{% elif (message['role'] == 'assistant') %}{{message['content'] + '<|im_end|>'}}{% endif %}{% endfor %}",
8284
};
8385
std::vector<std::string> expected_output = {
8486
// teknium/OpenHermes-2.5-Mistral-7B
@@ -137,6 +139,8 @@ int main(void) {
137139
"<s>You are a helpful assistant<|message_sep|>user<|role_sep|>Hello<|message_sep|>available functions<|role_sep|>[]<|message_sep|>assistant<|role_sep|>Hi there<|message_sep|>user<|role_sep|>Who are you<|message_sep|>available functions<|role_sep|>[]<|message_sep|>assistant<|role_sep|> I am an assistant <|message_sep|>user<|role_sep|>Another question<|message_sep|>available functions<|role_sep|>[]<|message_sep|>assistant<|role_sep|>",
138140
// Infinigence/Megrez-3B-Instruct
139141
"<|role_start|>system<|role_end|>You are a helpful assistant<|turn_end|><|role_start|>user<|role_end|>Hello<|turn_end|><|role_start|>assistant<|role_end|>Hi there<|turn_end|><|role_start|>user<|role_end|>Who are you<|turn_end|><|role_start|>assistant<|role_end|> I am an assistant <|turn_end|><|role_start|>user<|role_end|>Another question<|turn_end|><|role_start|>assistant<|role_end|>",
142+
// phi-4
143+
"<|im_start|>system<|im_sep|>You are a helpful assistant<|im_end|><|im_start|>user<|im_sep|>Hello<|im_end|><|im_start|>assistant<|im_sep|>Hi there<|im_end|><|im_start|>user<|im_sep|>Who are you<|im_end|><|im_start|>assistant<|im_sep|> I am an assistant <|im_end|><|im_start|>user<|im_sep|>Another question<|im_end|><|im_start|>assistant<|im_sep|>",
140144
};
141145
std::vector<char> formatted_chat(1024);
142146
int32_t res;

0 commit comments

Comments
 (0)