@@ -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