| 719 | } |
| 720 | |
| 721 | static std::string apply( |
| 722 | const common_chat_template & tmpl, |
| 723 | const nlohmann::ordered_json & messages, |
| 724 | const nlohmann::ordered_json & tools, |
| 725 | bool add_generation_prompt, |
| 726 | const nlohmann::ordered_json & extra_context = nlohmann::ordered_json()) |
| 727 | { |
| 728 | minja::chat_template_inputs tmpl_inputs; |
| 729 | tmpl_inputs.messages = messages; |
| 730 | tmpl_inputs.tools = tools; |
| 731 | tmpl_inputs.add_generation_prompt = add_generation_prompt; |
| 732 | tmpl_inputs.extra_context = extra_context; |
| 733 | // TODO: add flag to control date/time, if only for testing purposes. |
| 734 | // tmpl_inputs.now = std::chrono::system_clock::now(); |
| 735 | |
| 736 | minja::chat_template_options tmpl_opts; |
| 737 | // To avoid double BOS / EOS tokens, we're manually removing begining / trailing tokens |
| 738 | // instead of using `chat_template_options.use_bos_token = false`, since these tokens |
| 739 | // may be needed inside the template / between messages too. |
| 740 | auto result = tmpl.apply(tmpl_inputs, tmpl_opts); |
| 741 | if (string_starts_with(result, tmpl.bos_token())) { |
| 742 | result = result.substr(tmpl.bos_token().size()); |
| 743 | } |
| 744 | if (string_ends_with(result, tmpl.eos_token())) { |
| 745 | result = result.substr(0, result.size() - tmpl.eos_token().size()); |
| 746 | } |
| 747 | return result; |
| 748 | } |
| 749 | |
| 750 | static common_chat_params common_chat_params_init_generic(const common_chat_template & tmpl, const struct templates_params & inputs) { |
| 751 | common_chat_params data; |
no test coverage detected