langchain.js
    Preparing search index...

    Function llmToolSelectorMiddleware

    • Middleware for selecting tools using an LLM-based strategy.

      This middleware analyzes the user's query and available tools to select the most relevant tools for the task, reducing the cognitive load on the main model and improving response quality.

      Parameters

      • options: InferInteropZodInput

        Configuration options for the middleware

        • model

          The language model to use for tool selection (default: the provided model from the agent options).

        • systemPrompt

          System prompt for the tool selection model.

        • maxTools

          Maximum number of tools to select.

        • includeFullHistory

          Whether to include the full conversation history in the tool selection prompt.

        • maxRetries

          Maximum number of retries if the model selects incorrect tools.

      Returns AgentMiddleware<any>

      import { llmToolSelectorMiddleware } from "langchain/agents/middleware";

      const middleware = llmToolSelectorMiddleware({
      maxTools: 3,
      systemPrompt: "Select the most relevant tools for the user's query."
      });

      const agent = createAgent({
      model: "openai:gpt-4o",
      tools: [tool1, tool2, tool3, tool4, tool5],
      middleware: [middleware],
      });