Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch user passed chat template #1204

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading