Building AI Agents with LlamaIndex: ReAct Model Practice

In previous articles, we explored document question answering, dialogue systems, RAG pipelines, and system monitoring. Today, we will delve into how to build AI agents using LlamaIndex, particularly focusing on the ReAct (Reasoning and Acting) model, which enables AI to reason and perform specific actions.

Building AI Agents with LlamaIndex: ReAct Model Practice

1. Introduction to ReAct Model

ReAct is an AI model that combines reasoning and acting:

  • Reasoning: Analyzing problems and formulating solutions
  • Acting: Executing specific operational steps
  • Observing: Evaluating the results of actions taken
  • Thinking: Adjusting strategies based on observation results

2. Basic Environment Configuration

import os
from dotenv import load_dotenv
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
from llama_index.core.tools import FunctionTool

load_dotenv()

3. Building a Basic Toolset

First, we need to define some basic tools for the agent to use:

# Mathematical operation tools
def multiply(a: float, b: float) -> float:
    """Multiply two numbers and return the result"""
    return a * b

def add(a: float, b: float) -> float:
    """Add two numbers and return the result"""
    return a + b

def subtract(a: float, b: float) -> float:
    """Subtract two numbers and return the difference"""
    return a - b

def divide(a: float, b: float) -> float:
    """Divide two numbers and return the quotient"""
    return a / b

# Convert functions to tools
multiply_tool = FunctionTool.from_defaults(fn=multiply)
add_tool = FunctionTool.from_defaults(fn=add)
subtract_tool = FunctionTool.from_defaults(fn=subtract)
divide_tool = FunctionTool.from_defaults(fn=divide)

4. Creating a ReAct Agent

# Initialize language model
llm = OpenAI(model="gpt-4", temperature=0)

# Create ReAct agent
agent = ReActAgent.from_tools(
    tools=[multiply_tool, add_tool, subtract_tool, divide_tool],
    llm=llm,
    verbose=True
)

5. Implementing a Complex Calculation Example

def solve_math_problem():
    # Solve a complex math problem
    response = agent.chat(
        "20+(2*4)/(5-1)? Use tools to calculate each step."
    )
    print(response)

6. Expanding the Toolset

6.1 File Operation Tools

def read_file(file_path: str) -> str:
    """Read the contents of a file"""
    with open(file_path, 'r') as f:
        return f.read()

def write_file(file_path: str, content: str) -> bool:
    """Write content to a file"""
    try:
        with open(file_path, 'w') as f:
            f.write(content)
        return True
    except Exception as e:
        return False

# Convert to tools
file_tools = [
    FunctionTool.from_defaults(fn=read_file),
    FunctionTool.from_defaults(fn=write_file)
]

6.2 Web Request Tools

import requests

def fetch_web_content(url: str) -> str:
    """Fetch the content of a web page"""
    try:
        response = requests.get(url)
        return response.text
    except Exception as e:
        return str(e)

web_tool = FunctionTool.from_defaults(fn=fetch_web_content)

7. Advanced Agent Configuration

7.1 Custom Reasoning Chains

from llama_index.core.agent import ReActChatFormatter

# Custom reasoning formatter
custom_formatter = ReActChatFormatter(
    system_header="You are a professional assistant, skilled at solving complex problems.",
    system_footer="Remember to think step-by-step and use tools."
)

# Create an agent with custom reasoning
advanced_agent = ReActAgent.from_tools(
    tools=[multiply_tool, add_tool, subtract_tool, divide_tool],
    llm=llm,
    chat_formatter=custom_formatter,
    verbose=True
)

7.2 Error Handling

def safe_agent_call(agent, query):
    try:
        response = agent.chat(query)
        return response
    except Exception as e:
        print(f"Error occurred: {str(e)}")
        return None

Conclusion

Through this article, we learned about:

  • The basic principles of the ReAct model
  • How to build a basic toolset
  • Creating and configuring a ReAct agent

In the next article, we will explore how to use LlamaParse to process complex PDF documents, further enhancing document processing capabilities.

Leave a Comment