Skip to content

Commit 29d7b45

Browse files
committed
Added template code and test for mistral-v7
1 parent fded997 commit 29d7b45

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/llama.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21884,6 +21884,20 @@ static int32_t llama_chat_apply_template_internal(
2188421884
is_inside_turn = false;
2188521885
}
2188621886
}
21887+
} else if (tmpl == "mistral-v7" || (tmpl_contains("[SYSTEM_PROMPT]") && tmpl_contains("[INST]"))) {
21888+
// See: https://huggingface.co/mistralai/Mistral-Large-Instruct-2411#basic-instruct-template-v7
21889+
for (auto message : chat) {
21890+
std::string role(message->role);
21891+
std::string content(message->content);
21892+
if (role == "system") {
21893+
ss << "[SYSTEM_PROMPT] " << content << "[/SYSTEM_PROMPT]";
21894+
} else if (role == "user") {
21895+
ss << "[INST] " << content << "[/INST]";
21896+
}
21897+
else {
21898+
ss << " " << content << "</s>";
21899+
}
21900+
}
2188721901
} else if (tmpl == "llama2" || tmpl == "mistral" || tmpl_contains("[INST]")) {
2188821902
// llama2 template and its variants
2188921903
// [variant] support system message

tests/test-chat-template.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ int main(void) {
6767
"{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{{ bos_token }}{% for message in messages %}{% if message['role'] == 'user' %}{{ 'User: ' + message['content'] + '\n\n' }}{% elif message['role'] == 'assistant' %}{{ 'Assistant: ' + message['content'] + eos_token }}{% elif message['role'] == 'system' %}{{ message['content'] + '\n\n' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}",
6868
// ibm-granite/granite-3.0-8b-instruct
6969
"{%- if tools %}\n {{- '<|start_of_role|>available_tools<|end_of_role|>\n' }}\n {%- for tool in tools %}\n {{- tool | tojson(indent=4) }}\n {%- if not loop.last %}\n {{- '\n\n' }}\n {%- endif %}\n {%- endfor %}\n {{- '<|end_of_text|>\n' }}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'system' %}\n {{- '<|start_of_role|>system<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'user' %}\n {{- '<|start_of_role|>user<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'assistant' %}\n {{- '<|start_of_role|>assistant<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'assistant_tool_call' %}\n {{- '<|start_of_role|>assistant<|end_of_role|><|tool_call|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'tool_response' %}\n {{- '<|start_of_role|>tool_response<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- endif %}\n {%- if loop.last and add_generation_prompt %}\n {{- '<|start_of_role|>assistant<|end_of_role|>' }}\n {%- endif %}\n{%- endfor %}",
70+
// mistralai/Mistral-Large-Instruct-2411
71+
"{{ bos_token }}{% for message in messages %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + '[/INST]' }}{% elif message['role'] == 'system' %}{{ '[SYSTEM_PROMPT] ' + message['content'] + '[/SYSTEM_PROMPT]' }}{% elif message['role'] == 'assistant' %}{{ ' ' + message['content'] + eos_token }}{% else %}{{ raise_exception('Only user, system and assistant roles are supported!') }}{% endif %}{% endfor %}",
7072
};
7173
std::vector<std::string> expected_output = {
7274
// teknium/OpenHermes-2.5-Mistral-7B
@@ -113,6 +115,8 @@ int main(void) {
113115
u8"You are a helpful assistant\n\nUser: Hello\n\nAssistant: Hi there<|end▁of▁sentence|>User: Who are you\n\nAssistant: I am an assistant <|end▁of▁sentence|>User: Another question\n\nAssistant:",
114116
// ibm-granite/granite-3.0-8b-instruct
115117
"<|start_of_role|>system<|end_of_role|>You are a helpful assistant<|end_of_text|>\n<|start_of_role|>user<|end_of_role|>Hello<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>Hi there<|end_of_text|>\n<|start_of_role|>user<|end_of_role|>Who are you<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|> I am an assistant <|end_of_text|>\n<|start_of_role|>user<|end_of_role|>Another question<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>\n",
118+
// mistralai/Mistral-Large-Instruct-2411
119+
"<s>[INST] Hello[/INST] Hi there</s>[INST] Who are you[/INST] I am an assistant </s>[INST] Another question[/INST]",
116120
};
117121
std::vector<char> formatted_chat(1024);
118122
int32_t res;

0 commit comments

Comments
 (0)