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

Function common_chat_parse_llama_3_1

smallthinker/common/chat.cpp:1132–1186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1130 return data;
1131}
1132static void common_chat_parse_llama_3_1(common_chat_msg_parser & builder, bool with_builtin_tools = false) {
1133 if (!builder.syntax().parse_tool_calls) {
1134 builder.add_content(builder.consume_rest());
1135 return;
1136 }
1137
1138 static const common_regex function_regex(
1139 "\\s*\\{\\s*(?:\"type\"\\s*:\\s*\"function\"\\s*,\\s*)?\"name\"\\s*:\\s*\"([^\"]+)\"\\s*,\\s*\"parameters\"\\s*: ");
1140 static const common_regex close_regex("\\}\\s*");
1141
1142 static const common_regex function_name_regex("\\s*(\\w+)\\s*\\.\\s*call\\(");
1143 static const common_regex arg_name_regex("\\s*(\\w+)\\s*=\\s*");
1144
1145 if (with_builtin_tools) {
1146 static const common_regex builtin_call_regex("<\\|python_tag\\|>");
1147 if (auto res = builder.try_find_regex(builtin_call_regex)) {
1148 auto fun_res = builder.consume_regex(function_name_regex);
1149 auto function_name = builder.str(fun_res.groups[1]);
1150
1151 common_healing_marker healing_marker;
1152 json args = json::object();
1153 while (true) {
1154 if (auto arg_res = builder.try_consume_regex(arg_name_regex)) {
1155 auto arg_name = builder.str(arg_res->groups[1]);
1156 auto partial = builder.consume_json();
1157 args[arg_name] = partial.json;
1158 healing_marker.marker = partial.healing_marker.marker;
1159 healing_marker.json_dump_marker = partial.healing_marker.json_dump_marker;
1160 builder.consume_spaces();
1161 if (!builder.try_consume_literal(",")) {
1162 break;
1163 }
1164 } else {
1165 break;
1166 }
1167 }
1168 builder.consume_literal(")");
1169 builder.consume_spaces();
1170
1171 auto arguments = args.dump();
1172 if (!builder.add_tool_call(function_name, "", arguments)) {
1173 throw common_chat_msg_partial_exception("Incomplete tool call");
1174 }
1175 return;
1176 }
1177 }
1178 parse_json_tool_calls(
1179 builder,
1180 /* block_open= */ std::nullopt,
1181 /* function_regex_start_only= */ function_regex,
1182 /* function_regex= */ std::nullopt,
1183 close_regex,
1184 std::nullopt);
1185
1186}
1187
1188static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_template & tmpl, const struct templates_params & inputs) {
1189 common_chat_params data;

Callers 1

common_chat_parseFunction · 0.85

Calls 14

parse_json_tool_callsFunction · 0.85
add_contentMethod · 0.80
consume_restMethod · 0.80
try_find_regexMethod · 0.80
consume_regexMethod · 0.80
try_consume_regexMethod · 0.80
consume_jsonMethod · 0.80
consume_spacesMethod · 0.80
try_consume_literalMethod · 0.80
consume_literalMethod · 0.80
dumpMethod · 0.80

Tested by

no test coverage detected