Skip to main content

How to use LangChain tools

Tools are interfaces that an agent, chain, or LLM can use to interact with the world. They combine a few things:

  1. The name of the tool
  2. A description of what the tool is
  3. JSON schema of what the inputs to the tool are
  4. The function to call
  5. Whether the result of a tool should be returned directly to the user

It is useful to have all this information because this information can be used to build action-taking systems! The name, description, and schema can be used to prompt the LLM so it knows how to specify what action to take, and then the function to call is equivalent to taking that action.

The simpler the input to a tool is, the easier it is for an LLM to be able to use it. Many agents will only work with tools that have a single string input. For a list of agent types and which ones work with more complicated inputs, please see this documentation

Importantly, the name, description, and schema (if used) are all used in the prompt. Therefore, it is vitally important that they are clear and describe exactly how the tool should be used.

Default Tools​

Let’s take a look at how to work with tools. To do this, we’ll work with a built in tool.

import { WikipediaQueryRun } from "@langchain/community/tools/wikipedia_query_run";

const tool = new WikipediaQueryRun({
topKResults: 1,
maxDocContentLength: 100,
});

This is the default name:

tool.name;
"wikipedia-api"

This is the default description:

tool.description;
"A tool for interacting with and fetching data from the Wikipedia API."

This is the default schema of the inputs. This is a Zod schema on the tool class. We convert it to JSON schema for display purposes:

import { zodToJsonSchema } from "zod-to-json-schema";

zodToJsonSchema(tool.schema);
{
type: "object",
properties: { input: { type: "string" } },
additionalProperties: false,
"$schema": "http://json-schema.org/draft-07/schema#"
}

We can see if the tool should return directly to the user

tool.returnDirect;
false

We can invoke this tool with an object input:

await tool.invoke({ input: "langchain" });
"Page: LangChain\n" +
"Summary: LangChain is a framework designed to simplify the creation of applications "

We can also invoke this tool with a single string input. We can do this because this tool expects only a single input. If it required multiple inputs, we would not be able to do that.

await tool.invoke("langchain");
"Page: LangChain\n" +
"Summary: LangChain is a framework designed to simplify the creation of applications "

More Topics​

This was a quick introduction to tools in LangChain, but there is a lot more to learn

Built-In Tools: For a list of all built-in tools, see this page

Custom Tools: Although built-in tools are useful, it’s highly likely that you’ll have to define your own tools. See this guide for instructions on how to do so.

Toolkits: Toolkits are collections of tools that work well together. For a more in depth description as well as a list of all built-in toolkits, see this page

Tools as OpenAI Functions: Tools are very similar to OpenAI Functions, and can easily be converted to that format. See this notebook for instructions on how to do that.


Was this page helpful?


You can leave detailed feedback on GitHub.