| 175 | }; |
| 176 | |
| 177 | static int generate_response(mtmd_cli_context & ctx, int n_predict) { |
| 178 | llama_tokens generated_tokens; |
| 179 | for (int i = 0; i < n_predict; i++) { |
| 180 | if (i > n_predict || !g_is_generating || g_is_interrupted) { |
| 181 | LOG("\n"); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | llama_token token_id = common_sampler_sample(ctx.smpl, ctx.lctx, -1); |
| 186 | generated_tokens.push_back(token_id); |
| 187 | common_sampler_accept(ctx.smpl, token_id, true); |
| 188 | |
| 189 | if (llama_vocab_is_eog(ctx.vocab, token_id) || ctx.check_antiprompt(generated_tokens)) { |
| 190 | LOG("\n"); |
| 191 | break; // end of generation |
| 192 | } |
| 193 | |
| 194 | LOG("%s", common_token_to_piece(ctx.lctx, token_id).c_str()); |
| 195 | fflush(stdout); |
| 196 | |
| 197 | if (g_is_interrupted) { |
| 198 | LOG("\n"); |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | // eval the token |
| 203 | common_batch_clear(ctx.batch); |
| 204 | common_batch_add(ctx.batch, token_id, ctx.n_past++, {0}, true); |
| 205 | if (llama_decode(ctx.lctx, ctx.batch)) { |
| 206 | LOG_ERR("failed to decode token\n"); |
| 207 | return 1; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | std::string generated_text = common_detokenize(ctx.lctx, generated_tokens); |
| 212 | common_chat_msg msg; |
| 213 | msg.role = "assistant"; |
| 214 | msg.content = generated_text; |
| 215 | ctx.chat_history.push_back(std::move(msg)); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static std::string chat_add_and_format(mtmd_cli_context & ctx, common_chat_msg & new_msg) { |
| 221 | LOG_DBG("chat_add_and_format: new_msg.role='%s', new_msg.content='%s'\n", |
no test coverage detected