chat: make tool description and parameters optional per OpenAI spec (#18478)
* chat: make tool description and parameters optional per OpenAI spec Per the OpenAI API specification, both 'description' and 'parameters' fields in tool function definitions are optional. Previously, the parser would throw an exception if these fields were missing. Attempts to fix #17667 * refactor: use value() for cleaner optional field access
This commit is contained in:
parent
13814eb370
commit
4cd162a123
|
|
@ -380,8 +380,8 @@ std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const json & too
|
|||
const auto & function = tool.at("function");
|
||||
result.push_back({
|
||||
/* .name = */ function.at("name"),
|
||||
/* .description = */ function.at("description"),
|
||||
/* .parameters = */ function.at("parameters").dump(),
|
||||
/* .description = */ function.value("description", ""),
|
||||
/* .parameters = */ function.value("parameters", json::object()).dump(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -724,6 +724,30 @@ static void test_tools_oaicompat_json_conversion() {
|
|||
"]"
|
||||
),
|
||||
common_chat_tools_to_json_oaicompat<json>({special_function_tool}).dump(2));
|
||||
|
||||
{
|
||||
auto tools_no_params = common_chat_tools_parse_oaicompat(json::parse(
|
||||
R"([{"type": "function", "function": {"name": "test_func", "description": "A test"}}])"));
|
||||
assert_equals((size_t) 1, tools_no_params.size());
|
||||
assert_equals(std::string("test_func"), tools_no_params[0].name);
|
||||
assert_equals(std::string("A test"), tools_no_params[0].description);
|
||||
assert_equals(std::string("{}"), tools_no_params[0].parameters);
|
||||
}
|
||||
{
|
||||
auto tools_no_desc = common_chat_tools_parse_oaicompat(json::parse(
|
||||
R"([{"type": "function", "function": {"name": "test_func", "parameters": {"type": "object"}}}])"));
|
||||
assert_equals((size_t) 1, tools_no_desc.size());
|
||||
assert_equals(std::string("test_func"), tools_no_desc[0].name);
|
||||
assert_equals(std::string(""), tools_no_desc[0].description);
|
||||
}
|
||||
{
|
||||
auto tools_minimal = common_chat_tools_parse_oaicompat(json::parse(
|
||||
R"([{"type": "function", "function": {"name": "test_func"}}])"));
|
||||
assert_equals((size_t) 1, tools_minimal.size());
|
||||
assert_equals(std::string("test_func"), tools_minimal[0].name);
|
||||
assert_equals(std::string(""), tools_minimal[0].description);
|
||||
assert_equals(std::string("{}"), tools_minimal[0].parameters);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_template_output_parsers() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue