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

Add method support for tool decorator #627

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

accupham
Copy link

@accupham accupham commented Feb 13, 2025

This PR address #574 and allows the tool decorator to work with class methods.

The original tool decorator doesn't work on class methods because it unconditionally adds a self parameter and treats methods as if it were a plain function. That does not play nicely with Python’s bound-method behavior. This inspects the signature to prevent double-binding of self.

The original tool decorator doesn't work on class methods because it unconditionally adds a self parameter treats method as if it were a plain function. That does not play nicely with Python’s bound-method behavior. This inspects the signature to prevent double-binding of self.
new_signature = original_signature.replace(parameters=new_parameters)
simple_tool.forward.__signature__ = new_signature

@functools.wraps(tool_function)
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't you reinventing inspect.ismethod and inspect.isfunction? First would correctly detect classmethod and just a method, the second will capture staticmethod and a regular function.

Copy link
Author

Choose a reason for hiding this comment

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

Didn’t know about that one, thanks. Will confirm with tests today

Co-authored-by: Alex <sysradium@users.noreply.github.com>
Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

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

Thanks. Maybe creating some tests?

@accupham
Copy link
Author

Will do

@accupham
Copy link
Author

@albertvillanova @sysradium I added tests and documentation. Can you run the workflow for me?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@accupham
Copy link
Author

FYI I pulled in the patches at #693 #691 and the tests are passing now for me locally.

@accupham
Copy link
Author

@albertvillanova Do you mind triggering the tests one more time?

@accupham
Copy link
Author

Any update on this?

@sysradium
Copy link
Contributor

Unfortunately I don't have rights to run the workflow : /

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants