Skip to content

Commit

Permalink
add dependencies and add support for args without type
Browse files Browse the repository at this point in the history
  • Loading branch information
BenderV committed Sep 9, 2024
1 parent 6f651ce commit 09780e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
20 changes: 20 additions & 0 deletions autochat/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def test_plot_widget(self):
self.assertDictEqual(result, expected)


def empty_function(test): # No type hinting, no docstring
return "empty"


def sums(a: int, b: int = 1):
"""Adds a + b"""
return a + b
Expand All @@ -201,6 +205,22 @@ def test_inspect_schema(self):
}
self.assertEqual(result, expected)

def test_inspect_schema_empty(self):
result = inspect_schema(empty_function)
expected = {
"name": "empty_function",
"description": None,
"parameters": {
"properties": {
"test": {"title": "Test"},
},
"required": ["test"],
"title": "Input for `empty_function`",
"type": "object",
},
}
self.assertEqual(result, expected)


if __name__ == "__main__":
unittest.main()
23 changes: 15 additions & 8 deletions autochat/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import ast
import csv
import inspect
import json
import re
from typing import Any
import typing
from inspect import Parameter
from io import StringIO

from pydantic import create_model
from pydantic import BaseConfig, create_model

from autochat.model import Message

Expand Down Expand Up @@ -205,10 +204,18 @@ def parse_chat_template(filename) -> list[Message]:
return instruction, examples


class AllowNonTypedParamsConfig(BaseConfig):
arbitrary_types_allowed = True


def inspect_schema(f):
kw = {
n: (o.annotation, ... if o.default == Parameter.empty else o.default)
for n, o in inspect.signature(f).parameters.items()
}
s = create_model(f"Input for `{f.__name__}`", **kw).schema()
kw = {}
for n, o in inspect.signature(f).parameters.items():
annotation = o.annotation if o.annotation != Parameter.empty else Any
default = ... if o.default == Parameter.empty else o.default
kw[n] = (annotation, default)

s = create_model(
f"Input for `{f.__name__}`", __config__=AllowNonTypedParamsConfig, **kw
).schema()
return dict(name=f.__name__, description=f.__doc__, parameters=s)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name="autochat",
version="0.3.2",
packages=find_packages(),
install_requires=["tenacity==8.3.0"],
install_requires=["tenacity==8.3.0", "pillow==10.4.0"],
extras_require={
"anthropic": ["anthropic==0.34.2"],
"openai": ["openai==1.26.0"],
Expand Down

0 comments on commit 09780e5

Please sign in to comment.