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

Function common_chat_params_init_command_r7b

smallthinker/common/chat.cpp:921–999  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

919}
920
921static common_chat_params common_chat_params_init_command_r7b(const common_chat_template & tmpl, const struct templates_params & inputs) {
922 common_chat_params data;
923
924 auto adjusted_messages = json::array();
925 for (const auto & msg : inputs.messages) {
926 auto has_reasoning_content = msg.contains("reasoning_content") && msg.at("reasoning_content").is_string();
927 auto has_tool_calls = msg.contains("tool_calls") && msg.at("tool_calls").is_array();
928 if (has_reasoning_content && has_tool_calls) {
929 auto adjusted_message = msg;
930 adjusted_message["tool_plan"] = msg.at("reasoning_content");
931 adjusted_message.erase("reasoning_content");
932 adjusted_messages.push_back(adjusted_message);
933 } else {
934 adjusted_messages.push_back(msg);
935 }
936 }
937 data.prompt = apply(tmpl, adjusted_messages, inputs.tools.empty() ? json() : inputs.tools, inputs.add_generation_prompt, {});
938 data.format = COMMON_CHAT_FORMAT_COMMAND_R7B;
939 if (string_ends_with(data.prompt, "<|START_THINKING|>")) {
940 if (!inputs.enable_thinking) {
941 data.prompt += "<|END_THINKING|>";
942 } else {
943 data.thinking_forced_open = true;
944 }
945 } else if (!inputs.enable_thinking && string_ends_with(data.prompt, "<|CHATBOT_TOKEN|>")) {
946 data.prompt += "<|START_THINKING|><|END_THINKING|>";
947 }
948
949 data.grammar_lazy = inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
950 data.grammar = build_grammar([&](const common_grammar_builder & builder) {
951 auto schemas = json::array();
952 foreach_function(inputs.tools, [&](const json & tool) {
953 const auto & function = tool.at("function");
954 schemas.push_back({
955 {"type", "object"},
956 {"properties", {
957 {"tool_call_id", {
958 {"type", "string"},
959 // Command-R's template expects an integer string.
960 {"pattern", "^[0-9]{1,10}$"},
961 }},
962 {"tool_name", {
963 {"type", "string"},
964 {"const", function.at("name")},
965 }},
966 {"parameters", function.at("parameters")},
967 }},
968 {"required", json::array({"tool_call_id", "tool_name", "parameters"})},
969 });
970 });
971 auto schema = json {
972 {"type", "array"},
973 {"items", schemas.size() == 1 ? schemas[0] : json {{"anyOf", schemas}}},
974 {"minItems", 1},
975 };
976 if (!inputs.parallel_tool_calls) {
977 schema["maxItems"] = 1;
978 }

Callers 1

Calls 9

applyFunction · 0.85
foreach_functionFunction · 0.85
add_ruleMethod · 0.80
string_ends_withFunction · 0.70
build_grammarFunction · 0.70
stringClass · 0.50
push_backMethod · 0.45
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected