ChatOn+Main: ChatApplyTemplateSimple cleanup

Cleanup the associated log messages.

Dont overload the return for status as well as data. Now the data
returned if any is kept independent of the status of the operation.

On failure log a message and exit.
This commit is contained in:
HanishKVC 2024-04-20 22:41:27 +05:30
parent aac2ee6e9d
commit ca55da2b6f
2 changed files with 21 additions and 13 deletions

View file

@ -6,30 +6,33 @@
#include "llama.h" #include "llama.h"
#include "log.h" #include "log.h"
inline std::string llama_chat_apply_template_simple( // Tag the passed message suitabley as expected by the specified chat handshake template
// and the role. If the specified template is not supported logic will return false.
inline bool llama_chat_apply_template_simple(
const std::string &tmpl, const std::string &tmpl,
const std::string &role, const std::string &role,
const std::string &content, const std::string &content,
std::string &dst,
bool add_ass) { bool add_ass) {
llama_chat_message msg = { role.c_str(), content.c_str() }; llama_chat_message msg = { role.c_str(), content.c_str() };
//std::vector<llama_chat_message> msgs{ msg }; std::vector<char> buf(content.size() * 2); // This may under allot for small messages and over allot for large messages
std::vector<char> buf(content.size() * 2);
int32_t slen = llama_chat_apply_template(nullptr, tmpl.c_str(), &msg, 1, add_ass, buf.data(), buf.size()); int32_t slen = llama_chat_apply_template(nullptr, tmpl.c_str(), &msg, 1, add_ass, buf.data(), buf.size());
LOG_TEELN("DBUG:%s:AA:%s:LengthNeeded:%d:BufSizeWas:%zu", __func__, role.c_str(), slen, buf.size());
if (slen == -1) { if (slen == -1) {
LOG_TEELN("WARN:%s:Unknown template [%s] encounted", __func__, tmpl.c_str()); LOG_TEELN("WARN:%s:Unknown template [%s] requested", __func__, tmpl.c_str());
return ""; dst = "";
return false;
} }
if ((size_t) slen > buf.size()) { if ((size_t) slen > buf.size()) {
LOGLN("INFO:%s:%s:LengthNeeded:%d:BufSizeWas:%zu", __func__, role.c_str(), slen, buf.size());
buf.resize(slen); buf.resize(slen);
slen = llama_chat_apply_template(nullptr, tmpl.c_str(), &msg, 1, add_ass, buf.data(), buf.size()); slen = llama_chat_apply_template(nullptr, tmpl.c_str(), &msg, 1, add_ass, buf.data(), buf.size());
LOG_TEELN("DBUG:%s:BB:%s:LengthNeeded:%d:BufSizeWas:%zu", __func__, role.c_str(), slen, buf.size());
} }
const std::string tagged_msg(buf.data(), slen); const std::string tagged_msg(buf.data(), slen);
LOGLN("INFO:%s:%s", __func__, tagged_msg.c_str()); LOGLN("INFO:%s:%s:%s", __func__, role.c_str(), tagged_msg.c_str());
return tagged_msg; dst = tagged_msg;
return true;
} }
// return what should be the reverse prompt for the given template id // return what should be the reverse prompt for the given template id

View file

@ -258,9 +258,10 @@ int main(int argc, char ** argv) {
params.prompt = "<|im_start|>system\n" + params.prompt + "<|im_end|>"; params.prompt = "<|im_start|>system\n" + params.prompt + "<|im_end|>";
} }
if (params.chaton) { if (params.chaton) {
LOG_TEELN("DBUG:%s:AA:%s", __func__, params.prompt.c_str()); if (!llama_chat_apply_template_simple(params.chaton_template_id, "system", params.prompt, params.prompt, false)) {
params.prompt = llama_chat_apply_template_simple(params.chaton_template_id, "system", params.prompt, false); LOG_TEELN("ERRR:%s:Wrt:%s:%s:%s", __func__, params.chaton_template_id.c_str(), "system", params.prompt.c_str());
LOG_TEELN("DBUG:%s:BB:%s", __func__, params.prompt.c_str()); exit(2);
}
} }
embd_inp = ::llama_tokenize(ctx, params.prompt, true, true); embd_inp = ::llama_tokenize(ctx, params.prompt, true, true);
} else { } else {
@ -897,7 +898,11 @@ int main(int argc, char ** argv) {
std::vector<int> line_inp; std::vector<int> line_inp;
if (params.chaton) { if (params.chaton) {
std::string f_chat = llama_chat_apply_template_simple(params.chaton_template_id, "user", buffer.c_str(), true); std::string f_chat;
if (!llama_chat_apply_template_simple(params.chaton_template_id, "user", buffer.c_str(), f_chat, true)) {
LOG_TEELN("ERRR:%s:Wrt:%s:%s:%s", __func__, params.chaton_template_id.c_str(), "user", params.prompt.c_str());
exit(2);
}
line_inp = ::llama_tokenize(ctx, f_chat, false, true); line_inp = ::llama_tokenize(ctx, f_chat, false, true);
LOG("formatted input tokens: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, line_inp).c_str()); LOG("formatted input tokens: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, line_inp).c_str());
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end()); embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());