ChatON:ChatTmplApply: Avoid the stringstream

This commit is contained in:
HanishKVC 2024-04-28 09:31:15 +05:30
parent 889a45ff28
commit af9a0a211b

View file

@ -428,9 +428,7 @@ inline bool chaton_tmpl_apply_ex(
return false; return false;
} }
ChatParts cp = {}; ChatParts cp = {};
std::stringstream ss;
std::string globalBegin = chaton_tmpl_role_kv(tmpl, K_GLOBAL, {K_BEGIN}); std::string globalBegin = chaton_tmpl_role_kv(tmpl, K_GLOBAL, {K_BEGIN});
ss << globalBegin;
cp.add_part(ChatParts::S, globalBegin); cp.add_part(ChatParts::S, globalBegin);
int cntSystem = 0; int cntSystem = 0;
int cntUser = 0; int cntUser = 0;
@ -444,63 +442,47 @@ inline bool chaton_tmpl_apply_ex(
auto end = chaton_tmpl_role_kv(tmpl, role, {K_END}); auto end = chaton_tmpl_role_kv(tmpl, role, {K_END});
if (role == K_SYSTEM) { if (role == K_SYSTEM) {
cntSystem += 1; cntSystem += 1;
ss << begin << prefix;
cp.add_part(ChatParts::S, begin); cp.add_part(ChatParts::S, begin);
cp.add_part(ChatParts::S, prefix); 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;
cp.add_part(ChatParts::S, 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;
cp.add_part(ChatParts::S, prefix); cp.add_part(ChatParts::S, prefix);
} }
} else { } else {
ss << begin << prefix;
cp.add_part(ChatParts::S, begin); cp.add_part(ChatParts::S, begin);
cp.add_part(ChatParts::S, prefix); cp.add_part(ChatParts::S, prefix);
} }
} else { } else {
cntOthers += 1; cntOthers += 1;
ss << begin << prefix;
cp.add_part(ChatParts::S, begin); cp.add_part(ChatParts::S, begin);
cp.add_part(ChatParts::S, prefix); cp.add_part(ChatParts::S, prefix);
} }
ss << content;
cp.add_part(ChatParts::N, content); cp.add_part(ChatParts::N, content);
if (role == K_SYSTEM) { if (role == K_SYSTEM) {
if (chaton_tmpl_kv_bool(tmpl, K_SYSTEMUSER_SYSTEM_HAS_SUFFIX)) { if (chaton_tmpl_kv_bool(tmpl, K_SYSTEMUSER_SYSTEM_HAS_SUFFIX)) {
ss << suffix;
cp.add_part(ChatParts::S, suffix); cp.add_part(ChatParts::S, suffix);
} }
if (chaton_tmpl_kv_bool(tmpl, K_SYSTEMUSER_SYSTEM_HAS_END)) { if (chaton_tmpl_kv_bool(tmpl, K_SYSTEMUSER_SYSTEM_HAS_END)) {
ss << end;
cp.add_part(ChatParts::S, end); cp.add_part(ChatParts::S, end);
} }
} else { } else {
ss << suffix << end;
cp.add_part(ChatParts::S, suffix); cp.add_part(ChatParts::S, suffix);
cp.add_part(ChatParts::S, end); cp.add_part(ChatParts::S, end);
} }
} }
if (alertAssistantAtEnd) { if (alertAssistantAtEnd) {
auto assistantBeginPrefix = chaton_tmpl_role_kv(tmpl, K_ASSISTANT, {K_BEGIN, K_PREFIX}); auto assistantBeginPrefix = chaton_tmpl_role_kv(tmpl, K_ASSISTANT, {K_BEGIN, K_PREFIX});
ss << assistantBeginPrefix;
cp.add_part(ChatParts::S, assistantBeginPrefix); cp.add_part(ChatParts::S, assistantBeginPrefix);
} }
auto globalEnd = chaton_tmpl_role_kv(tmpl, K_GLOBAL, {K_END}); auto globalEnd = chaton_tmpl_role_kv(tmpl, K_GLOBAL, {K_END});
ss << globalEnd;
cp.add_part(ChatParts::S, globalEnd); cp.add_part(ChatParts::S, globalEnd);
cp.dump(); cp.dump();
tagged = ss.str(); tagged = cp.str();
std::string cpStr = cp.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", __func__, tmpl.c_str(), tagged.c_str()); LOGLN("DBUG:%s:%s:%s", __func__, tmpl.c_str(), tagged.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);
types = cp.get_types(); types = cp.get_types();