diff --git a/gpttype_adapter.cpp b/gpttype_adapter.cpp index a41cc69da..df9c89c0e 100644 --- a/gpttype_adapter.cpp +++ b/gpttype_adapter.cpp @@ -3858,14 +3858,18 @@ generation_outputs gpttype_generate(const generation_inputs inputs) const std::string channel_open = "<|channel>"; const std::string channel_close = ""; const std::string channel_prefix = channel_open + channel_close; + const std::string systhink = "<|think|>"; const std::string fullbody = addedmemory + kcpp_data->prompt; const bool has_open = fullbody.find(channel_open) != std::string::npos; const bool has_close = fullbody.find(channel_close) != std::string::npos; + const bool has_systhink = fullbody.find(systhink) != std::string::npos; + const bool ends_with_turn = kcpp_string_ends_with(kcpp_rstrip(fullbody),"<|turn>model"); + const bool acceptable_jinja_exception = (ends_with_turn && has_systhink); // If neither opening nor closing tag is present anywhere, prepend both - if (!has_open && !has_close) { + if (!has_open && !has_close && !acceptable_jinja_exception) { addedmemory = channel_prefix + addedmemory; } } diff --git a/otherarch/utils.cpp b/otherarch/utils.cpp index 96b8e733d..a1becb289 100644 --- a/otherarch/utils.cpp +++ b/otherarch/utils.cpp @@ -1076,4 +1076,14 @@ std::vector kcpp_parse_device_list(const std::string & value devices.push_back(nullptr); } return devices; +} + +bool kcpp_string_ends_with(const std::string& str, const std::string& suffix) { + return str.size() >= suffix.size() && + str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; +} + +std::string kcpp_rstrip(const std::string& s) { + size_t end = s.find_last_not_of(" \t\n\r\f\v"); + return (end == std::string::npos) ? "" : s.substr(0, end + 1); } \ No newline at end of file diff --git a/otherarch/utils.h b/otherarch/utils.h index 9b0803810..83fda2451 100644 --- a/otherarch/utils.h +++ b/otherarch/utils.h @@ -75,6 +75,9 @@ bool kcpp_decode_audio_to_f32_stereo_48k(const uint8_t * data, size_t data_size, std::vector kcpp_parse_device_list(const std::string & value); +bool kcpp_string_ends_with(const std::string& str, const std::string& suffix); +std::string kcpp_rstrip(const std::string& s); + //duplcated and modified from llava_embd_batch struct kcpp_embd_batch { std::vector pos;