Skip to content

Commit c5c4b98

Browse files
committed
fixes #30
1 parent b0bc2fd commit c5c4b98

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

01_funccall.ipynb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,9 @@
645645
"outputs": [],
646646
"source": [
647647
"#| exports\n",
648-
"def get_schema(f:callable, pname='input_schema')->dict:\n",
648+
"def get_schema(f:Union[callable,dict], pname='input_schema')->dict:\n",
649649
" \"Generate JSON schema for a class, function, or method\"\n",
650+
" if isinstance(f, dict): return f\n",
650651
" schema = _get_nested_schema(f)\n",
651652
" desc = f.__doc__\n",
652653
" assert desc, \"Docstring missing!\"\n",
@@ -1530,14 +1531,14 @@
15301531
"output_type": "stream",
15311532
"text": [
15321533
"Traceback (most recent call last):\n",
1533-
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_12053/775515280.py\", line 12, in python\n",
1534+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_46879/2963369439.py\", line 14, in python\n",
15341535
" try: return _run(code, glb, loc)\n",
15351536
" ^^^^^^^^^^^^^^^^^^^^\n",
1536-
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_12053/1858893181.py\", line 18, in _run\n",
1537+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_46879/1858893181.py\", line 18, in _run\n",
15371538
" try: exec(compiled_code, glb, loc)\n",
15381539
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
15391540
" File \"<ast>\", line 1, in <module>\n",
1540-
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_12053/775515280.py\", line 9, in handler\n",
1541+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_46879/2963369439.py\", line 9, in handler\n",
15411542
" def handler(*args): raise TimeoutError()\n",
15421543
" ^^^^^^^^^^^^^^^^^^^^\n",
15431544
"TimeoutError\n",
@@ -1858,7 +1859,7 @@
18581859
}
18591860
],
18601861
"source": [
1861-
"call_func('sums', {'a': 1, 'b': 2}, ns=[sums])"
1862+
"await call_func('sums', {'a': 1, 'b': 2}, ns=[sums])"
18621863
]
18631864
},
18641865
{
@@ -1879,7 +1880,7 @@
18791880
}
18801881
],
18811882
"source": [
1882-
"call_func('subs', {'a': 1, 'b': 2}, ns=mk_ns(d))"
1883+
"await call_func('subs', {'a': 1, 'b': 2}, ns=mk_ns(d))"
18831884
]
18841885
},
18851886
{
@@ -1932,7 +1933,9 @@
19321933
" \"Awaits the function `fc_name` with the given `fc_inputs` using namespace `ns`.\"\n",
19331934
" if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)\n",
19341935
" func = ns[fc_name]\n",
1935-
" return await func(**fc_inputs)"
1936+
" res = func(**fc_inputs)\n",
1937+
" if inspect.iscoroutine(res): res = await res\n",
1938+
" return res"
19361939
]
19371940
},
19381941
{

toolslm/funccall.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ def _get_nested_schema(obj):
112112
return schema
113113

114114
# %% ../01_funccall.ipynb 35
115-
def get_schema(f:callable, pname='input_schema')->dict:
115+
def get_schema(f:Union[callable,dict], pname='input_schema')->dict:
116116
"Generate JSON schema for a class, function, or method"
117+
if isinstance(f, dict): return f
117118
schema = _get_nested_schema(f)
118119
desc = f.__doc__
119120
assert desc, "Docstring missing!"
@@ -201,4 +202,6 @@ async def call_func_async(fc_name, fc_inputs, ns):
201202
"Awaits the function `fc_name` with the given `fc_inputs` using namespace `ns`."
202203
if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)
203204
func = ns[fc_name]
204-
return await func(**fc_inputs)
205+
res = func(**fc_inputs)
206+
if inspect.iscoroutine(res): res = await res
207+
return res

0 commit comments

Comments
 (0)