Logo
Modules

Tools

A "tool" is a utility that can be called by an agent on behalf of an LLM. A tool can be called to perform custom actions, or retrieve extra information based on the LLM-generated input. A result from a tool call can be used by subsequent steps in a workflow, or to compute a final answer. For example, a "weather tool" could fetch some live weather information from a geographical location.

Function tool

Function tools are implemented with the FunctionTool class. A FunctionTool is constructed from a function with signature

(input: T, additionalArg?: AdditionalToolArgument) => R

where

  • input is generated by the LLM, T is the type defined by the tool parameters
  • additionalArg is an optional extra argument, see "Binding" below
  • R is the return type

Binding

An additional argument can be bound to a tool, each tool call will be passed

  • the input provided by the LLM
  • the additional argument (extends object)

Note: calling the bind method will return a new FunctionTool instance, without modifying the tool which bind is called on.

Example to pass a userToken as additional argument:

// first arg is LLM input, second is bound arg
const queryKnowledgeBase = async ({ question }, { userToken }) => {
  const response = await fetch(`https://knowledge-base.com?token=${userToken}&query=${question}`);
  // ...
};
 
// define tool as usual
const kbTool = FunctionTool.from(queryKnowledgeBase, {
  name: 'queryKnowledgeBase',
  description: 'Query knowledge base',
  parameters: z.object({
    question: z.string({
      description: 'The user question',
    }),
  }),
});
 
// create an agent
const additionalArg = { userToken: 'abcd1234' };
const kbAgent = new LLMAgent({
  tools: [kbTool.bind(additionalArg)],
  // llm, systemPrompt etc
})
Edit on GitHub

Last updated on

On this page