Simplify tool call grammars when there's only 1 tool

This commit is contained in:
ochafik 2024-12-27 02:20:52 +00:00
parent a2fe8a4922
commit 523ebf8cba

View file

@ -448,7 +448,10 @@ llama_tool_call_handler llama_tool_call_handler_init(
{"properties", { {"properties", {
{"tool_calls", { {"tool_calls", {
{"type", "array"}, {"type", "array"},
{"items", json {{"anyOf", tool_call_schemas}}} {"items", tool_call_schemas.size() == 1 ? tool_call_schemas[0] : json {
{"anyOf", tool_call_schemas},
}},
{"minItems", 1},
}}, }},
}}, }},
{"required", json::array({"tool_calls"})}, {"required", json::array({"tool_calls"})},
@ -456,7 +459,9 @@ llama_tool_call_handler llama_tool_call_handler_init(
: json { : json {
{"type", "object"}, {"type", "object"},
{"properties", { {"properties", {
{"tool_call", json {{"anyOf", tool_call_schemas}}}, {"tool_call", tool_call_schemas.size() == 1 ? tool_call_schemas[0] : json {
{"anyOf", tool_call_schemas},
}},
}}, }},
{"required", json::array({"tool_call"})}, {"required", json::array({"tool_call"})},
}; };
@ -473,6 +478,7 @@ llama_tool_call_handler llama_tool_call_handler_init(
: json_schema : json_schema
}, },
}}, }},
{"required", json::array({"response"})},
}, },
})} })}
} }
@ -514,7 +520,7 @@ llama_tool_call_handler llama_tool_call_handler_init(
} }
auto schema = json { auto schema = json {
{"type", "array"}, {"type", "array"},
{"items", json {{"anyOf", schemas}}}, {"items", schemas.size() == 1 ? schemas[0] : json {{"anyOf", schemas}}},
{"minItems", 1}, {"minItems", 1},
}; };
if (!parallel) { if (!parallel) {
@ -548,7 +554,7 @@ llama_tool_call_handler llama_tool_call_handler_init(
} }
auto schema = json { auto schema = json {
{"type", "array"}, {"type", "array"},
{"items", json {{"anyOf", schemas}}}, {"items", schemas.size() == 1 ? schemas[0] : json {{"anyOf", schemas}}},
{"minItems", 1}, {"minItems", 1},
}; };
if (!parallel) { if (!parallel) {