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 parameter and description creation from create_tool_from_function into Tool #9006

Open
sjrl opened this issue Mar 10, 2025 · 2 comments
Open

Comments

@sjrl
Copy link
Contributor

sjrl commented Mar 10, 2025

Is your feature request related to a problem? Please describe.
The create_tool_from_function is very useful as well as the tool decorator for not requiring users to have to specify description and parameters when converting a function into a tool.

However, when working from a yaml file (like in dC) it's not easy to use either of these conveniences.

Describe the solution you'd like
So I was wondering if we could update Tool to optionally require description and parameters and if they are missing we use the logic in create_tool_from_function to auto fill the parameters and description.

Describe alternatives you've considered
Leave as is and require users in Pipeline Studio to have to specify description and parameters when using Tool.

Additional context
What do you think @anakin87 since you've worked on this a lot?

@LastRemote
Copy link
Contributor

LastRemote commented Mar 11, 2025

This PR is also related to this issue to some degree: #9004

@anakin87
Copy link
Member

Based on my understanding, the idea is this: incorporate the create_tool_from_function logic into Tool.

This would allow the following:

from haystack.tools import Tool

def get_weather(
    city: Annotated[str, "the city for which to get the weather"] = "Munich",
    unit: Annotated[Literal["Celsius", "Fahrenheit"], "the unit for the temperature"] = "Celsius",
    nullable_param: Annotated[Optional[str], "a nullable parameter"] = None,
) -> str:
    """A simple function to get the current weather for a location."""
    return f"Weather report for {city}: 20 {unit}, sunny"

my_tool=Tool(name="get_weather", function=get_weather)  # also the name could be easily inferred if we want.

Technically, there would be some difficulties related to the fact that we cannot do the following using a dataclass

@dataclass
class Tool:
    name: Optional[str] = None
    description: Optional[str] = None
    parameters: Optional[Dict[str, Any]] = None
    function: Callable

It would fail with the error TypeError: non-default argument 'function' follows default argument.

Ofc, we can find several solutions.

In case we decide that this is relevant, I would like to work on this.

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

No branches or pull requests

3 participants