Skip to content

Commit c53b34d

Browse files
committed
server: fix format_chat
1 parent b19f46a commit c53b34d

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

examples/server/server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2390,7 +2390,6 @@ static void server_params_parse(int argc, char **argv, server_params &sparams,
23902390
invalid_param = true;
23912391
break;
23922392
}
2393-
std::string value(argv[i]);
23942393
if (!verify_custom_template(argv[i])) {
23952394
fprintf(stderr, "error: the supplied chat template is not supported: %s\n", argv[i]);
23962395
fprintf(stderr, "note: llama.cpp does not use jinja parser, we only support commonly used templates\n");

examples/server/utils.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ static T json_value(const json &body, const std::string &key, const T &default_v
168168
}
169169

170170
// Check if the template supplied via "--chat-template" is supported or not. Returns true if it's valid
171-
inline bool verify_custom_template(std::string tmpl) {
171+
inline bool verify_custom_template(const std::string & tmpl) {
172172
llama_chat_message chat[] = {{"user", "test"}};
173173
std::vector<char> buf(1);
174174
int res = llama_chat_apply_template(nullptr, tmpl.c_str(), chat, 1, true, buf.data(), buf.size());
175175
return res >= 0;
176176
}
177177

178178
// Format given chat. If tmpl is empty, we take the template from model metadata
179-
inline std::string format_chat(const struct llama_model * model, const std::string tmpl, std::vector<json> messages)
179+
inline std::string format_chat(const struct llama_model * model, const std::string & tmpl, const std::vector<json> & messages)
180180
{
181181
size_t alloc_size = 0;
182182
// vector holding all allocated string to be passed to llama_chat_apply_template
@@ -185,11 +185,11 @@ inline std::string format_chat(const struct llama_model * model, const std::stri
185185

186186
for (size_t i = 0; i < messages.size(); ++i) {
187187
auto &curr_msg = messages[i];
188-
str[i] = json_value(curr_msg, "role", std::string(""));
189-
str[i + 1] = json_value(curr_msg, "content", std::string(""));
190-
alloc_size += str[i + 1].length();
191-
chat[i].role = str[i].c_str();
192-
chat[i].content = str[i + 1].c_str();
188+
str[i*2 + 0] = json_value(curr_msg, "role", std::string(""));
189+
str[i*2 + 1] = json_value(curr_msg, "content", std::string(""));
190+
alloc_size += str[i*2 + 1].length();
191+
chat[i].role = str[i*2 + 0].c_str();
192+
chat[i].content = str[i*2 + 1].c_str();
193193
}
194194

195195
const char * ptr_tmpl = tmpl.empty() ? nullptr : tmpl.c_str();

0 commit comments

Comments
 (0)