From 825a78abaa972262e2fe408e862335283e033120 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Wed, 24 Apr 2024 20:54:19 +0530 Subject: [PATCH] ChatOn: ChatTemplateApplySingle[Ex] return parts detail Now there is a simple and extended version of returning tagged message wrt a single role and its content. The extended version returns the tagged string, as well as the details of the parts that make up that tagged message interms of the type of parts and the lengths of the parts. --- common/chaton.hpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/common/chaton.hpp b/common/chaton.hpp index 1cccee95b..431aa0980 100644 --- a/common/chaton.hpp +++ b/common/chaton.hpp @@ -169,8 +169,14 @@ inline bool chaton_meta_load(std::string &fname) { // Return user-prefix + msg + user-suffix -// NOTE: This currently doesnt return about which parts of the tagged message contain tags and which parts the user message -inline std::string chaton_tmpl_apply_single(const std::string &tmpl, const std::string &role, const std::string &content) { +inline bool chaton_tmpl_apply_single_ex( + const std::string &tmpl, + const std::string &role, + const std::string &content, + std::string &tagged, + std::string &types, + std::vector &lens + ) { ChatParts cp = {}; std::stringstream ss; std::string begin = ""; @@ -187,14 +193,25 @@ inline std::string chaton_tmpl_apply_single(const std::string &tmpl, const std:: cp.add_part(ChatParts::S, suffix); cp.dump(); ss << begin << prefix << content << suffix; - std::string taggedStr = ss.str(); + tagged = ss.str(); std::string cpStr = cp.str(); - if (taggedStr != cpStr) { - LOG_TEELN("DBUG:%s:Mismatch between CP[%s] and SS[%s]", __func__, cpStr.c_str(), taggedStr.c_str()); + if (tagged != cpStr) { + LOG_TEELN("DBUG:%s:Mismatch between CP[%s] and SS[%s]", __func__, cpStr.c_str(), tagged.c_str()); exit(2); } - LOGLN("DBUG:%s:%s:%s:%s", __func__, tmpl.c_str(), role.c_str(), taggedStr.c_str()); - return taggedStr; + LOGLN("DBUG:%s:%s:%s:%s", __func__, tmpl.c_str(), role.c_str(), tagged.c_str()); + types = cp.get_types(); + lens = cp.get_partslens(); + return true; +} + +// Return user-prefix + msg + user-suffix, types string and lens vector wrt the parts that make up the returned string +inline std::string chaton_tmpl_apply_single(const std::string &tmpl, const std::string &role, const std::string &content) { + std::string tagged; + std::string types; + std::vector lens; + chaton_tmpl_apply_single_ex(tmpl, role, content, tagged, types, lens); + return tagged; } // global-begin + [role-prefix + msg + role-suffix] + global-end