ToolInvoker

Executes tool calls it receives from the LLM.

Basic Information

For an overview, input and output information, and parameters, see Haystack documentation.

Usage Examples

Pipeline Components as Tools

You can use other components as tools for the LLM. In such case, you must use the ComponentTool wrapper to pass the component configuration. This example uses SerperDevWebSearch as a tool:

  ToolInvoker:
    type: haystack.components.tools.tool_invoker.ToolInvoker
    init_parameters:
      tools: # this is where you pass the tools, or components
      - type: haystack.tools.component_tool.ComponentTool # this is the wrapper you must use for components as tools
        data: # note that instead of `init_parameters`, you must use the `data` object to pass component configuration
          component: # here, you configure the component just as you would normally do
            type: haystack.components.websearch.serper_dev.SerperDevWebSearch
            init_parameters:
              api_key:
                type: env_var
                env_vars:
                - SERPERDEV_API_KEY
                strict: false
              top_k: 10
          name: web_search
          description: Search the web for current information on any topic
      raise_on_failure: true
      convert_result_to_json_string: false

Additionally, you must configure the component in the tools parameter of the ChatGenerator. This example uses OpenAIChatGenerator:

  OpenAIChatGenerator:
    type: haystack.components.generators.chat.openai.OpenAIChatGenerator
    init_parameters:
      api_key:
        type: env_var
        env_vars:
        - OPENAI_API_KEY
        strict: false
      model: gpt-4o-mini
      streaming_callback:
      api_base_url:
      organization:
      generation_kwargs:
      timeout:
      max_retries:
      tools: # here is the tool configuration
      - type: haystack.tools.component_tool.ComponentTool # the type always has this value
        data:
          description: Search the web for current information on any topic # the description of the tool, some models rely on it heavily
          name: web_search # the name of the tool, must be the same as the name you passed in ToolInvoker configuration
          component: # this is the component configuration
            type: haystack.components.websearch.serper_dev.SerperDevWebSearch
            init_parameters:
              api_key:
                type: env_var
                env_vars:
                - SERPERDEV_API_KEY
                strict: false
              top_k: 10
      tools_strict: false

Functions as Tools

You can use a custom function as a tool for the LLM. To do this, create a custom component that includes this function. For details, see Create a Custom Component.

When you have a custom component with your function and it's imported to deepset AI Platform, you can pass it as a tool to the ChatGenerator:

  OpenAIChatGenerator:
    type: haystack.components.generators.chat.openai.OpenAIChatGenerator
    init_parameters:
      api_key:
        type: env_var
        env_vars:
        - OPENAI_API_KEY
        strict: false
      model: gpt-4o-mini
      tools: 
      - type: haystack.tools.tool.Tool # the type is always the same
        data:
          description: # the description of the function
          name: # the name of the function
          parameters: # a schema defining the parameters your custom component expects
          function: # the import path to the function in custom component, for example `custom_component.path.to.your.function.function_name`
      tools_strict: false

Then, you add the custom component configuration to ToolInvoker:

  ToolInvoker:
    type: haystack.components.tools.tool_invoker.ToolInvoker
    init_parameters:
      tools: 
      - type: haystack.tools.tool.Tool # the type is always the same
        data:
          description: # the description of the function
          name: # the name of the function
          parameters: # a schema defining the parameters your custom component expects
          function: # the import path to the function in custom component, for example `custom_component.path.to.your.function.function_name`
      raise_on_failure: true
      convert_result_to_json_string: false

And finally, you connect the ChatGenerator's output to ToolInvoker.