haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Add `parameter` and `description` creation from `create_tool_from_function` into `Tool`

Open sjrl opened this issue 9 months ago • 3 comments

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?

sjrl avatar Mar 10 '25 09:03 sjrl

This PR is also related to this issue to some degree: https://github.com/deepset-ai/haystack/pull/9004

LastRemote avatar Mar 11 '25 10:03 LastRemote

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.

anakin87 avatar Mar 11 '25 18:03 anakin87

Yes that's correct @anakin87! I think this would be quite useful and provide a nice interface for quickly creating tools from functions especially when working in yamls.

sjrl avatar Mar 12 '25 10:03 sjrl