Skip to content

Commit b896302

Browse files
authored
Merge pull request #19 from AnswerDotAI/expanded_types
Minor: Add markdown comments to funccall `get_schema()` tests
2 parents f1cc504 + 17bee17 commit b896302

File tree

2 files changed

+87
-68
lines changed

2 files changed

+87
-68
lines changed

01_funccall.ipynb

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,52 +1049,37 @@
10491049
]
10501050
},
10511051
{
1052-
"cell_type": "code",
1053-
"execution_count": null,
1054-
"id": "ac8f3d19",
1052+
"cell_type": "markdown",
1053+
"id": "369320d4",
10551054
"metadata": {},
1056-
"outputs": [
1057-
{
1058-
"data": {
1059-
"text/plain": [
1060-
"{'name': '_optional_test',\n",
1061-
" 'description': 'Schema test for Optional / Union\\n\\nReturns:\\n- type: string',\n",
1062-
" 'input_schema': {'type': 'object',\n",
1063-
" 'properties': {'opt_tup': {'type': 'object',\n",
1064-
" 'description': '',\n",
1065-
" 'default': None,\n",
1066-
" 'anyOf': [{'type': 'array'}, {'type': 'null'}]}},\n",
1067-
" 'title': None}}"
1068-
]
1069-
},
1070-
"execution_count": null,
1071-
"metadata": {},
1072-
"output_type": "execute_result"
1073-
}
1074-
],
10751055
"source": [
1076-
"def _optional_test(opt_tup: Optional[Tuple[int, int]]=None) -> str:\n",
1077-
" \"Schema test for Optional / Union\"\n",
1078-
" return \"\"\n",
1079-
"get_schema(_optional_test)"
1056+
"### Additional `get_schema()` Test Cases"
1057+
]
1058+
},
1059+
{
1060+
"cell_type": "markdown",
1061+
"id": "a8052380",
1062+
"metadata": {},
1063+
"source": [
1064+
"Union types are approximately mapped to JSON schema 'anyOf' with two or more value types."
10801065
]
10811066
},
10821067
{
10831068
"cell_type": "code",
10841069
"execution_count": null,
1085-
"id": "825769a5",
1070+
"id": "6fc1d6f9",
10861071
"metadata": {},
10871072
"outputs": [
10881073
{
10891074
"data": {
10901075
"text/plain": [
10911076
"{'name': '_union_test',\n",
1092-
" 'description': 'Schema test for Union\\n\\nReturns:\\n- type: string',\n",
1077+
" 'description': 'Mandatory docstring',\n",
10931078
" 'input_schema': {'type': 'object',\n",
1094-
" 'properties': {'opt_tup': {'type': 'array',\n",
1079+
" 'properties': {'opt_tup': {'type': 'object',\n",
10951080
" 'description': '',\n",
1096-
" 'items': {'type': 'integer'},\n",
1097-
" 'default': None}},\n",
1081+
" 'default': None,\n",
1082+
" 'anyOf': [{'type': 'array'}, {'type': 'string'}, {'type': 'integer'}]}},\n",
10981083
" 'title': None}}"
10991084
]
11001085
},
@@ -1104,27 +1089,36 @@
11041089
}
11051090
],
11061091
"source": [
1107-
"def _union_test(opt_tup: Union[Tuple[int, int]]=None) -> str:\n",
1108-
" \"Schema test for Union\"\n",
1092+
"def _union_test(opt_tup: Union[Tuple[int, int], str, int]=None):\n",
1093+
" \"Mandatory docstring\"\n",
11091094
" return \"\"\n",
11101095
"get_schema(_union_test)"
11111096
]
11121097
},
1098+
{
1099+
"cell_type": "markdown",
1100+
"id": "7641aca8",
1101+
"metadata": {},
1102+
"source": [
1103+
"The new (Python 3.10+) union syntax can also be used, producing an equivalent schema."
1104+
]
1105+
},
11131106
{
11141107
"cell_type": "code",
11151108
"execution_count": null,
1116-
"id": "df9b9d13",
1109+
"id": "a1a11b3b",
11171110
"metadata": {},
11181111
"outputs": [
11191112
{
11201113
"data": {
11211114
"text/plain": [
11221115
"{'name': '_new_union_test',\n",
1123-
" 'description': 'Schema test for Union with the > 3.10 syntax\\n\\nReturns:\\n- type: string',\n",
1116+
" 'description': 'Mandatory docstring',\n",
11241117
" 'input_schema': {'type': 'object',\n",
11251118
" 'properties': {'opt_tup': {'type': 'object',\n",
11261119
" 'description': '',\n",
1127-
" 'default': None}},\n",
1120+
" 'default': None,\n",
1121+
" 'anyOf': [{'type': 'array'}, {'type': 'string'}, {'type': 'integer'}]}},\n",
11281122
" 'title': None}}"
11291123
]
11301124
},
@@ -1134,27 +1128,37 @@
11341128
}
11351129
],
11361130
"source": [
1137-
"def _new_union_test(opt_tup: str | None =None) -> str:\n",
1138-
" \"Schema test for Union with the > 3.10 syntax\"\n",
1139-
" return \"\"\n",
1131+
"def _new_union_test(opt_tup: Tuple[int, int] | str | int =None):\n",
1132+
" \"Mandatory docstring\"\n",
1133+
" pass\n",
11401134
"get_schema(_new_union_test)"
11411135
]
11421136
},
1137+
{
1138+
"cell_type": "markdown",
1139+
"id": "8d24cc0a",
1140+
"metadata": {},
1141+
"source": [
1142+
"Optional is a special case of union types, limited to two types, one of which is None (mapped to null in JSON schema):"
1143+
]
1144+
},
11431145
{
11441146
"cell_type": "code",
11451147
"execution_count": null,
1146-
"id": "41ad5756",
1148+
"id": "ac8f3d19",
11471149
"metadata": {},
11481150
"outputs": [
11491151
{
11501152
"data": {
11511153
"text/plain": [
1152-
"{'name': '_none_test',\n",
1153-
" 'description': 'Schema test for NoneType.',\n",
1154+
"{'name': '_optional_test',\n",
1155+
" 'description': 'Mandatory docstring',\n",
11541156
" 'input_schema': {'type': 'object',\n",
1155-
" 'properties': {'none': {'type': 'null', 'description': ''}},\n",
1156-
" 'title': None,\n",
1157-
" 'required': ['none']}}"
1157+
" 'properties': {'opt_tup': {'type': 'object',\n",
1158+
" 'description': '',\n",
1159+
" 'default': None,\n",
1160+
" 'anyOf': [{'type': 'array'}, {'type': 'null'}]}},\n",
1161+
" 'title': None}}"
11581162
]
11591163
},
11601164
"execution_count": null,
@@ -1163,10 +1167,18 @@
11631167
}
11641168
],
11651169
"source": [
1166-
"def _none_test(none: type(None)):\n",
1167-
" \"Schema test for NoneType.\"\n",
1170+
"def _optional_test(opt_tup: Optional[Tuple[int, int]]=None):\n",
1171+
" \"Mandatory docstring\"\n",
11681172
" pass\n",
1169-
"get_schema(_none_test)"
1173+
"get_schema(_optional_test)"
1174+
]
1175+
},
1176+
{
1177+
"cell_type": "markdown",
1178+
"id": "c969721b",
1179+
"metadata": {},
1180+
"source": [
1181+
"Containers can also be used, both in their parameterized form (`List[int]`) or as their unparameterized raw type (`List`). In the latter case, the item type is mapped to `object` in JSON schema."
11701182
]
11711183
},
11721184
{
@@ -1179,7 +1191,7 @@
11791191
"data": {
11801192
"text/plain": [
11811193
"{'name': '_list_test',\n",
1182-
" 'description': 'Schema test for List\\n\\nReturns:\\n- type: string',\n",
1194+
" 'description': 'Mandatory docstring',\n",
11831195
" 'input_schema': {'type': 'object',\n",
11841196
" 'properties': {'l': {'type': 'array',\n",
11851197
" 'description': '',\n",
@@ -1194,9 +1206,9 @@
11941206
}
11951207
],
11961208
"source": [
1197-
"def _list_test(l: List[int]) -> str:\n",
1198-
" \"Schema test for List\"\n",
1199-
" return \"\"\n",
1209+
"def _list_test(l: List[int]):\n",
1210+
" \"Mandatory docstring\"\n",
1211+
" pass\n",
12001212
"get_schema(_list_test)"
12011213
]
12021214
},
@@ -1210,7 +1222,7 @@
12101222
"data": {
12111223
"text/plain": [
12121224
"{'name': '_raw_list_test',\n",
1213-
" 'description': 'Schema test for List\\n\\nReturns:\\n- type: string',\n",
1225+
" 'description': 'Mandatory docstring',\n",
12141226
" 'input_schema': {'type': 'object',\n",
12151227
" 'properties': {'l': {'type': 'array',\n",
12161228
" 'description': '',\n",
@@ -1225,12 +1237,20 @@
12251237
}
12261238
],
12271239
"source": [
1228-
"def _raw_list_test(l: List) -> str:\n",
1229-
" \"Schema test for List\"\n",
1230-
" return \"\"\n",
1240+
"def _raw_list_test(l: List):\n",
1241+
" \"Mandatory docstring\"\n",
1242+
" pass\n",
12311243
"get_schema(_raw_list_test)"
12321244
]
12331245
},
1246+
{
1247+
"cell_type": "markdown",
1248+
"id": "5704c197",
1249+
"metadata": {},
1250+
"source": [
1251+
"The same applies to dictionary, which can similarly be parameterized with key/value types or specified as a raw type."
1252+
]
1253+
},
12341254
{
12351255
"cell_type": "code",
12361256
"execution_count": null,
@@ -1241,7 +1261,7 @@
12411261
"data": {
12421262
"text/plain": [
12431263
"{'name': '_dict_test',\n",
1244-
" 'description': 'Schema test for Dict.',\n",
1264+
" 'description': 'Mandatory docstring',\n",
12451265
" 'input_schema': {'type': 'object',\n",
12461266
" 'properties': {'d': {'type': 'object',\n",
12471267
" 'description': '',\n",
@@ -1256,8 +1276,8 @@
12561276
}
12571277
],
12581278
"source": [
1259-
"def _dict_test(d: Dict[int, int]):\n",
1260-
" \"Schema test for Dict.\"\n",
1279+
"def _dict_test(d: Dict[str, int]):\n",
1280+
" \"Mandatory docstring\"\n",
12611281
" pass\n",
12621282
"get_schema(_dict_test)"
12631283
]
@@ -1272,7 +1292,7 @@
12721292
"data": {
12731293
"text/plain": [
12741294
"{'name': '_raw_dict_test',\n",
1275-
" 'description': 'Schema test for Dict.',\n",
1295+
" 'description': 'Mandatory docstring',\n",
12761296
" 'input_schema': {'type': 'object',\n",
12771297
" 'properties': {'d': {'type': 'object', 'description': ''}},\n",
12781298
" 'title': None,\n",
@@ -1286,8 +1306,7 @@
12861306
],
12871307
"source": [
12881308
"def _raw_dict_test(d: Dict):\n",
1289-
" \"Schema test for Dict.\"\n",
1290-
" pass\n",
1309+
" \"Mandatory docstring\"\n",
12911310
"get_schema(_raw_dict_test)"
12921311
]
12931312
},

toolslm/funccall.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ def PathArg(
112112
path: str # A filesystem path
113113
): return Path(path)
114114

115-
# %% ../01_funccall.ipynb 61
115+
# %% ../01_funccall.ipynb 66
116116
import ast, time, signal, traceback
117117
from fastcore.utils import *
118118

119-
# %% ../01_funccall.ipynb 62
119+
# %% ../01_funccall.ipynb 67
120120
def _copy_loc(new, orig):
121121
"Copy location information from original node to new node and all children."
122122
new = ast.copy_location(new, orig)
@@ -125,7 +125,7 @@ def _copy_loc(new, orig):
125125
elif isinstance(o, list): setattr(new, field, [_copy_loc(value, orig) for value in o])
126126
return new
127127

128-
# %% ../01_funccall.ipynb 64
128+
# %% ../01_funccall.ipynb 69
129129
def _run(code:str ):
130130
"Run `code`, returning final expression (similar to IPython)"
131131
tree = ast.parse(code)
@@ -148,7 +148,7 @@ def _run(code:str ):
148148
if _result is not None: return _result
149149
return stdout_buffer.getvalue().strip()
150150

151-
# %% ../01_funccall.ipynb 69
151+
# %% ../01_funccall.ipynb 74
152152
def python(code, # Code to execute
153153
timeout=5 # Maximum run time in seconds before a `TimeoutError` is raised
154154
): # Result of last node, if it's an expression, or `None` otherwise
@@ -161,7 +161,7 @@ def handler(*args): raise TimeoutError()
161161
except Exception as e: return traceback.format_exc()
162162
finally: signal.alarm(0)
163163

164-
# %% ../01_funccall.ipynb 76
164+
# %% ../01_funccall.ipynb 81
165165
def mk_ns(*funcs_or_objs):
166166
merged = {}
167167
for o in funcs_or_objs:
@@ -170,7 +170,7 @@ def mk_ns(*funcs_or_objs):
170170
if callable(o) and hasattr(o, '__name__'): merged |= {o.__name__: o}
171171
return merged
172172

173-
# %% ../01_funccall.ipynb 85
173+
# %% ../01_funccall.ipynb 90
174174
def call_func(fc_name, fc_inputs, ns):
175175
"Call the function `fc_name` with the given `fc_inputs` using namespace `ns`."
176176
if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)

0 commit comments

Comments
 (0)