From e8d5a09b4868372d024bcd90638f8be50e33fdb1 Mon Sep 17 00:00:00 2001 From: yatarkan Date: Fri, 19 Jul 2024 15:16:28 +0400 Subject: [PATCH] Replace python style slice with method --- src/cpp/src/tokenizer.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/cpp/src/tokenizer.cpp b/src/cpp/src/tokenizer.cpp index 9b4a206a1e..7cc83fda10 100644 --- a/src/cpp/src/tokenizer.cpp +++ b/src/cpp/src/tokenizer.cpp @@ -347,14 +347,21 @@ class Tokenizer::TokenizerImpl { std::string replacement_string = "{% if false %}{% set placeholder = false %}"; std::string system_message = ""; - size_t pos = chat_tpl.find(header_with_slice); - if (pos != std::string::npos) { - chat_tpl.replace(pos, header_with_slice.length(), replacement_string); - - if (!history.empty() && history[0].at("role") == "system") { - system_message = history[0].at("content"); - history.erase(history.begin()); - } + // size_t pos = chat_tpl.find(header_with_slice); + // if (pos != std::string::npos) { + // chat_tpl.replace(pos, header_with_slice.length(), replacement_string); + + // if (!history.empty() && history[0].at("role") == "system") { + // system_message = history[0].at("content"); + // history.erase(history.begin()); + // } + // } + + std::string slice_string = "messages[1:]"; + std::string replacement_slice_string = "messages.slice(1)"; + size_t slice_pos = chat_tpl.find(slice_string); + if (slice_pos != std::string::npos) { + chat_tpl.replace(slice_pos, slice_string.length(), replacement_slice_string); } // Jinja2Cpp accepts system_message only as a string and incorrectly handles it as a bool. @@ -398,7 +405,7 @@ class Tokenizer::TokenizerImpl { try { return tpl.RenderAsString(params).value(); - } catch (const std::bad_alloc& error) { + } catch (const std::exception& error) { OPENVINO_THROW("Chat template for the current model is not supported by Jinja2Cpp. " "Please apply template manually to your prompt before calling generate. " "For exmaple: user{user_prompt}model");