ComponentTool
This wrapper allows using Haystack components to be used as tools by LLMs.
Mandatory init variables | "component": The Haystack component to wrap |
API reference | Tools |
GitHub link | https://github.com/deepset-ai/haystack/blob/main/haystack/tools/component_tool.py |
Overview
ComponentTool
is a Tool that wraps Haystack components, allowing them to be used as tools by LLMs. ComponentTool automatically generates LLM-compatible tool schemas from component input sockets, which are derived from the component's run
method signature and type hints.
It does input type conversion and offers support for components with run methods that have the following input types:
- Basic types (str, int, float, bool, dict)
- Dataclasses (both simple and nested structures)
- Lists of basic types (e.g., List[str])
- Lists of dataclasses (e.g., List[Document])
- Parameters with mixed types (e.g., List[Document], str etc.)
Parameters
component
is mandatory and needs to be a Haystack component, either an existing one or a custom component.
name
is optional and defaults to the name of the component written in snake case, such as "serper_dev_web_search" for SerperDevWebSearch.
description
is optional and defaults to the component’s docstring. It’s the description that explains to the LLM what the tool can be used for.
Usage
Install the additional dependencies docstring-parser
and jsonschema
package to use the ComponentTool
:
pip install docstring-parser, jsonschema
In a pipeline
You can create a ComponentTool
from an existing SerperDevWebSearch
component and let an OpenAIChatGenerator
use it as a tool in a pipeline.
from haystack import component, Pipeline
from haystack.tools import ComponentTool
from haystack.components.websearch import SerperDevWebSearch
from haystack.utils import Secret
from haystack.components.tools.tool_invoker import ToolInvoker
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
# Create a SerperDev search component
search = SerperDevWebSearch(api_key=Secret.from_env_var("SERPERDEV_API_KEY"), top_k=3)
# Create a tool from the component
tool = ComponentTool(
component=search,
name="web_search", # Optional: defaults to "serper_dev_web_search"
description="Search the web for current information on any topic" # Optional: defaults to component docstring
)
# Create pipeline with OpenAIChatGenerator and ToolInvoker
pipeline = Pipeline()
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini", tools=[tool]))
pipeline.add_component("tool_invoker", ToolInvoker(tools=[tool]))
# Connect components
pipeline.connect("llm.replies", "tool_invoker.messages")
message = ChatMessage.from_user("Use the web search tool to find information about Nikola Tesla")
# Run pipeline
result = pipeline.run({"llm": {"messages": [message]}})
print(result)
Updated 3 days ago