@@ -168,15 +168,15 @@ static T json_value(const json &body, const std::string &key, const T &default_v
168
168
}
169
169
170
170
// 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) {
172
172
llama_chat_message chat[] = {{" user" , " test" }};
173
173
std::vector<char > buf (1 );
174
174
int res = llama_chat_apply_template (nullptr , tmpl.c_str (), chat, 1 , true , buf.data (), buf.size ());
175
175
return res >= 0 ;
176
176
}
177
177
178
178
// 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)
180
180
{
181
181
size_t alloc_size = 0 ;
182
182
// 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
185
185
186
186
for (size_t i = 0 ; i < messages.size (); ++i) {
187
187
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 ();
193
193
}
194
194
195
195
const char * ptr_tmpl = tmpl.empty () ? nullptr : tmpl.c_str ();
0 commit comments