From 308d3bf3ffd8089fbcad4244672c31d6039a2849 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Fri, 26 Apr 2024 07:58:40 +0530 Subject: [PATCH] ChatON:WIP:Add c api wrapper for chat_template_apply Initial skeletons Update existing logics to help with same. Also the inbetween helper was having a bad signature wrt returning status and data, thats also fixed. --- common/chaton.hpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/common/chaton.hpp b/common/chaton.hpp index 7cfdd8f99..7c31ad4dc 100644 --- a/common/chaton.hpp +++ b/common/chaton.hpp @@ -306,6 +306,9 @@ inline bool chaton_tmpl_apply_ex( std::vector &lens, bool alertAssistantAtEnd ) { + if (!chaton_tmpl_exists(tmpl)) { + return false; + } ChatParts cp = {}; std::stringstream ss; std::string globalBegin = chaton_tmpl_role_kv(tmpl, K_GLOBAL, {K_BEGIN}); @@ -390,16 +393,37 @@ inline bool chaton_tmpl_apply_ex( // global-begin + [[role-begin] + [role-prefix] + msg + role-suffix] + global-end // if there is a combination of system-user messages, // then 1st user message will have user-prefix only if systemuser-1st-user-has-prefix is true -inline std::string chaton_tmpl_apply( +inline int32_t chaton_tmpl_apply( const std::string &tmpl, const std::vector &msgs, - bool alertAssistantAtEnd + bool alertAssistantAtEnd, + std::string &tagged ) { - std::string tagged; std::string types; std::vector lens; - chaton_tmpl_apply_ex(tmpl, msgs, tagged, types, lens, alertAssistantAtEnd); - return tagged; + if (!chaton_tmpl_apply_ex(tmpl, msgs, tagged, types, lens, alertAssistantAtEnd)) { + return -1; + } + return tagged.size(); +} + +inline int32_t chaton_tmpl_apply_capi( + const char *tmpl, + const struct llama_chat_message *msgs, + const size_t numMsgs, + bool alertAssistantAtEnd, + char *dest, + int32_t destLength + ) { + if ((tmpl == nullptr) || (dest == nullptr)) { + return -1; + } + std::vector vMsgs; + for(size_t i=0; i