Skip to content

Commit 929ea5c

Browse files
committed
ChatON:ChatTemplateApplyCAPI remaining base logic
As c doesnt have the concept of pass by reference, and inturn the existing c api uses pointers wrt llama chat message structure, so switching to same wrt chat_tmpl_apply logics. Also fix a oversight in previous commit and add the remaining logic.
1 parent 1f6c8a9 commit 929ea5c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

common/chaton.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ inline size_t chat_tmpl_apply_single_capi(
300300
// NOTE: returns types and lens to help identify the parts of the tagged msg, which relate to passed and added tags
301301
inline bool chaton_tmpl_apply_ex(
302302
const std::string &tmpl,
303-
const std::vector<llama_chat_message> &msgs,
303+
const std::vector<const llama_chat_message *> &msgs,
304304
std::string &tagged,
305305
std::string &types,
306306
std::vector<int> &lens,
@@ -318,8 +318,8 @@ inline bool chaton_tmpl_apply_ex(
318318
int cntUser = 0;
319319
int cntOthers = 0;
320320
for(const auto msg: msgs) {
321-
auto role = msg.role;
322-
auto content = msg.content;
321+
auto role = msg->role;
322+
auto content = msg->content;
323323
std::string begin = chaton_tmpl_role_kv(tmpl, role, {K_BEGIN});
324324
auto prefix = chaton_tmpl_role_kv(tmpl, role, {K_PREFIX});
325325
auto suffix = chaton_tmpl_role_kv(tmpl, role, {K_SUFFIX});
@@ -395,7 +395,7 @@ inline bool chaton_tmpl_apply_ex(
395395
// then 1st user message will have user-prefix only if systemuser-1st-user-has-prefix is true
396396
inline int32_t chaton_tmpl_apply(
397397
const std::string &tmpl,
398-
const std::vector<llama_chat_message> &msgs,
398+
const std::vector<const llama_chat_message *> &msgs,
399399
bool alertAssistantAtEnd,
400400
std::string &tagged
401401
) {
@@ -420,10 +420,17 @@ inline int32_t chaton_tmpl_apply_capi(
420420
}
421421
std::vector<const llama_chat_message *> vMsgs;
422422
for(size_t i=0; i<numMsgs; i++) {
423-
vMsgs.push_back(vMsgs[i]);
423+
vMsgs.push_back(&msgs[i]);
424424
}
425425
std::string taggedMsgs;
426426
int32_t taggedLength = chaton_tmpl_apply(tmpl, vMsgs, alertAssistantAtEnd, taggedMsgs);
427+
if (taggedLength <= 0) {
428+
return taggedLength;
429+
}
430+
if (destLength > 0) {
431+
strlcpy(dest, taggedMsgs.c_str(), destLength);
432+
}
433+
return taggedLength;
427434
}
428435

429436

0 commit comments

Comments
 (0)