-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtest_pydantic.py
310 lines (287 loc) · 12.2 KB
/
test_pydantic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from __future__ import annotations
from enum import Enum
from typing import Any, Dict
from pydantic import Field, BaseModel, ConfigDict
from inline_snapshot import snapshot
import openai
from openai._compat import PYDANTIC_V2
from .schema_types.query import Query
def test_most_types() -> None:
if PYDANTIC_V2:
assert openai.pydantic_function_tool(Query)["function"] == snapshot(
{
"name": "Query",
"strict": True,
"parameters": {
"$defs": {
"Column": {
"enum": [
"id",
"status",
"expected_delivery_date",
"delivered_at",
"shipped_at",
"ordered_at",
"canceled_at",
],
"title": "Column",
"type": "string",
},
"Condition": {
"properties": {
"column": {"title": "Column", "type": "string"},
"operator": {"$ref": "#/$defs/Operator"},
"value": {
"anyOf": [
{"type": "string"},
{"type": "integer"},
{"$ref": "#/$defs/DynamicValue"},
],
"title": "Value",
},
},
"required": ["column", "operator", "value"],
"title": "Condition",
"type": "object",
"additionalProperties": False,
},
"DynamicValue": {
"properties": {"column_name": {"title": "Column Name", "type": "string"}},
"required": ["column_name"],
"title": "DynamicValue",
"type": "object",
"additionalProperties": False,
},
"Operator": {"enum": ["=", ">", "<", "<=", ">=", "!="], "title": "Operator", "type": "string"},
"OrderBy": {"enum": ["asc", "desc"], "title": "OrderBy", "type": "string"},
"Table": {"enum": ["orders", "customers", "products"], "title": "Table", "type": "string"},
},
"properties": {
"name": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Name"},
"table_name": {"$ref": "#/$defs/Table"},
"columns": {
"items": {"$ref": "#/$defs/Column"},
"title": "Columns",
"type": "array",
},
"conditions": {
"items": {"$ref": "#/$defs/Condition"},
"title": "Conditions",
"type": "array",
},
"order_by": {"$ref": "#/$defs/OrderBy"},
},
"required": ["name", "table_name", "columns", "conditions", "order_by"],
"title": "Query",
"type": "object",
"additionalProperties": False,
},
}
)
else:
assert openai.pydantic_function_tool(Query)["function"] == snapshot(
{
"name": "Query",
"strict": True,
"parameters": {
"title": "Query",
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"table_name": {"$ref": "#/definitions/Table"},
"columns": {"type": "array", "items": {"$ref": "#/definitions/Column"}},
"conditions": {
"title": "Conditions",
"type": "array",
"items": {"$ref": "#/definitions/Condition"},
},
"order_by": {"$ref": "#/definitions/OrderBy"},
},
"required": ["name", "table_name", "columns", "conditions", "order_by"],
"definitions": {
"Table": {
"title": "Table",
"description": "An enumeration.",
"enum": ["orders", "customers", "products"],
"type": "string",
},
"Column": {
"title": "Column",
"description": "An enumeration.",
"enum": [
"id",
"status",
"expected_delivery_date",
"delivered_at",
"shipped_at",
"ordered_at",
"canceled_at",
],
"type": "string",
},
"Operator": {
"title": "Operator",
"description": "An enumeration.",
"enum": ["=", ">", "<", "<=", ">=", "!="],
"type": "string",
},
"DynamicValue": {
"title": "DynamicValue",
"type": "object",
"properties": {"column_name": {"title": "Column Name", "type": "string"}},
"required": ["column_name"],
"additionalProperties": False,
},
"Condition": {
"title": "Condition",
"type": "object",
"properties": {
"column": {"title": "Column", "type": "string"},
"operator": {"$ref": "#/definitions/Operator"},
"value": {
"title": "Value",
"anyOf": [
{"type": "string"},
{"type": "integer"},
{"$ref": "#/definitions/DynamicValue"},
],
},
},
"required": ["column", "operator", "value"],
"additionalProperties": False,
},
"OrderBy": {
"title": "OrderBy",
"description": "An enumeration.",
"enum": ["asc", "desc"],
"type": "string",
},
},
"additionalProperties": False,
},
}
)
class Color(Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
class ColorDetection(BaseModel):
color: Color = Field(description="The detected color")
hex_color_code: str = Field(description="The hex color code of the detected color")
def test_enums() -> None:
if PYDANTIC_V2:
assert openai.pydantic_function_tool(ColorDetection)["function"] == snapshot(
{
"name": "ColorDetection",
"strict": True,
"parameters": {
"$defs": {"Color": {"enum": ["red", "blue", "green"], "title": "Color", "type": "string"}},
"properties": {
"color": {
"description": "The detected color",
"enum": ["red", "blue", "green"],
"title": "Color",
"type": "string",
},
"hex_color_code": {
"description": "The hex color code of the detected color",
"title": "Hex Color Code",
"type": "string",
},
},
"required": ["color", "hex_color_code"],
"title": "ColorDetection",
"type": "object",
"additionalProperties": False,
},
}
)
else:
assert openai.pydantic_function_tool(ColorDetection)["function"] == snapshot(
{
"name": "ColorDetection",
"strict": True,
"parameters": {
"properties": {
"color": {
"description": "The detected color",
"title": "Color",
"enum": ["red", "blue", "green"],
},
"hex_color_code": {
"description": "The hex color code of the detected color",
"title": "Hex Color Code",
"type": "string",
},
},
"required": ["color", "hex_color_code"],
"title": "ColorDetection",
"definitions": {
"Color": {"title": "Color", "description": "An enumeration.", "enum": ["red", "blue", "green"]}
},
"type": "object",
"additionalProperties": False,
},
}
)
class GenerateToolCallArguments(BaseModel):
arguments: Dict[str, Any] = Field(description="The arguments to pass to the tool")
def test_dictionaries():
# JSON schema definitions for this pydantic model are the same in Pydantic 1.x and 2.x
assert openai.pydantic_function_tool(GenerateToolCallArguments)["function"] == snapshot(
{
'name': 'GenerateToolCallArguments',
'parameters': {
'additionalProperties': False,
'properties': {
'arguments': {
'description': 'The arguments to pass to the tool',
'title': 'Arguments',
'type': 'object',
},
},
'required': [
'arguments',
],
'title': 'GenerateToolCallArguments',
'type': 'object',
},
'strict': True,
}
)
class EmptyAllowExtras(BaseModel):
pass
if PYDANTIC_V2:
class EmptyForbidExtras(BaseModel):
model_config = ConfigDict(extra="forbid")
else:
class EmptyForbidExtras(BaseModel):
class Config:
extra = "forbid"
def test_empty_objects():
# JSON schema definitions for these pydantic models are the same in Pydantic 1.x and 2.x
assert openai.pydantic_function_tool(EmptyAllowExtras)["function"] == snapshot(
{
'name': 'EmptyAllowExtras',
'parameters': {
'properties': {},
'required': [],
'title': 'EmptyAllowExtras',
'type': 'object',
},
'strict': True,
}
)
assert openai.pydantic_function_tool(EmptyForbidExtras)["function"] == snapshot(
{
'name': 'EmptyForbidExtras',
'parameters': {
'additionalProperties': False,
'properties': {},
'required': [],
'title': 'EmptyForbidExtras',
'type': 'object',
},
'strict': True,
}
)