| 417 | } |
| 418 | |
| 419 | std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const json & tools) { |
| 420 | std::vector<common_chat_tool> result; |
| 421 | |
| 422 | try { |
| 423 | if (!tools.is_null()) { |
| 424 | if (!tools.is_array()) { |
| 425 | throw std::invalid_argument("Expected 'tools' to be an array, got " + tools.dump()); |
| 426 | } |
| 427 | for (const auto & tool : tools) { |
| 428 | if (!tool.contains("type")) { |
| 429 | throw std::invalid_argument("Missing tool type: " + tool.dump()); |
| 430 | } |
| 431 | const auto & type = tool.at("type"); |
| 432 | if (!type.is_string() || type != "function") { |
| 433 | throw std::invalid_argument("Unsupported tool type: " + tool.dump()); |
| 434 | } |
| 435 | if (!tool.contains("function")) { |
| 436 | throw std::invalid_argument("Missing tool function: " + tool.dump()); |
| 437 | } |
| 438 | |
| 439 | const auto & function = tool.at("function"); |
| 440 | result.push_back({ |
| 441 | /* .name = */ function.at("name"), |
| 442 | /* .description = */ function.value("description", ""), |
| 443 | /* .parameters = */ function.value("parameters", json::object()).dump(), |
| 444 | }); |
| 445 | } |
| 446 | } |
| 447 | } catch (const std::exception & e) { |
| 448 | throw std::runtime_error("Failed to parse tools: " + std::string(e.what()) + "; tools = " + tools.dump(2)); |
| 449 | } |
| 450 | |
| 451 | return result; |
| 452 | } |
| 453 | |
| 454 | bool common_chat_verify_template(const std::string & tmpl, bool use_jinja) { |
| 455 | if (use_jinja) { |