improve tools (#6062)

This commit is contained in:
Harrison Chase
2023-06-12 22:19:03 -07:00
committed by GitHub
parent 5b6bbf4ab2
commit ec1a2adf9c
2 changed files with 75 additions and 14 deletions

View File

@@ -315,6 +315,39 @@ def test_tool_lambda_args_schema() -> None:
assert tool.args == expected_args
def test_structured_tool_from_function_docstring() -> None:
"""Test that structured tools can be created from functions."""
def foo(bar: int, baz: str) -> str:
"""Docstring
Args:
bar: int
baz: str
"""
raise NotImplementedError()
structured_tool = StructuredTool.from_function(foo)
assert structured_tool.name == "foo"
assert structured_tool.args == {
"bar": {"title": "Bar", "type": "integer"},
"baz": {"title": "Baz", "type": "string"},
}
assert structured_tool.args_schema.schema() == {
"properties": {
"bar": {"title": "Bar", "type": "integer"},
"baz": {"title": "Baz", "type": "string"},
},
"title": "fooSchemaSchema",
"type": "object",
"required": ["bar", "baz"],
}
prefix = "foo(bar: int, baz: str) -> str - "
assert foo.__doc__ is not None
assert structured_tool.description == prefix + foo.__doc__.strip()
def test_structured_tool_lambda_multi_args_schema() -> None:
"""Test args schema inference when the tool argument is a lambda function."""
tool = StructuredTool.from_function(
@@ -577,12 +610,13 @@ def test_structured_tool_from_function() -> None:
}
assert structured_tool.args_schema.schema() == {
"title": "fooSchemaSchema",
"type": "object",
"properties": {
"bar": {"title": "Bar", "type": "integer"},
"baz": {"title": "Baz", "type": "string"},
},
"title": "fooSchemaSchema",
"type": "object",
"required": ["bar", "baz"],
}
prefix = "foo(bar: int, baz: str) -> str - "