ChatON: Test ChatParts in chat-template-apply

This commit is contained in:
HanishKVC 2024-04-23 22:22:06 +05:30
parent 9de1d6017f
commit d1899728aa

View file

@ -188,8 +188,10 @@ inline std::string chaton_tmpl_apply_single(const std::string &tmpl, const std::
// then 1st user message will have user-prefix only if systemuser-1st-user-has-prefix is true // then 1st user message will have user-prefix only if systemuser-1st-user-has-prefix is true
// NOTE: This currently doesnt return about which parts of the tagged message contain tags and which parts the user message // 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(const std::string &tmpl, const std::vector<llama_chat_message> &msgs) { inline std::string chaton_tmpl_apply(const std::string &tmpl, const std::vector<llama_chat_message> &msgs) {
ChatParts cp = {};
std::stringstream ss; std::stringstream ss;
ss << conMeta[tmpl][K_GLOBAL][K_BEGIN]; ss << conMeta[tmpl][K_GLOBAL][K_BEGIN];
cp.add_part(ChatParts::S, conMeta[tmpl][K_GLOBAL][K_BEGIN]);
int cntSystem = 0; int cntSystem = 0;
int cntUser = 0; int cntUser = 0;
int cntOthers = 0; int cntOthers = 0;
@ -199,6 +201,7 @@ inline std::string chaton_tmpl_apply(const std::string &tmpl, const std::vector<
std::string begin = ""; std::string begin = "";
try { try {
begin = conMeta[tmpl][role][K_BEGIN]; begin = conMeta[tmpl][role][K_BEGIN];
cp.add_part(ChatParts::S, begin);
} catch (json::exception &err) { } catch (json::exception &err) {
} }
@ -206,26 +209,43 @@ inline std::string chaton_tmpl_apply(const std::string &tmpl, const std::vector<
if (role == K_SYSTEM) { if (role == K_SYSTEM) {
cntSystem += 1; cntSystem += 1;
ss << begin << prefix; ss << begin << prefix;
cp.add_part(ChatParts::S, begin);
cp.add_part(ChatParts::S, prefix);
} else if (role == K_USER) { } else if (role == K_USER) {
cntUser += 1; cntUser += 1;
if ((cntSystem == 1) && (cntUser == 1)) { if ((cntSystem == 1) && (cntUser == 1)) {
if (conMeta[tmpl][K_SYSTEMUSER_1ST_USER_HAS_BEGIN]) { if (conMeta[tmpl][K_SYSTEMUSER_1ST_USER_HAS_BEGIN]) {
ss << begin; ss << begin;
cp.add_part(ChatParts::S, begin);
} }
if (conMeta[tmpl][K_SYSTEMUSER_1ST_USER_HAS_PREFIX]) { if (conMeta[tmpl][K_SYSTEMUSER_1ST_USER_HAS_PREFIX]) {
ss << prefix; ss << prefix;
cp.add_part(ChatParts::S, prefix);
} }
} else { } else {
ss << begin << prefix; ss << begin << prefix;
cp.add_part(ChatParts::S, begin);
cp.add_part(ChatParts::S, prefix);
} }
} else { } else {
cntOthers += 1; cntOthers += 1;
ss << begin << prefix; ss << begin << prefix;
cp.add_part(ChatParts::S, begin);
cp.add_part(ChatParts::S, prefix);
} }
ss << content << conMeta[tmpl][role][K_SUFFIX]; ss << content << conMeta[tmpl][role][K_SUFFIX];
cp.add_part(ChatParts::N, content);
cp.add_part(ChatParts::S, conMeta[tmpl][role][K_SUFFIX]);
} }
ss << conMeta[tmpl][K_GLOBAL][K_END]; ss << conMeta[tmpl][K_GLOBAL][K_END];
cp.add_part(ChatParts::S, conMeta[tmpl][K_GLOBAL][K_END]);
cp.dump();
std::string taggedMsgs = ss.str(); std::string taggedMsgs = ss.str();
std::string cpStr = cp.str();
if (taggedMsgs != cpStr) {
LOG_TEELN("DBUG:%s:Mismatch between CP[%s] and SS[%s]", __func__, cpStr.c_str(), taggedMsgs.c_str());
exit(2);
}
LOGLN("DBUG:%s:%s:%s", __func__, tmpl.c_str(), taggedMsgs.c_str()); LOGLN("DBUG:%s:%s:%s", __func__, tmpl.c_str(), taggedMsgs.c_str());
LOGLN("DBUG:%s:%s:CntSys[%d]:CntUsr[%d]:CntOthers[%d]", __func__, tmpl.c_str(), cntSystem, cntUser, cntOthers); LOGLN("DBUG:%s:%s:CntSys[%d]:CntUsr[%d]:CntOthers[%d]", __func__, tmpl.c_str(), cntSystem, cntUser, cntOthers);
return taggedMsgs; return taggedMsgs;