Skip to content
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

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft

Conversation

ethanglide
Copy link

Add functionality for transforming methods and classmethods into tools. This is an improvement over the implementation in #28160.

Resolves #27471

Example:

class A:
    def __init__(self, c: int):
        self.c = c

    @MethodTool
    def foo(self, a: int, b: int) -> int:
        """Add two numbers to c."""
        return a + b + self.c

a = A(10)
a.foo.invoke({"a": 1, "b": 2}) # 13

Example with classmethod:

class A:
    c = 10
  
    @MethodTool
    @classmethod
    def foo(cls, a: int, b: int) -> int:
        """Add two numbers to c."""
        return a + b + cls.c

A.foo.invoke({"a": 1, "b": 2}) # 13

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:

  • The first parameter of the method is not required to be named self or cls, it can be any name
  • The argument name outer_instance is no longer a "claimed" parameter name. Parameters can be of any name
  • classmethods are fully supported

Add MethodTool descriptor
Extend StructuredTool to use `outer_instance`s
Extend `tool` decorator for creating method tools
Add test cases
Copy link

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 14, 2024 2:02am

@ethanglide ethanglide marked this pull request as ready for review December 12, 2024 21:35
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 12, 2024
@ethanglide ethanglide mentioned this pull request Dec 12, 2024
@dosubot dosubot bot added the langchain Related to the langchain package label Dec 12, 2024
@efriis efriis self-assigned this Dec 12, 2024
Copy link
Member

@efriis efriis left a 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

libs/core/langchain_core/tools/__init__.py Outdated Show resolved Hide resolved
@@ -421,3 +429,15 @@ def invoke_wrapper(callbacks: Optional[Callbacks] = None, **kwargs: Any) -> Any:
description=description,
args_schema=args_schema,
)


class MethodTool:
Copy link
Member

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?

Copy link
Member

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

Copy link
Author

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?

Copy link
Member

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?

Copy link
Author

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 works owner is for the class itself and instance 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.

libs/core/langchain_core/tools/structured.py Outdated Show resolved Hide resolved
libs/core/tests/unit_tests/test_tools.py Outdated Show resolved Hide resolved
libs/core/tests/unit_tests/test_tools.py Show resolved Hide resolved
libs/core/tests/unit_tests/test_tools.py Show resolved Hide resolved
Comment on lines 434 to 443
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link
Member

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?

Copy link
Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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!

Copy link
Author

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.

@efriis
Copy link
Member

efriis commented Dec 14, 2024

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 Class.method instead of the object's self.method instance, so it may be better to just document that instead, with a way of adding a separate property/method that generates that for you if you so wish

@ethanglide
Copy link
Author

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 Class.method instead of the object's self.method instance, so it may be better to just document that instead, with a way of adding a separate property/method that generates that for you if you so wish

Sorry, I don't think I understand. I get that the decorator does decorate Class.method instead of self.method, but with the way things are currently set up even if you decorated the method within the constructor (which would decorate self.method) you would still be unable to use the tool because of the conflicting names of the self parameter. There would need to still be code changes.

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 @tool decorator, why not do it?

ethanglide and others added 3 commits December 13, 2024 20:26
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
@baskaryan
Copy link
Collaborator

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

@ccurme ccurme marked this pull request as draft December 20, 2024 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
langchain Related to the langchain package size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

make sure that @tool decorator can be applied to a method (regression?)
3 participants