MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / apply

Function apply

smallthinker/common/chat.cpp:721–748  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

719}
720
721static 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
750static common_chat_params common_chat_params_init_generic(const common_chat_template & tmpl, const struct templates_params & inputs) {
751 common_chat_params data;

Calls 5

substrMethod · 0.80
string_starts_withFunction · 0.70
string_ends_withFunction · 0.70
applyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected