Skip to content

Extend @tool to work with methods #19783

@eyurtsev

Description

@eyurtsev

It looks like a number of users are trying to use @tool with methods. This is likely helpful in binding state to the tools for users that do not want to make a tool factory.

Discussed in #9404

Originally posted by rlnasuti August 17, 2023
Hello everyone!

I'm trying to create an Agent with access to some tools where the Agent and the tools themselves are part of a class. This is pretty standard stuff outside of a class, but I'm running into issues when I try to use it as part of a custom class.

Specifically, I'm trying to use the @tool decorator on a member function. Here's an example:

@tool
 def summarize_document(self) -> str:
    """Useful for retrieving a summary of the document."""
    return "No summary available"

The problem I'm running into is that the Agent will supply the self parameter:

> Entering new AgentExecutor chain...
Invoking: `summarize_document` with `{'self': {}}`

which causes an error:
TypeError: _run() got multiple values for argument 'self'

So my question - has anyone encountered this issue and figured out a way to work around it? I tried giving it an arg_schema that was an empty class along with infer_schema=False, but it still tried to send in that self parameter. I'd like it to call the member function like this:

> Entering new AgentExecutor chain...

Invoking: `self.summarize_document` with `{}`
```</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions