Skip to content

Commit

Permalink
Patch user passed chat template (#1204)
Browse files Browse the repository at this point in the history
External chat template is not patched for some reason, but it still can
contain JinjaCpp unsupported constructions.
  • Loading branch information
ilya-lavrenov authored Nov 13, 2024
1 parent 6b8083a commit 164dc78
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/cpp/src/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class Tokenizer::TokenizerImpl {
return std::vector<std::string>(res_data, res_data + res.get_shape()[0]);
}

std::string patch_chat_template(std::string template_str) {
std::string patch_chat_template(std::string template_str) const {
// Replace what jinja2cpp doesn't support
std::pair<std::string, std::string> replace_str_map[] = {
{"'}", "' }"},
Expand Down Expand Up @@ -422,18 +422,16 @@ class Tokenizer::TokenizerImpl {
if (!file.is_open())
return "";

std::string res = "";
std::string res;
ov::genai::utils::read_json_param(nlohmann::json::parse(file), "chat_template", res);
if (res.empty())
return res;

return patch_chat_template(res);
}

std::string apply_chat_template(ChatHistory history,
bool add_generation_prompt,
const std::string& chat_template) const {
auto chat_tpl = chat_template.empty() ? m_chat_template : chat_template;
std::string chat_tpl = chat_template.empty() ? m_chat_template : patch_chat_template(chat_template);
OPENVINO_ASSERT(!chat_tpl.empty(),
"Chat template wasn't found. This may indicate that the model wasn't trained for chat scenario."
" Please add 'chat_template' to tokenizer_config.json to use the model in chat scenario."
Expand Down

0 comments on commit 164dc78

Please sign in to comment.