A Python implementation of a ReAct (Reasoning and Acting) agent using LangChain and OpenAI. This agent can search Wikipedia and answer complex questions by reasoning through the search results step-by-step.
- 🤖 ReAct (Reasoning and Acting) agent implementation
- 🔍 Wikipedia search and lookup capabilities
- 🧠 OpenAI GPT-3.5-turbo model integration
- 🔧 Environment variable configuration
- 📚 Professional project structure
- ✅ Fully tested and working
- Python 3.7 or higher
- OpenAI API key
-
Clone or download this repository
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
# Copy the example environment file cp .env.example .env # Edit .env and add your OpenAI API key nano .env
Add your OpenAI API key to the
.envfile:OPENAI_API_KEY=your_actual_api_key_here
Run the ReAct agent:
python react-agent.pyThe agent will process the example question about David Chanoff and the U.S. Navy admiral, demonstrating its reasoning and acting capabilities.
- Search Tool: The agent can search Wikipedia for relevant information
- Lookup Tool: The agent can look up specific articles or sections
- Reasoning: The agent uses the ReAct framework to reason about what information it needs
- Acting: The agent takes actions (search/lookup) based on its reasoning
- Answer: The agent provides a final answer based on the information gathered
ReAct Agent/
├── react-agent.py # Main script
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── .env # Your actual environment variables (not in git)
├── .gitignore # Git ignore file
└── README.md # This file
openai>=1.6.1,<2.0.0: OpenAI API clientwikipedia==1.4.0: Wikipedia API wrapperlangchain==0.1.0: Framework for building LLM applicationslangchain-openai: OpenAI integration for LangChainlangchain-community: Community tools and utilitieslangchainhub: LangChain Hub integrationpython-dotenv==1.0.0: Environment variable management
🤖 ReAct Agent is thinking...
Question: Which Italian city was Michelangelo working in when he painted the ceiling of the Sistine Chapel, and what was the name of the Pope who commissioned this work?
--------------------------------------------------
> Entering new AgentExecutor chain...
I need to find out more about Michelangelo and the Sistine Chapel, specifically where he was working and who commissioned the work.
Action: Lookup
Action Input: Michelangelo
Observation: [Wikipedia search results about Michelangelo and his works]
I need to find more information about the Sistine Chapel and who commissioned it.
Action: Lookup
Action Input: Sistine Chapel
Observation: [Wikipedia search results about the Sistine Chapel, its location in Vatican City, and Pope Julius II's commission]
I now know that Michelangelo was working in Vatican City (Rome) when he painted the Sistine Chapel ceiling, and Pope Julius II commissioned this work.
Final Answer: Vatican City (Rome) and Pope Julius II
> Finished chain.
--------------------------------------------------
Answer: Vatican City (Rome) and Pope Julius II
To ask different questions, modify the question variable in react-agent.py:
question = "Your question here"Here are some medium-difficulty questions that effectively demonstrate the ReAct agent's reasoning capabilities:
Art History: "Which Italian city was Michelangelo working in when he painted the ceiling of the Sistine Chapel, and what was the name of the Pope who commissioned this work?"
Martial Arts: "What is the name of the martial art style that Bruce Lee developed, and which traditional Chinese martial art did he study under Ip Man before creating his own system?"
Statistics: "Who developed the statistical method known as the t-test, and what was the name of the brewery where this statistician worked when he created this important statistical tool?"
These questions require multi-step reasoning, information synthesis, and demonstrate the agent's ability to connect different pieces of information from Wikipedia searches.
- Make sure to keep your
.envfile secure and never commit it to version control - The agent uses the
gpt-3.5-turbomodel for cost-effectiveness - Verbose mode is enabled to show the agent's reasoning process
- The agent uses a custom ReAct prompt template for Wikipedia research