@@ -1932,11 +1932,19 @@ std::string common_chat_format_example(const common_chat_templates & tmpl, bool
19321932 return common_chat_apply_template (tmpl, msgs, true , use_jinja);
19331933}
19341934
1935+ #define CHATML_TEMPLATE_SRC \
1936+ " {%- for message in messages -%}\n " \
1937+ " {{- '<|im_start|>' + message.role + '\n ' + message.content + '<|im_end|>\n ' -}}\n " \
1938+ " {%- endfor -%}\n " \
1939+ " {%- if add_generation_prompt -%}\n " \
1940+ " {{- '<|im_start|>assistant\n ' -}}\n " \
1941+ " {%- endif -%}"
1942+
19351943common_chat_templates common_chat_templates_from_model (const struct llama_model * model, const std::string & chat_template_override)
19361944{
1937- auto vocab = llama_model_get_vocab (model) ;
1938- std::string default_template_src = chat_template_override ;
1939- std::string template_tool_use_src = chat_template_override;
1945+ std::string default_template_src ;
1946+ std::string template_tool_use_src ;
1947+
19401948 bool has_explicit_template = !chat_template_override.empty ();
19411949 if (chat_template_override.empty ()) {
19421950 auto str = llama_model_chat_template (model, /* name */ nullptr );
@@ -1949,21 +1957,21 @@ common_chat_templates common_chat_templates_from_model(const struct llama_model
19491957 template_tool_use_src = str;
19501958 has_explicit_template = true ;
19511959 }
1960+ } else {
1961+ default_template_src = chat_template_override;
19521962 }
19531963 if (default_template_src.empty () || default_template_src == " chatml" ) {
19541964 if (!template_tool_use_src.empty ()) {
19551965 default_template_src = template_tool_use_src;
19561966 } else {
1957- default_template_src = R"(
1958- {%- for message in messages -%}
1959- {{- "<|im_start|>" + message.role + "\n" + message.content + "<|im_end|>\n" -}}
1960- {%- endfor -%}
1961- {%- if add_generation_prompt -%}
1962- {{- "<|im_start|>assistant\n" -}}
1963- {%- endif -%}
1964- )" ;
1967+ default_template_src = CHATML_TEMPLATE_SRC;
19651968 }
19661969 }
1970+ std::string token_bos;
1971+ std::string token_eos;
1972+ // TODO: update logic that adds BOS and EOS tokens to the tokenized prompt, in favour of the template.
1973+ #if 0
1974+ auto vocab = llama_model_get_vocab(model);
19671975 const auto get_token = [&](llama_token token, const char * name, const char * jinja_variable_name) {
19681976 if (token == LLAMA_TOKEN_NULL) {
19691977 if (default_template_src.find(jinja_variable_name) != std::string::npos
@@ -1975,15 +1983,25 @@ common_chat_templates common_chat_templates_from_model(const struct llama_model
19751983 return common_token_to_piece(vocab, token, true);
19761984 }
19771985 };
1978- auto token_bos = get_token (llama_vocab_bos (vocab), " BOS" , " bos_token" );
1979- auto token_eos = get_token (llama_vocab_eos (vocab), " EOS" , " eos_token" );
1980- return {
1981- has_explicit_template,
1982- std::make_unique<minja::chat_template>(default_template_src, token_bos, token_eos),
1983- template_tool_use_src.empty ()
1984- ? nullptr
1985- : std::make_unique<minja::chat_template>(template_tool_use_src, token_bos, token_eos)
1986- };
1986+ token_bos = get_token(llama_vocab_bos(vocab), "BOS", "bos_token");
1987+ token_eos = get_token(llama_vocab_eos(vocab), "EOS", "eos_token");
1988+ #endif
1989+ try {
1990+ return {
1991+ has_explicit_template,
1992+ std::make_unique<minja::chat_template>(default_template_src, token_bos, token_eos),
1993+ template_tool_use_src.empty ()
1994+ ? nullptr
1995+ : std::make_unique<minja::chat_template>(template_tool_use_src, token_bos, token_eos),
1996+ };
1997+ } catch (const std::exception & e) {
1998+ LOG_ERR (" %s: failed to parse chat template: %s\n " , __func__, e.what ());
1999+ return {
2000+ has_explicit_template,
2001+ std::make_unique<minja::chat_template>(CHATML_TEMPLATE_SRC, token_bos, token_eos),
2002+ nullptr ,
2003+ };
2004+ }
19872005}
19882006
19892007//
0 commit comments