LangChainJune 5, 2023
Building a Simple ReAct Agent with LangChain
In this tutorial, we'll explore how to create a ReAct agent using LangChain's agent framework. ReAct (Reasoning + Acting) combines reasoning and action to enhance the problem-solving capabilities of language models.
from langchain.agents import AgentType, initialize_agent
from langchain.tools import BaseTool
from langchain.llms import OpenAI
# Define your custom tools
class Calculator(BaseTool):
name = "Calculator"
description = "Useful for performing mathematical calculations"
def _run(self, query):
try:
return eval(query)
except Exception as e:
return f"Error: {e}"
def _arun(self, query):
raise NotImplementedError("This tool does not support async")
# Initialize the LLM
llm = OpenAI(temperature=0)
# Create the agent
tools = [Calculator()]
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
Read Full Article →