Przejdź do głównej zawartości

ReAct Agent

The ReAct agent is an AI agent that can reason over the next action, construct an action command, execute the action, and repeat these steps in an iterative loop until the task is complete.

In this notebook tutorial, we showcase how to write your ReAct agent using the llamaindex package.

Setup

First, you need to install the llamaindex package. You can do this by running the following command in your terminal:

pnpm i llamaindex

And then you can import the OpenAIAgent and FunctionTool from the llamaindex package.

import { FunctionTool, OpenAIAgent } from "llamaindex";

Then we can define a function to sum two numbers and another function to divide two numbers.

function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}

// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}

Create a function tool

Now we can create a function tool from the sum function and another function tool from the divide function.

For the parameters of the sum function, we can define a JSON schema.

JSON Schema

const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};

const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend a to divide",
},
b: {
type: "number",
description: "The divisor b to divide by",
},
},
required: ["a", "b"],
};

const sumFunctionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});

const divideFunctionTool = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});

Create an ReAct

Now we can create an OpenAIAgent with the function tools.

const agent = new ReActAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});

Chat with the agent

Now we can chat with the agent.

const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});

console.log(String(response));

The output will be:

Thought: I need to use a tool to help me answer the question.
Action: sumNumbers
Action Input: {"a":5,"b":5}

Observation: 10
Thought: I can answer without using any more tools.
Answer: The sum of 5 and 5 is 10, and when divided by 2, the result is 5.

The sum of 5 and 5 is 10, and when divided by 2, the result is 5.

Full code

import { FunctionTool, ReActAgent } from "llamaindex";

// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}

// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}

// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};

// Define the parameters of the divide function as a JSON schema
const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The argument a to divide",
},
b: {
type: "number",
description: "The argument b to divide",
},
},
required: ["a", "b"],
};

async function main() {
// Create a function tool from the sum function
const sumFunctionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});

// Create a function tool from the divide function
const divideFunctionTool = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});

// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});

// Chat with the agent
const response = await agent.chat({
message: "I want to sum 5 and 5 and then divide by 2",
});

// Print the response
console.log(String(response));
}

main().then(() => {
console.log("Done");
});