Skip to content

Commit

Permalink
Merge pull request #13 from AnswerDotAI/class_methods
Browse files Browse the repository at this point in the history
Add check for 'self' property in _get_nested_schema
  • Loading branch information
ncoop57 authored Oct 16, 2024
2 parents 6289f1c + 0ff5ed1 commit 7662446
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
66 changes: 64 additions & 2 deletions 01_funccall.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
" props, req, defs = {}, {}, {}\n",
" \n",
" for n, o in d.items():\n",
" if n != 'return':\n",
" if n != 'return' and n != 'self':\n",
" _process_property(n, o, props, req, defs)\n",
" \n",
" schema = dict(type='object', properties=props, title=obj.__name__ if isinstance(obj, type) else None)\n",
Expand Down Expand Up @@ -577,6 +577,53 @@
"get_schema(silly_test)"
]
},
{
"cell_type": "markdown",
"id": "e3f36f8a",
"metadata": {},
"source": [
"This also works with class methods:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05d33447",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'sums',\n",
" 'description': 'Adds a + b.\\n\\nReturns:\\n- type: integer',\n",
" 'input_schema': {'type': 'object',\n",
" 'properties': {'a': {'type': 'integer', 'description': 'First thing to sum'},\n",
" 'b': {'type': 'integer',\n",
" 'description': 'Second thing to sum',\n",
" 'default': 1}},\n",
" 'title': None,\n",
" 'required': ['a']}}"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Dummy:\n",
" def sums(\n",
" self,\n",
" a:int, # First thing to sum\n",
" b:int=1 # Second thing to sum\n",
" ) -> int: # The sum of the inputs\n",
" \"Adds a + b.\"\n",
" print(f\"Finding the sum of {a} and {b}\")\n",
" return a + b\n",
"\n",
"get_schema(Dummy.sums)"
]
},
{
"cell_type": "markdown",
"id": "ae3fdfa4",
Expand Down Expand Up @@ -950,7 +997,22 @@
"execution_count": null,
"id": "1e9ee5c1",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/nathan/git/.venv/lib/python3.10/site-packages/nbdev/export.py:80: UserWarning: Notebook '/Users/nathan/git/toolslm/01_funccall-aimagic-2.ipynb' uses `#|export` without `#|default_exp` cell.\n",
"Note nbdev2 no longer supports nbdev1 syntax. Run `nbdev_migrate` to upgrade.\n",
"See https://nbdev.fast.ai/getting_started.html for more information.\n",
" warn(f\"Notebook '{nbname}' uses `#|export` without `#|default_exp` cell.\\n\"\n",
"/Users/nathan/git/.venv/lib/python3.10/site-packages/nbdev/export.py:80: UserWarning: Notebook '/Users/nathan/git/toolslm/01_funccall-aimagic.ipynb' uses `#|export` without `#|default_exp` cell.\n",
"Note nbdev2 no longer supports nbdev1 syntax. Run `nbdev_migrate` to upgrade.\n",
"See https://nbdev.fast.ai/getting_started.html for more information.\n",
" warn(f\"Notebook '{nbname}' uses `#|export` without `#|default_exp` cell.\\n\"\n"
]
}
],
"source": [
"#|hide\n",
"#|eval: false\n",
Expand Down
10 changes: 5 additions & 5 deletions toolslm/funccall.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _get_nested_schema(obj):
props, req, defs = {}, {}, {}

for n, o in d.items():
if n != 'return':
if n != 'return' and n != 'self':
_process_property(n, o, props, req, defs)

schema = dict(type='object', properties=props, title=obj.__name__ if isinstance(obj, type) else None)
Expand All @@ -95,11 +95,11 @@ def get_schema(f:callable, pname='input_schema')->dict:
if ret.anno is not empty: desc += f'\n\nReturns:\n- type: {_types(ret.anno)[0]}'
return {"name": f.__name__, "description": desc, pname: schema}

# %% ../01_funccall.ipynb 37
# %% ../01_funccall.ipynb 39
import ast, time, signal, traceback
from fastcore.utils import *

# %% ../01_funccall.ipynb 38
# %% ../01_funccall.ipynb 40
def _copy_loc(new, orig):
"Copy location information from original node to new node and all children."
new = ast.copy_location(new, orig)
Expand All @@ -108,7 +108,7 @@ def _copy_loc(new, orig):
elif isinstance(o, list): setattr(new, field, [_copy_loc(value, orig) for value in o])
return new

# %% ../01_funccall.ipynb 40
# %% ../01_funccall.ipynb 42
def _run(code:str ):
"Run `code`, returning final expression (similar to IPython)"
tree = ast.parse(code)
Expand All @@ -131,7 +131,7 @@ def _run(code:str ):
if _result is not None: return _result
return stdout_buffer.getvalue().strip()

# %% ../01_funccall.ipynb 45
# %% ../01_funccall.ipynb 47
def python(code, # Code to execute
timeout=5 # Maximum run time in seconds before a `TimeoutError` is raised
): # Result of last node, if it's an expression, or `None` otherwise
Expand Down

0 comments on commit 7662446

Please # to comment.