ChatON:MetaJson: Add key constants, check metaJson loaded ifNeeded

This commit is contained in:
HanishKVC 2024-04-23 01:13:50 +05:30
parent 221ccd6462
commit 11b47fbcfc
4 changed files with 24 additions and 16 deletions

View file

@ -13,6 +13,10 @@
* d. systemuser-1st-user-has-prefix * d. systemuser-1st-user-has-prefix
* * if a combination of 1 system message followed by 1 or more user messages is seen, * * if a combination of 1 system message followed by 1 or more user messages is seen,
* then include user prefix only if this flag is set. * then include user prefix only if this flag is set.
* * one or two models which I looked at seem to require not just BoS, but also the user-role-tag-prefix
* to also be controlled wrt this case. So not differentiating between BoS and any user-role-tag-prefix
* However if this needs to be decoupled, then maybe will add begin and end keys to role blocks in the json.
* then depending on what model needs, one can setup role-begin and role-prefix suitably.
* 2. Give the below option to user wrt system prompt, this should give the flexibility to either keep system prompt simple or complex in a flexible yet simple way. * 2. Give the below option to user wrt system prompt, this should give the flexibility to either keep system prompt simple or complex in a flexible yet simple way.
* a. the system prompt they specify using -f, is used as is with parse_special when tokenising or * a. the system prompt they specify using -f, is used as is with parse_special when tokenising or
* b. whether the system prefix and suffix is added, but without parse_special tokenisation of system-prompt provided by user. * b. whether the system prefix and suffix is added, but without parse_special tokenisation of system-prompt provided by user.
@ -35,6 +39,9 @@ const auto K_SYSTEM = "system";
const auto K_USER = "user"; const auto K_USER = "user";
const auto K_PREFIX = "prefix"; const auto K_PREFIX = "prefix";
const auto K_SUFFIX = "suffix"; const auto K_SUFFIX = "suffix";
const auto K_BEGIN = "begin";
const auto K_END = "end";
const auto K_GLOBAL = "global";
const auto K_SYSTEMUSER_1ST_USER_HAS_PREFIX = "systemuser-1st-user-has-prefix"; const auto K_SYSTEMUSER_1ST_USER_HAS_PREFIX = "systemuser-1st-user-has-prefix";
@ -48,26 +55,25 @@ inline bool chaton_meta_load(std::string &fname) {
return true; return true;
} }
inline void _chaton_meta_dump() {
LOG_TEELN("\n\nINFO:%s:ChatOn Meta\n%s", __func__, conMeta.dump(4).c_str());
}
inline bool chaton_meta_ok() { inline bool chaton_meta_ok() {
if (conMeta == nullptr) { if (conMeta == nullptr) {
LOG_TEELN("ERRR:%s:ChatOn Meta: Not loaded yet...", __func__);
return false; return false;
} }
_chaton_meta_dump();
return true; return true;
} }
inline void chaton_meta_dump() {
if (!chaton_meta_ok()) {
LOG_TEELN("ERRR:%s:ChatOn Meta: Not loaded yet...", __func__);
return;
}
LOG_TEELN("\n\nINFO:%s:ChatOn Meta\n%s", __func__, conMeta.dump(4).c_str());
}
// Return user-prefix + msg + user-suffix // 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 // 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 std::string chaton_tmpl_apply_single(const std::string &tmpl, const std::string &role, const std::string &content) {
std::stringstream ss; std::stringstream ss;
ss << conMeta[tmpl][role]["prefix"] << content << conMeta[tmpl][role]["suffix"]; ss << conMeta[tmpl][role][K_PREFIX] << content << conMeta[tmpl][role][K_SUFFIX];
std::string taggedStr = ss.str(); std::string taggedStr = ss.str();
LOG_TEELN("DBUG:%s:%s:%s:%s", __func__, tmpl.c_str(), role.c_str(), taggedStr.c_str()); LOG_TEELN("DBUG:%s:%s:%s:%s", __func__, tmpl.c_str(), role.c_str(), taggedStr.c_str());
return taggedStr; return taggedStr;
@ -79,7 +85,7 @@ inline std::string chaton_tmpl_apply_single(const std::string &tmpl, const std::
// 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) {
std::stringstream ss; std::stringstream ss;
ss << conMeta[tmpl]["global"]["begin"]; ss << conMeta[tmpl][K_GLOBAL][K_BEGIN];
int cntSystem = 0; int cntSystem = 0;
int cntUser = 0; int cntUser = 0;
int cntOthers = 0; int cntOthers = 0;
@ -103,9 +109,9 @@ inline std::string chaton_tmpl_apply(const std::string &tmpl, const std::vector<
cntOthers += 1; cntOthers += 1;
ss << prefix; ss << prefix;
} }
ss << content << conMeta[tmpl][role]["suffix"]; ss << content << conMeta[tmpl][role][K_SUFFIX];
} }
ss << conMeta[tmpl]["global"]["end"]; ss << conMeta[tmpl][K_GLOBAL][K_END];
std::string taggedMsgs = ss.str(); std::string taggedMsgs = ss.str();
LOG_TEELN("DBUG:%s:%s:%s", __func__, tmpl.c_str(), taggedMsgs.c_str()); LOG_TEELN("DBUG:%s:%s:%s", __func__, tmpl.c_str(), taggedMsgs.c_str());
return taggedMsgs; return taggedMsgs;

View file

@ -915,12 +915,12 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
params.chatml = true; params.chatml = true;
return true; return true;
} }
if (arg == "--chaton-json") { if (arg == "--chaton-meta-json") {
if (++i >= argc) { if (++i >= argc) {
invalid_param = true; invalid_param = true;
return true; return true;
} }
params.chaton_json = argv[i]; params.chaton_meta_json = argv[i];
return true; return true;
} }
if (arg == "--chaton-template-id") { if (arg == "--chaton-template-id") {

View file

@ -142,7 +142,7 @@ struct gpt_params {
bool interactive = false; // interactive mode bool interactive = false; // interactive mode
bool chatml = false; // chatml mode (used for models trained on chatml syntax) bool chatml = false; // chatml mode (used for models trained on chatml syntax)
bool chaton = false; // whether chaton is enabled or disabled bool chaton = false; // whether chaton is enabled or disabled
std::string chaton_json = ""; // name of the json file containing the chaton templates std::string chaton_meta_json = ""; // name of the json file containing the chaton templates
std::string chaton_template_id = ""; // the specific chat-handshake-template-standard to use std::string chaton_template_id = ""; // the specific chat-handshake-template-standard to use
bool prompt_cache_all = false; // save user input and generations to prompt cache bool prompt_cache_all = false; // save user input and generations to prompt cache
bool prompt_cache_ro = false; // open the prompt cache read-only and do not update it bool prompt_cache_ro = false; // open the prompt cache read-only and do not update it

View file

@ -143,8 +143,10 @@ int main(int argc, char ** argv) {
atexit([]() { console::cleanup(); }); atexit([]() { console::cleanup(); });
if (params.chaton) { if (params.chaton) {
chaton_meta_load(params.chaton_json); chaton_meta_load(params.chaton_meta_json);
chaton_meta_dump(); if (!chaton_meta_ok()) {
exit(1);
}
} }
if (params.logits_all) { if (params.logits_all) {