Skip to content

Commit 74eebff

Browse files
authored
chat: make tool description and parameters optional per OpenAI spec (ggml-org#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 ggml-org#17667 * refactor: use value() for cleaner optional field access
1 parent b86b793 commit 74eebff

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

common/chat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const json & too
380380
const auto & function = tool.at("function");
381381
result.push_back({
382382
/* .name = */ function.at("name"),
383-
/* .description = */ function.at("description"),
384-
/* .parameters = */ function.at("parameters").dump(),
383+
/* .description = */ function.value("description", ""),
384+
/* .parameters = */ function.value("parameters", json::object()).dump(),
385385
});
386386
}
387387
}

tests/test-chat.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,30 @@ static void test_tools_oaicompat_json_conversion() {
724724
"]"
725725
),
726726
common_chat_tools_to_json_oaicompat<json>({special_function_tool}).dump(2));
727+
728+
{
729+
auto tools_no_params = common_chat_tools_parse_oaicompat(json::parse(
730+
R"([{"type": "function", "function": {"name": "test_func", "description": "A test"}}])"));
731+
assert_equals((size_t) 1, tools_no_params.size());
732+
assert_equals(std::string("test_func"), tools_no_params[0].name);
733+
assert_equals(std::string("A test"), tools_no_params[0].description);
734+
assert_equals(std::string("{}"), tools_no_params[0].parameters);
735+
}
736+
{
737+
auto tools_no_desc = common_chat_tools_parse_oaicompat(json::parse(
738+
R"([{"type": "function", "function": {"name": "test_func", "parameters": {"type": "object"}}}])"));
739+
assert_equals((size_t) 1, tools_no_desc.size());
740+
assert_equals(std::string("test_func"), tools_no_desc[0].name);
741+
assert_equals(std::string(""), tools_no_desc[0].description);
742+
}
743+
{
744+
auto tools_minimal = common_chat_tools_parse_oaicompat(json::parse(
745+
R"([{"type": "function", "function": {"name": "test_func"}}])"));
746+
assert_equals((size_t) 1, tools_minimal.size());
747+
assert_equals(std::string("test_func"), tools_minimal[0].name);
748+
assert_equals(std::string(""), tools_minimal[0].description);
749+
assert_equals(std::string("{}"), tools_minimal[0].parameters);
750+
}
727751
}
728752

729753
static void test_template_output_parsers() {

0 commit comments

Comments
 (0)