Skip to content

Commit 154bfaa

Browse files
author
ochafik
committed
Refactor chat template validation
1 parent 8c84aef commit 154bfaa

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

common/arg.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
323323
throw std::invalid_argument("error: either --embedding or --reranking can be specified, but not both");
324324
}
325325

326+
if (!params.chat_template.empty() && !common_chat_verify_template(params.chat_template, params.use_jinja)) {
327+
throw std::runtime_error(string_format(
328+
"error: the supplied chat template is not supported: %s%s\n",
329+
params.chat_template.c_str(),
330+
params.use_jinja ? "" : "\nnote: llama.cpp was started without --jinja, we only support commonly used templates"
331+
));
332+
}
333+
326334
return true;
327335
}
328336

@@ -1954,13 +1962,6 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
19541962
"list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
19551963
),
19561964
[](common_params & params, const std::string & value) {
1957-
if (!common_chat_verify_template(value, params.use_jinja)) {
1958-
throw std::runtime_error(string_format(
1959-
"error: the supplied chat template is not supported: %s%s\n",
1960-
value.c_str(),
1961-
params.use_jinja ? "" : "\nnote: llama.cpp does not use jinja parser, we only support commonly used templates"
1962-
));
1963-
}
19641965
params.chat_template = value;
19651966
}
19661967
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
@@ -1977,20 +1978,10 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
19771978
if (!file) {
19781979
throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
19791980
}
1980-
std::string chat_template;
19811981
std::copy(
19821982
std::istreambuf_iterator<char>(file),
19831983
std::istreambuf_iterator<char>(),
1984-
std::back_inserter(chat_template)
1985-
);
1986-
if (!common_chat_verify_template(chat_template, params.use_jinja)) {
1987-
throw std::runtime_error(string_format(
1988-
"error: the supplied chat template is not supported: %s%s\n",
1989-
value.c_str(),
1990-
params.use_jinja ? "" : "\nnote: llama.cpp does not use jinja parser, we only support commonly used templates"
1991-
));
1992-
}
1993-
params.chat_template = chat_template;
1984+
std::back_inserter(params.chat_template));
19941985
}
19951986
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE_FILE"));
19961987
add_opt(common_arg(

0 commit comments

Comments
 (0)