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

Function common_chat_tools_parse_oaicompat

smallthinker/common/chat.cpp:326–359  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

324
325template <>
326std::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
361template <>
362std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const std::string & tools) {

Calls 5

dumpMethod · 0.80
stringClass · 0.50
parseFunction · 0.50
push_backMethod · 0.45
whatMethod · 0.45

Tested by 1