Skip to main content

Agents

Chains are great when we know the specific sequence of tool usage needed for any user input. But for certain use cases, how many times we use tools depends on the input. In these cases, we want to let the model itself decide how many times to use tools and in what order. That's where Agents come in!

LangChain comes with a number of built-in agents that are optimized for different use cases. Read about all the available agent types here.

For this example, let’s try out the OpenAI tools agent, which makes use of the new OpenAI tool-calling API (this is only available in the latest OpenAI models, and differs from function-calling in that the model can return multiple function invocations at once).

Keep in mind that some agents only support single-argument tools - for these agents, you will need to use a DynamicTool instead and parse the input string yourself.

Setup​

Because we're using OpenAI for this guide, we'll need to install its partner package:

npm install @langchain/openai

You'll need to sign up for an OpenAI key and set it as an environment variable named OPENAI_API_KEY.

We'll also use the popular validation library Zod to define our tool schemas. It's already a dependency of langchain, but you can install it explicitly like this too:

npm install zod

Create tools​

First, we need to create some tool to call. For this example, we will create custom tools from functions. For more information on creating custom tools, please see this guide.

import { z } from "zod";
import { DynamicStructuredTool } from "@langchain/core/tools";

const addTool = new DynamicStructuredTool({
name: "add",
description: "Add two integers together.",
schema: z.object({
firstInt: z.number(),
secondInt: z.number(),
}),
func: async ({ firstInt, secondInt }) => {
return (firstInt + secondInt).toString();
},
});

const multiplyTool = new DynamicStructuredTool({
name: "multiply",
description: "Multiply two integers together.",
schema: z.object({
firstInt: z.number(),
secondInt: z.number(),
}),
func: async ({ firstInt, secondInt }) => {
return (firstInt * secondInt).toString();
},
});

const exponentiateTool = new DynamicStructuredTool({
name: "exponentiate",
description: "Exponentiate the base to the exponent power.",
schema: z.object({
base: z.number(),
exponent: z.number(),
}),
func: async ({ base, exponent }) => {
return (base ** exponent).toString();
},
});

const tools = [addTool, multiplyTool, exponentiateTool];

Create prompt​

import { pull } from "langchain/hub";
import type { ChatPromptTemplate } from "@langchain/core/prompts";

// Get the prompt to use - you can modify this!
// You can also see the full prompt at:
// https://smith.langchain.com/hub/hwchase17/openai-tools-agent
const prompt = await pull<ChatPromptTemplate>("hwchase17/openai-tools-agent");

Create agent​

import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createOpenAIToolsAgent } from "langchain/agents";

const model = new ChatOpenAI({
model: "gpt-3.5-turbo-1106",
temperature: 0,
});

const agent = await createOpenAIToolsAgent({
llm: model,
tools,
prompt,
});

const agentExecutor = new AgentExecutor({
agent,
tools,
verbose: true,
});

Invoke agent​

info

You can see a LangSmith trace of this example here

await agentExecutor.invoke({
input:
"Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result",
});
> Entering new AgentExecutor chain...

Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`


243
Invoking: `add` with `{'first_int': 12, 'second_int': 3}`


15
Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`


3645
Invoking: `exponentiate` with `{'base': 3645, 'exponent': 2}`


13286025

The result of taking 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.

> Finished chain.
{
input: 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',
output: 'The result of taking 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.'
}

Help us out by providing feedback on this documentation page: