| 748 | } |
| 749 | |
| 750 | static common_chat_params common_chat_params_init_generic(const common_chat_template & tmpl, const struct templates_params & inputs) { |
| 751 | common_chat_params data; |
| 752 | |
| 753 | auto tool_call_schemas = json::array(); |
| 754 | foreach_function(inputs.tools, [&](const json & tool) { |
| 755 | const auto & function = tool.at("function"); |
| 756 | auto tool_schema = json { |
| 757 | {"type", "object"}, |
| 758 | {"properties", { |
| 759 | {"name", { |
| 760 | {"type", "string"}, |
| 761 | {"const", function.at("name")}, |
| 762 | }}, |
| 763 | {"arguments", function.at("parameters")}, |
| 764 | }}, |
| 765 | {"required", json::array({"name", "arguments"})}, |
| 766 | }; |
| 767 | if (function.contains("description")) { |
| 768 | tool_schema["description"] = function.at("description"); |
| 769 | } |
| 770 | if (inputs.parallel_tool_calls) { |
| 771 | tool_schema.at("properties")["id"] = { |
| 772 | {"type", "string"}, |
| 773 | {"minLength", 4}, |
| 774 | }; |
| 775 | tool_schema.at("required").push_back("id"); |
| 776 | } |
| 777 | tool_call_schemas.emplace_back(tool_schema); |
| 778 | }); |
| 779 | const auto tool_call = |
| 780 | inputs.parallel_tool_calls |
| 781 | ? json { |
| 782 | {"type", "object"}, |
| 783 | {"properties", { |
| 784 | {"tool_calls", { |
| 785 | {"type", "array"}, |
| 786 | {"items", tool_call_schemas.size() == 1 ? tool_call_schemas[0] : json { |
| 787 | {"anyOf", tool_call_schemas}, |
| 788 | }}, |
| 789 | {"minItems", 1}, |
| 790 | }}, |
| 791 | }}, |
| 792 | {"required", json::array({"tool_calls"})}, |
| 793 | } |
| 794 | : json { |
| 795 | {"type", "object"}, |
| 796 | {"properties", { |
| 797 | {"tool_call", tool_call_schemas.size() == 1 ? tool_call_schemas[0] : json { |
| 798 | {"anyOf", tool_call_schemas}, |
| 799 | }}, |
| 800 | }}, |
| 801 | {"required", json::array({"tool_call"})}, |
| 802 | }; |
| 803 | const auto schema = |
| 804 | inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED |
| 805 | ? json { |
| 806 | {"anyOf", json::array({ |
| 807 | tool_call, |
no test coverage detected