-
Notifications
You must be signed in to change notification settings - Fork 15.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: improved method tools #28695
base: master
Are you sure you want to change the base?
core: improved method tools #28695
Conversation
Add MethodTool descriptor Extend StructuredTool to use `outer_instance`s Extend `tool` decorator for creating method tools Add test cases
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! a few suggestions and questions
@@ -421,3 +429,15 @@ def invoke_wrapper(callbacks: Optional[Callbacks] = None, **kwargs: Any) -> Any: | |||
description=description, | |||
args_schema=args_schema, | |||
) | |||
|
|||
|
|||
class MethodTool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this a function instead of a class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see - this maybe solves the issue of typing. going to suggest some ideas - not sure if they work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class is needed for classmethod support. I have created a wrapper function methodtool
that simply returns MethodTool(func)
. How's that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it needed for classmethod support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mentioned in another reply:
With the way the
__get__
descriptor worksowner
is for the class itself andinstance
is for the instance of the class.
Keep in mind that in your other solution (a function that returns a property
), you are still returning a descriptor class. That class, property
. works for our regular method tool case, but it will not work for class methods. Notice in the MethodTool
class that it will pass in owner
(class itself) for outer_instance
with classmethods, and instance
(class instance) for regular methods. Since the property
class is not enough for our use, I had to create a custom descriptor that did exactly what we wanted it to.
"Why could we not do classmethod(property(func))
for classmethods?" you may ask. That is because classmethod
no longer wraps descriptors as of Python 3.13.
If, for syntax reasons, you would like the decorator to be a function that is something that I have added to the code. The function is simply a wrapper for the MethodTool
class since that overloaded __get__
method is needed, but the MethodTool
class never needs to be exposed to client code.
class MethodTool: | ||
"""A descriptor that converts a method into a tool.""" | ||
|
||
def __init__(self, func: Union[Callable, classmethod]) -> None: | ||
self.func = func | ||
|
||
def __get__(self, instance: Any, owner: Any) -> BaseTool: | ||
if isinstance(self.func, classmethod): | ||
return tool(self.func.__func__, outer_instance=owner) | ||
return tool(self.func, outer_instance=instance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class MethodTool: | |
"""A descriptor that converts a method into a tool.""" | |
def __init__(self, func: Union[Callable, classmethod]) -> None: | |
self.func = func | |
def __get__(self, instance: Any, owner: Any) -> BaseTool: | |
if isinstance(self.func, classmethod): | |
return tool(self.func.__func__, outer_instance=owner) | |
return tool(self.func, outer_instance=instance) | |
def toolmethod(f: callable) -> property: | |
"""A decorator that converts a method into a tool.""" | |
def inner_f(outer_instance) -> BaseTool: | |
tool_f = functools.partial(f, outer_instance) # this substitutes in for self | |
return tool(tool_f) | |
return property(inner_f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this leaves out what was being handled by owner vs instance for outer_instance
- what is that doing for us?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was to allow the support for classmethods. With the way the __get__
descriptor works owner
is for the class itself and instance
is for the instance of the class.
Instead of passing `outer_instance` into `tool` and then saving in `StructuredTool`, the work can be done completely within the `MethodTool` class itself.
Fixed lack of description and added a test case to check the tool metadata
Add `methodtool` function to take the place of `MethodTool` class as the decorator. Also change the name of one of the test cases to not be confused with the actual metadata field
"""Add two numbers to c.""" | ||
return a + b + cls.c | ||
|
||
assert "cls" not in A.foo.args |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert "cls" not in A.foo.args | |
assert "cls" not in A.foo.args | |
class A: | |
... | |
class TestStandardAUnit(BaseToolUnitTests): | |
... | |
class TestStandardAIntegration(BaseToolIntegrationTests): | |
... |
would also be good to implement some of these as standard tests - it tests a few of the exhaustive cases: https://python.langchain.com/docs/contributing/how_to/integrations/standard_tests/
sorry this is dragging on a bit! appreciate your help!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems cool, I've never heard of this before and would like to give it a try since it could make the tests more concise. But I am having trouble putting them into the test_tools.py
file. The issue comes from being unable to import the ToolsUnitTests
class.
also before starting too much work @baskaryan had a suggestion for how to document this behavior without needing code changes. He smartly suggested that the underlying issue with decorating methods with anything is that it incorrectly decorates |
Sorry, I don't think I understand. I get that the decorator does decorate However, since we know we can create this decorator which does the job we want and works with a very similar interface to the current |
Add tests for seeing if the methods work for methods and classmethods not using "self" and "cls" specifically Needed to ignore some ruff rules in order for these tests
Also remove now unnecessary tests
hey @ethanglide, appreciate the pr! i have a strong bias against adding more interfaces that sound/seem very similar and have similar functionality. thoughts on documenting this type of usage of the existing @tool instead of introducing another decorator? from langchain_core.tools import tool, BaseTool
class A:
def __init__(self, c: int):
self.c = c
def _foo(self, a: int, b: int) -> int:
"""Add two numbers to c."""
return a + b + self.c
@property
def foo(self) -> BaseTool:
return tool(self._foo)
a = A(10)
a.foo.invoke({"a": 1, "b": 2}) # 13 |
Add functionality for transforming methods and classmethods into tools. This is an improvement over the implementation in #28160.
Resolves #27471
Example:
Example with
classmethod
:This implementation requires the programmer to specify
@MethodTool
rather than using the@tool
decorator, which allows for a much better implementation which gets over a lot of shortcomings of my previous attempt in #28160. Some improvements are as follows:self
orcls
, it can be any nameouter_instance
is no longer a "claimed" parameter name. Parameters can be of any nameclassmethod
s are fully supported