You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I love smolagents, but defining my own tools as classes feels clunky
Currently when using classes over functions, class definitions are not very idiomatic python, e.g.
classHFModelDownloadsTool(Tool):
name="model_download_counter"description=""" This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. It returns the name of the checkpoint."""inputs= {
"task": {
"type": "string",
"description": "the task category (such as text-classification, depth-estimation, etc)",
}
}
output_type="string"defforward(self, task: str):
fromhuggingface_hubimportlist_modelsmodel=next(iter(list_models(filter=task, sort="downloads", direction=-1)))
returnmodel.id
all of the 4 required class attributes could instead be introspected from docstrings and type hints (as they are when using tool function decorators).
Additionally, the example is confusing, because it looks like there is some introspection of the forward happening method:
but it's not really clear how that relates to the class attributes, if one takes precedence over the other, particularly the (less expressive) type in output_type. It also looks like it's possible to have contradictory type hints and class attributes, and it's not clear what the overall effect is on prompt generation and result interpretation.
Describe the solution you'd like
More idiomatic python would look like:
classHFModelDownloadsTool(Tool):
""" This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. It returns the name of the checkpoint."""defforward(self, task: str) ->string:
""" Args: task: the task category (such as text-classification, depth-estimation, etc) Returns: name of the checkpoint """fromhuggingface_hubimportlist_modelsmodel=next(iter(list_models(filter=task, sort="downloads", direction=-1)))
returnmodel.id
I could try a PR for this but I don't know where this fits on your roadmap
Is this not possible with the current options.
As an interim step it would be good to have more docs here that explain best practice:
An output_type attribute, which specifies the output type. The types for both inputs and output_type should be Pydantic formats, they can be either of these: ~AUTHORIZED_TYPES().
The pydantic docs are for generating json schema, and it looks like the ~AUTHORIZED_TYPES() is meant to auto-expand.
Describe alternatives you've considered
An alternative would be to encourage use of decorated functions, but to add a context argument, similar to pydantic-ai, which would bypass the current limitations of this (simpler) approach
Is your feature request related to a problem? Please describe.
I love smolagents, but defining my own tools as classes feels clunky
Currently when using classes over functions, class definitions are not very idiomatic python, e.g.
all of the 4 required class attributes could instead be introspected from docstrings and type hints (as they are when using tool function decorators).
Additionally, the example is confusing, because it looks like there is some introspection of the
forward
happening method:smolagents/src/smolagents/tools.py
Lines 149 to 157 in d74837b
but it's not really clear how that relates to the class attributes, if one takes precedence over the other, particularly the (less expressive) type in
output_type
. It also looks like it's possible to have contradictory type hints and class attributes, and it's not clear what the overall effect is on prompt generation and result interpretation.Describe the solution you'd like
More idiomatic python would look like:
I could try a PR for this but I don't know where this fits on your roadmap
Is this not possible with the current options.
As an interim step it would be good to have more docs here that explain best practice:
https://huggingface.co/docs/smolagents/tutorials/tools
In particular this isn't clear:
The pydantic docs are for generating json schema, and it looks like the
~AUTHORIZED_TYPES()
is meant to auto-expand.Describe alternatives you've considered
An alternative would be to encourage use of decorated functions, but to add a
context
argument, similar to pydantic-ai, which would bypass the current limitations of this (simpler) approachhttps://ai.pydantic.dev/#tools-dependency-injection-example
The text was updated successfully, but these errors were encountered: