| 324 | |
| 325 | template <> |
| 326 | std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const json & tools) { |
| 327 | std::vector<common_chat_tool> result; |
| 328 | |
| 329 | try { |
| 330 | if (!tools.is_null()) { |
| 331 | if (!tools.is_array()) { |
| 332 | throw std::runtime_error("Expected 'tools' to be an array, got " + tools.dump()); |
| 333 | } |
| 334 | for (const auto & tool : tools) { |
| 335 | if (!tool.contains("type")) { |
| 336 | throw std::runtime_error("Missing tool type: " + tool.dump()); |
| 337 | } |
| 338 | const auto & type = tool.at("type"); |
| 339 | if (!type.is_string() || type != "function") { |
| 340 | throw std::runtime_error("Unsupported tool type: " + tool.dump()); |
| 341 | } |
| 342 | if (!tool.contains("function")) { |
| 343 | throw std::runtime_error("Missing tool function: " + tool.dump()); |
| 344 | } |
| 345 | |
| 346 | const auto & function = tool.at("function"); |
| 347 | result.push_back({ |
| 348 | /* .name = */ function.at("name"), |
| 349 | /* .description = */ function.at("description"), |
| 350 | /* .parameters = */ function.at("parameters").dump(), |
| 351 | }); |
| 352 | } |
| 353 | } |
| 354 | } catch (const std::exception & e) { |
| 355 | throw std::runtime_error("Failed to parse tools: " + std::string(e.what()) + "; tools = " + tools.dump(2)); |
| 356 | } |
| 357 | |
| 358 | return result; |
| 359 | } |
| 360 | |
| 361 | template <> |
| 362 | std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const std::string & tools) { |