1
1
"""Support functions to enable tool calling"""
2
2
3
3
from typing import List , Dict
4
- from copy import deepcopy
5
4
import json
6
5
7
6
from endpoints .OAI .types .tools import ToolCall
10
9
def postprocess_tool_call (call_str : str ) -> List [ToolCall ]:
11
10
print (call_str )
12
11
tool_calls = json .loads (call_str )
12
+ print (tool_calls )
13
13
for tool_call in tool_calls :
14
14
tool_call ["function" ]["arguments" ] = json .dumps (
15
15
tool_call ["function" ]["arguments" ]
16
16
)
17
17
return [ToolCall (** tool_call ) for tool_call in tool_calls ]
18
18
19
-
20
- def generate_strict_schemas (data : ChatCompletionRequest ):
21
- base_schema = {
22
- "$defs" : {},
23
- "properties" : {
24
- "id" : {"title" : "Id" , "type" : "string" },
25
- "function" : {"title" : "Function" },
26
- "type" : {"$ref" : "#/$defs/Type" }
27
- },
28
- "required" : ["id" , "function" , "type" ],
29
- "title" : "ModelItem" ,
30
- "type" : "object"
19
+ def generate_strict_schemas (data : ChatCompletionRequest ) -> Dict :
20
+ # Generate the $defs section
21
+ defs = generate_defs (data .tools )
22
+
23
+ # Generate the root structure (now an array)
24
+ root_structure = {
25
+ "type" : "array" ,
26
+ "items" : {"$ref" : "#/$defs/ModelItem" }
31
27
}
32
28
33
- function_schemas = []
34
- argument_schemas = {}
29
+ # Combine the $defs and root structure
30
+ full_schema = {
31
+ "$defs" : defs ,
32
+ ** root_structure
33
+ }
35
34
36
- for i , tool in enumerate (data .tools ):
37
- function_name = f"Function{ i + 1 } " if i > 0 else "Function"
38
- argument_name = f"Arguments{ i + 1 } " if i > 0 else "Arguments"
39
- name_def = f"Name{ i + 1 } " if i > 0 else "Name"
35
+ return full_schema
36
+
37
+ def generate_defs (tools : List ) -> Dict :
38
+ defs = {}
39
+
40
+ for i , tool in enumerate (tools ):
41
+ function_name = f"Function{ i + 1 } " if i > 0 else "Function"
42
+ arguments_name = f"Arguments{ i + 1 } " if i > 0 else "Arguments"
43
+ name_const = f"Name{ i + 1 } " if i > 0 else "Name"
44
+
45
+ # Generate Arguments schema
46
+ defs [arguments_name ] = generate_arguments_schema (tool .function .parameters )
40
47
41
- # Create Name definition
42
- base_schema [ "$ defs" ][ name_def ] = {
48
+ # Generate Name schema
49
+ defs [ name_const ] = {
43
50
"const" : tool .function .name ,
44
- "enum" : [tool .function .name ],
45
- "title" : name_def ,
51
+ "title" : name_const ,
46
52
"type" : "string"
47
53
}
48
54
49
- # Create Arguments definition
50
- arg_properties = {}
51
- required_params = tool .function .parameters .get ('required' , [])
52
- for arg_name , arg_info in tool .function .parameters .get ('properties' , {}).items ():
53
- arg_properties [arg_name ] = {
54
- "title" : arg_name .capitalize (),
55
- "type" : arg_info ['type' ]
56
- }
57
-
58
- argument_schemas [argument_name ] = {
59
- "properties" : arg_properties ,
60
- "required" : required_params ,
61
- "title" : argument_name ,
62
- "type" : "object"
63
- }
64
-
65
- # Create Function definition
66
- function_schema = {
55
+ # Generate Function schema
56
+ defs [function_name ] = {
57
+ "type" : "object" ,
67
58
"properties" : {
68
- "name" : {"$ref" : f"#/$defs/{ name_def } " },
69
- "arguments" : {"$ref" : f"#/$defs/{ argument_name } " }
59
+ "name" : {"$ref" : f"#/$defs/{ name_const } " },
60
+ "arguments" : {"$ref" : f"#/$defs/{ arguments_name } " }
70
61
},
71
- "required" : ["name" , "arguments" ],
72
- "title" : function_name ,
73
- "type" : "object"
62
+ "required" : ["name" , "arguments" ]
74
63
}
75
-
76
- function_schemas .append ({"$ref" : f"#/$defs/{ function_name } " })
77
- base_schema ["$defs" ][function_name ] = function_schema
78
64
79
- # Add argument schemas to $defs
80
- base_schema ["$defs" ].update (argument_schemas )
81
-
82
- # Add Type definition
83
- base_schema ["$defs" ]["Type" ] = {
65
+ # Add ModelItem and Type schemas
66
+ defs ["ModelItem" ] = generate_model_item_schema (len (tools ))
67
+ defs ["Type" ] = {
84
68
"const" : "function" ,
85
- "enum" : ["function" ],
86
- "title" : "Type" ,
87
69
"type" : "string"
88
70
}
89
71
90
- # Set up the function property
91
- base_schema ["properties" ]["function" ]["anyOf" ] = function_schemas
92
-
93
- return base_schema
94
-
95
-
96
- # def generate_strict_schemas(data: ChatCompletionRequest):
97
- # schema = {
98
- # "type": "object",
99
- # "properties": {
100
- # "name": {"type": "string"},
101
- # "arguments": {
102
- # "type": "object",
103
- # "properties": {},
104
- # "required": []
105
- # }
106
- # },
107
- # "required": ["name", "arguments"]
108
- # }
72
+ return defs
109
73
110
- # function_schemas = []
74
+ def generate_arguments_schema (parameters : Dict ) -> Dict :
75
+ properties = {}
76
+ required = parameters .get ('required' , [])
111
77
112
- # for tool in data.tools:
113
- # func_schema = deepcopy(schema)
114
- # func_schema["properties"]["name"]["enum"] = [tool.function.name]
115
- # raw_params = tool.function.parameters.get('properties', {})
116
- # required_params = tool.function.parameters.get('required', [])
117
-
118
- # # Add argument properties and required fields
119
- # arg_properties = {}
120
- # for arg_name, arg_type in raw_params.items():
121
- # arg_properties[arg_name] = {"type": arg_type['type']}
122
-
123
- # func_schema["properties"]["arguments"]["properties"] = arg_properties
124
- # func_schema["properties"]["arguments"]["required"] = required_params
125
-
126
- # function_schemas.append(func_schema)
78
+ for name , details in parameters .get ('properties' , {}).items ():
79
+ properties [name ] = {"type" : details ['type' ]}
127
80
128
- # return _create_full_schema(function_schemas)
81
+ return {
82
+ "type" : "object" ,
83
+ "properties" : properties ,
84
+ "required" : required
85
+ }
129
86
130
- # def _create_full_schema(function_schemas: List) -> Dict:
131
- # # Define the master schema structure with placeholders for function schemas
132
- # tool_call_schema = {
133
- # "$schema": "http://json-schema.org/draft-07/schema#",
134
- # "type": "array",
135
- # "items": {
136
- # "type": "object",
137
- # "properties": {
138
- # "id": {"type": "string"},
139
- # "function": {
140
- # "type": "object", # Add this line
141
- # "oneOf": function_schemas
142
- # },
143
- # "type": {"type": "string", "enum": ["function"]}
144
- # },
145
- # "required": ["id", "function", "type"]
146
- # }
147
- # }
148
-
149
- # print(json.dumps(tool_call_schema, indent=2))
150
- # return tool_call_schema
87
+ def generate_model_item_schema (num_functions : int ) -> Dict :
88
+ function_refs = [{"$ref" : f"#/$defs/Function{ i + 1 } " if i > 0 else "#/$defs/Function" } for i in range (num_functions )]
89
+
90
+ return {
91
+ "type" : "object" ,
92
+ "properties" : {
93
+ "id" : {"type" : "string" },
94
+ "function" : {
95
+ "anyOf" : function_refs
96
+ },
97
+ "type" : {"$ref" : "#/$defs/Type" }
98
+ },
99
+ "required" : ["id" , "function" , "type" ]
100
+ }
0 commit comments