mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-02 11:39:18 +00:00
Ignore self/cls from required args of class functions in convert_to_openai_tool (#20691)
Removed redundant self/cls from required args of class functions in _get_python_function_required_args: ```python class MemberTool: def search_member( self, keyword: str, *args, **kwargs, ): """Search on members with any keyword like first_name, last_name, email Args: keyword: Any keyword of member """ headers = dict(authorization=kwargs['token']) members = [] try: members = request_( method='SEARCH', url=f'{service_url}/apiv1/members', headers=headers, json=dict(query=keyword), ) except Exception as e: logger.info(e.__doc__) return members convert_to_openai_tool(MemberTool.search_member) ``` expected result: ``` {'type': 'function', 'function': {'name': 'search_member', 'description': 'Search on members with any keyword like first_name, last_name, username, email', 'parameters': {'type': 'object', 'properties': {'keyword': {'type': 'string', 'description': 'Any keyword of member'}}, 'required': ['keyword']}}} ``` #20685 --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
@@ -71,6 +71,29 @@ def json_schema() -> Dict:
|
||||
}
|
||||
|
||||
|
||||
class Dummy:
|
||||
def dummy_function(self, arg1: int, arg2: Literal["bar", "baz"]) -> None:
|
||||
"""dummy function
|
||||
|
||||
Args:
|
||||
arg1: foo
|
||||
arg2: one of 'bar', 'baz'
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class DummyWithClassMethod:
|
||||
@classmethod
|
||||
def dummy_function(cls, arg1: int, arg2: Literal["bar", "baz"]) -> None:
|
||||
"""dummy function
|
||||
|
||||
Args:
|
||||
arg1: foo
|
||||
arg2: one of 'bar', 'baz'
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def test_convert_to_openai_function(
|
||||
pydantic: Type[BaseModel],
|
||||
function: Callable,
|
||||
@@ -94,7 +117,15 @@ def test_convert_to_openai_function(
|
||||
},
|
||||
}
|
||||
|
||||
for fn in (pydantic, function, dummy_tool, json_schema, expected):
|
||||
for fn in (
|
||||
pydantic,
|
||||
function,
|
||||
dummy_tool,
|
||||
json_schema,
|
||||
expected,
|
||||
Dummy.dummy_function,
|
||||
DummyWithClassMethod.dummy_function,
|
||||
):
|
||||
actual = convert_to_openai_function(fn) # type: ignore
|
||||
assert actual == expected
|
||||
|
||||
|
Reference in New Issue
Block a user