-
Notifications
You must be signed in to change notification settings - Fork 16.3k
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
core: improved method tools #28695
Changes from 6 commits
883d120
ee50e43
6b881cc
af1a44c
3565b9f
7272bb5
57118fe
fca11c4
6a01806
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
ethanglide marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -43,6 +43,7 @@ | |||||||||||||||||||||||
StructuredTool, | ||||||||||||||||||||||||
Tool, | ||||||||||||||||||||||||
ToolException, | ||||||||||||||||||||||||
methodtool, | ||||||||||||||||||||||||
tool, | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
from langchain_core.tools.base import ( | ||||||||||||||||||||||||
|
@@ -2174,3 +2175,146 @@ def foo(x: int) -> Bar: | |||||||||||||||||||||||
assert foo.invoke( | ||||||||||||||||||||||||
{"type": "tool_call", "args": {"x": 0}, "name": "foo", "id": "bar"} | ||||||||||||||||||||||||
) == Bar(x=0) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_self_ref() -> None: | ||||||||||||||||||||||||
"""Test that a method tool can reference self.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
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) | ||||||||||||||||||||||||
assert a.foo.invoke({"a": 1, "b": 2}) == 13 | ||||||||||||||||||||||||
ethanglide marked this conversation as resolved.
Show resolved
Hide resolved
ethanglide marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_args() -> None: | ||||||||||||||||||||||||
"""Test that a method tool's args do not include self.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
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) | ||||||||||||||||||||||||
assert "self" not in a.foo.args | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
async def test_method_tool_async() -> None: | ||||||||||||||||||||||||
"""Test that a method tool can be async.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
class A: | ||||||||||||||||||||||||
def __init__(self, c: int): | ||||||||||||||||||||||||
self.c = c | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@methodtool | ||||||||||||||||||||||||
async def foo(self, a: int, b: int) -> int: | ||||||||||||||||||||||||
"""Add two numbers to c.""" | ||||||||||||||||||||||||
return a + b + self.c | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
a = A(10) | ||||||||||||||||||||||||
async_response = await a.foo.ainvoke({"a": 1, "b": 2}) | ||||||||||||||||||||||||
assert async_response == 13 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_string_invoke() -> None: | ||||||||||||||||||||||||
"""Test that a method tool can be invoked with a string.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
class A: | ||||||||||||||||||||||||
def __init__(self, a: str): | ||||||||||||||||||||||||
self.a = a | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@methodtool | ||||||||||||||||||||||||
def foo(self, b: str) -> str: | ||||||||||||||||||||||||
"""Concatenate a and b.""" | ||||||||||||||||||||||||
return self.a + b | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
a = A("a") | ||||||||||||||||||||||||
assert a.foo.invoke("b") == "ab" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_toolcall_invoke() -> None: | ||||||||||||||||||||||||
"""Test that a method tool can be invoked with a ToolCall.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
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) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
tool_call = { | ||||||||||||||||||||||||
"name": a.foo.name, | ||||||||||||||||||||||||
"args": {"a": 1, "b": 2}, | ||||||||||||||||||||||||
"id": "123", | ||||||||||||||||||||||||
"type": "tool_call", | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
tool_message = a.foo.invoke(tool_call) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
assert int(tool_message.content) == 13 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_info() -> None: | ||||||||||||||||||||||||
"""Test that a method tools have correct name/description/args schema.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
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 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
args = { | ||||||||||||||||||||||||
"a": {"title": "A", "type": "integer"}, | ||||||||||||||||||||||||
"b": {"title": "B", "type": "integer"}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
assert A.foo.name == "foo" | ||||||||||||||||||||||||
assert A.foo.description == "Add two numbers to c." | ||||||||||||||||||||||||
assert A.foo.args == args | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_classmethod() -> None: | ||||||||||||||||||||||||
"""Test that a method tool can be a classmethod.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
class A: | ||||||||||||||||||||||||
c = 5 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@methodtool | ||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||
def foo(cls, a: int, b: int) -> int: | ||||||||||||||||||||||||
"""Add two numbers to c.""" | ||||||||||||||||||||||||
return a + b + cls.c | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
assert A.foo.invoke({"a": 1, "b": 2}) == 8 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def test_method_tool_classmethod_args() -> None: | ||||||||||||||||||||||||
"""Test that a classmethod tool's args do not include cls.""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
class A: | ||||||||||||||||||||||||
c = 5 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@methodtool | ||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||
def foo(cls, a: int, b: int) -> int: | ||||||||||||||||||||||||
"""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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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 commentThe 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 |
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 returnsMethodTool(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:
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 theMethodTool
class that it will pass inowner
(class itself) forouter_instance
with classmethods, andinstance
(class instance) for regular methods. Since theproperty
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 becauseclassmethod
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 theMethodTool
class never needs to be exposed to client code.