| 1149 | } |
| 1150 | |
| 1151 | static int process_user_message(const Opt & opt, const std::string & user_input, LlamaData & llama_data, |
| 1152 | const common_chat_templates_ptr & chat_templates, int & prev_len, |
| 1153 | const bool stdout_a_terminal) { |
| 1154 | add_message("user", opt.user.empty() ? user_input : opt.user, llama_data); |
| 1155 | int new_len; |
| 1156 | if (apply_chat_template_with_error_handling(chat_templates.get(), llama_data, true, new_len, opt.use_jinja) < 0) { |
| 1157 | return 1; |
| 1158 | } |
| 1159 | |
| 1160 | std::string prompt(llama_data.fmtted.begin() + prev_len, llama_data.fmtted.begin() + new_len); |
| 1161 | std::string response; |
| 1162 | if (generate_response(llama_data, prompt, response, stdout_a_terminal)) { |
| 1163 | return 1; |
| 1164 | } |
| 1165 | |
| 1166 | if (!opt.user.empty()) { |
| 1167 | return 2; |
| 1168 | } |
| 1169 | |
| 1170 | add_message("assistant", response, llama_data); |
| 1171 | if (apply_chat_template_with_error_handling(chat_templates.get(), llama_data, false, prev_len, opt.use_jinja) < 0) { |
| 1172 | return 1; |
| 1173 | } |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | // Main chat loop function |
| 1179 | static int chat_loop(LlamaData & llama_data, const Opt & opt) { |
no test coverage detected