| 1280 | } |
| 1281 | |
| 1282 | static common_chat_params common_chat_params_init_firefunction_v2(const common_chat_template & tmpl, const struct templates_params & inputs) { |
| 1283 | LOG_DBG("%s\n", __func__); |
| 1284 | common_chat_params data; |
| 1285 | data.prompt = apply(tmpl, inputs.messages, /* tools= */ nullptr, inputs.add_generation_prompt, { |
| 1286 | {"datetime", format_time(inputs.now, "%b %d %Y %H:%M:%S GMT")}, |
| 1287 | {"functions", json(inputs.tools.empty() ? "" : inputs.tools.dump(2))}, |
| 1288 | }); |
| 1289 | if (inputs.tools.is_array() && !inputs.tools.empty()) { |
| 1290 | data.grammar_lazy = inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED; |
| 1291 | data.grammar = build_grammar([&](const common_grammar_builder & builder) { |
| 1292 | auto schemas = json::array(); |
| 1293 | foreach_function(inputs.tools, [&](const json & tool) { |
| 1294 | const auto & function = tool.at("function"); |
| 1295 | schemas.push_back({ |
| 1296 | {"type", "object"}, |
| 1297 | {"properties", { |
| 1298 | {"name", { |
| 1299 | {"type", "string"}, |
| 1300 | {"const", function.at("name")}, |
| 1301 | }}, |
| 1302 | {"arguments", function.at("parameters")}, |
| 1303 | }}, |
| 1304 | {"required", json::array({"name", "arguments", "id"})}, |
| 1305 | }); |
| 1306 | }); |
| 1307 | auto schema = json { |
| 1308 | {"type", "array"}, |
| 1309 | {"items", schemas.size() == 1 ? schemas[0] : json {{"anyOf", schemas}}}, |
| 1310 | {"minItems", 1}, |
| 1311 | }; |
| 1312 | if (!inputs.parallel_tool_calls) { |
| 1313 | schema["maxItems"] = 1; |
| 1314 | } |
| 1315 | builder.add_rule("root", "\" functools\"? " + builder.add_schema("tool_calls", schema)); |
| 1316 | }); |
| 1317 | data.grammar_triggers.push_back({COMMON_GRAMMAR_TRIGGER_TYPE_WORD, " functools["}); |
| 1318 | data.preserved_tokens = { |
| 1319 | " functools[", |
| 1320 | }; |
| 1321 | data.format = COMMON_CHAT_FORMAT_FIREFUNCTION_V2; |
| 1322 | } else { |
| 1323 | data.format = COMMON_CHAT_FORMAT_CONTENT_ONLY; |
| 1324 | } |
| 1325 | return data; |
| 1326 | } |
| 1327 | static void common_chat_parse_firefunction_v2(common_chat_msg_parser & builder) { |
| 1328 | if (!builder.syntax().parse_tool_calls) { |
| 1329 | builder.add_content(builder.consume_rest()); |
no test coverage detected