Skip to content

Commit a442bd0

Browse files
authored
Merge pull request #27 from mikonapoli/main
Adds `call_func_async`
2 parents d5bc47f + 41fa15e commit a442bd0

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

01_funccall.ipynb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,80 @@
17981798
"call_func('subs', {'a': 1, 'b': 2}, ns=mk_ns(d))"
17991799
]
18001800
},
1801+
{
1802+
"cell_type": "markdown",
1803+
"id": "591574b8-6b53-4908-8159-b87be42133f7",
1804+
"metadata": {},
1805+
"source": [
1806+
"### Async function calling"
1807+
]
1808+
},
1809+
{
1810+
"cell_type": "markdown",
1811+
"id": "96a3a7d3-31ef-4cc6-b47c-35eaa8bbff8b",
1812+
"metadata": {},
1813+
"source": [
1814+
"Since tools defined by MCP servers are async function, it is probably a good idea to have an async version of `call_func`."
1815+
]
1816+
},
1817+
{
1818+
"cell_type": "code",
1819+
"execution_count": null,
1820+
"id": "e273507b-6e4b-40bb-ae23-6397e89a4d51",
1821+
"metadata": {},
1822+
"outputs": [
1823+
{
1824+
"data": {
1825+
"text/plain": [
1826+
"{'asums': <function __main__.asums(a, b)>}"
1827+
]
1828+
},
1829+
"execution_count": null,
1830+
"metadata": {},
1831+
"output_type": "execute_result"
1832+
}
1833+
],
1834+
"source": [
1835+
"async def asums(a, b): return a + b\n",
1836+
"ns = mk_ns(asums); ns"
1837+
]
1838+
},
1839+
{
1840+
"cell_type": "code",
1841+
"execution_count": null,
1842+
"id": "7ac04e80-7bb9-4b52-8285-454684605d47",
1843+
"metadata": {},
1844+
"outputs": [],
1845+
"source": [
1846+
"#| exports\n",
1847+
"async def call_func_async(fc_name, fc_inputs, ns):\n",
1848+
" \"Awaits the function `fc_name` with the given `fc_inputs` using namespace `ns`.\"\n",
1849+
" if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)\n",
1850+
" func = ns[fc_name]\n",
1851+
" return await func(**fc_inputs)"
1852+
]
1853+
},
1854+
{
1855+
"cell_type": "code",
1856+
"execution_count": null,
1857+
"id": "b83998ac-68e2-4dbe-b594-65fb4fdf59b8",
1858+
"metadata": {},
1859+
"outputs": [
1860+
{
1861+
"data": {
1862+
"text/plain": [
1863+
"3"
1864+
]
1865+
},
1866+
"execution_count": null,
1867+
"metadata": {},
1868+
"output_type": "execute_result"
1869+
}
1870+
],
1871+
"source": [
1872+
"await call_func_async('asums', {'a': 1, 'b': 2}, ns=[asums])"
1873+
]
1874+
},
18011875
{
18021876
"cell_type": "markdown",
18031877
"id": "94ec4289",

toolslm/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'toolslm.funccall._run': ('funccall.html#_run', 'toolslm/funccall.py'),
2727
'toolslm.funccall._types': ('funccall.html#_types', 'toolslm/funccall.py'),
2828
'toolslm.funccall.call_func': ('funccall.html#call_func', 'toolslm/funccall.py'),
29+
'toolslm.funccall.call_func_async': ('funccall.html#call_func_async', 'toolslm/funccall.py'),
2930
'toolslm.funccall.get_schema': ('funccall.html#get_schema', 'toolslm/funccall.py'),
3031
'toolslm.funccall.mk_ns': ('funccall.html#mk_ns', 'toolslm/funccall.py'),
3132
'toolslm.funccall.python': ('funccall.html#python', 'toolslm/funccall.py')},

toolslm/funccall.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../01_funccall.ipynb.
22

33
# %% auto 0
4-
__all__ = ['empty', 'custom_types', 'get_schema', 'PathArg', 'python', 'mk_ns', 'call_func']
4+
__all__ = ['empty', 'custom_types', 'get_schema', 'PathArg', 'python', 'mk_ns', 'call_func', 'call_func_async']
55

66
# %% ../01_funccall.ipynb 2
77
import inspect
@@ -191,3 +191,10 @@ def call_func(fc_name, fc_inputs, ns):
191191
if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)
192192
func = ns[fc_name]
193193
return func(**fc_inputs)
194+
195+
# %% ../01_funccall.ipynb 97
196+
async def call_func_async(fc_name, fc_inputs, ns):
197+
"Awaits the function `fc_name` with the given `fc_inputs` using namespace `ns`."
198+
if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)
199+
func = ns[fc_name]
200+
return await func(**fc_inputs)

0 commit comments

Comments
 (0)