deepset-ai / haystack

AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.
https://haystack.deepset.ai
Apache License 2.0
17.72k stars 1.92k forks source link

🧪 Tools: convert functions (and others) into Tools #8189

Closed anakin87 closed 2 weeks ago

anakin87 commented 3 months ago

While detailed, manually crafted specifications are ideal, utilities for converting different types of callables into Tools can be helpful for users.

To explore and see if feasible:

Emil-io commented 1 month ago

@anakin87 Just want to understand this. Is the idea that I code a python function and only use this to create a Tool Object?

anakin87 commented 1 month ago

I'm working on something like this:

from typing import Annotated, Literal
from haystack_experimental.dataclasses 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"):
    """A simple function to get the current weather for a location."""

    return f"Weather report for {city}: 20 {unit}, sunny"

tool = Tool.from_function(get_weather)

print(tool)
>>> Tool(name='get_weather', description='A simple function to get the current weather for a location.',
>>> parameters={
>>> 'type': 'object',
>>> 'properties': {
>>>     'city': {'type': 'string', 'description': 'the city for which to get the weather', 'default': 'Munich'},
>>>     'unit': {
>>>         'type': 'string',
>>>         'enum': ['Celsius', 'Fahrenheit'],
>>>         'description': 'the unit for the temperature',
>>>         'default': 'Celsius',
>>>     },
>>>     }
>>> },
>>> function=<function get_weather at 0x7f7b3a8a9b80>)

Related PR: https://github.com/deepset-ai/haystack-experimental/pull/114

Emil-io commented 1 month ago

awesome, this looks super cool!

anakin87 commented 1 month ago

Update

vblagoje commented 1 month ago

Yes, agreed @anakin87 - users can also prepare dataclass (Document, ChatMessage) instances in these functions from primitive inputs and pass dataclass instances to pipelines as inputs. I would regard automatic pipeline invocation as tools as a north star we should aim for but right now we can provide immediate benefits to our users via function "bridges" that are almost as good.

anakin87 commented 2 weeks ago

It is possible to create the tools:

I'm closing this issue and moving the discussion of potential directions to explore in #8505.