This is a lightweight AI Agent example built with Python. It implements the classic ReAct (Reasoning and Acting) pattern, enabling large language models to solve complex tasks through a continuous "Thought -> Action -> Observation" loop.
This project is designed around a "Smart Travel Assistant" scenario, utilizing the Anthropic Claude API as the reasoning engine and integrating external tools to fetch real-time data.
- ReAct Core Loop: Demonstrates how to guide the LLM via system prompts to generate structured
ThoughtandActionresponses, which are then automatically parsed and executed by the script. - Tool Use:
- 🌤️ Real-time Weather: Fetches current weather conditions via
wttr.in(No API key required). - 🔍 Web Search Integration: Integrates the
Tavily Search APIto gather optimized, LLM-friendly attraction recommendations based on the weather.
- 🌤️ Real-time Weather: Fetches current weather conditions via
- Infinite Loop Prevention: Built-in maximum loop count (default is 5) to prevent runaway processes and unnecessary API costs.
- Robust Regex Parsing: Utilizes regular expressions to accurately extract and truncate action instructions from the model's output.
Ensure you have Python 3.8 or higher installed. It is highly recommended to use a virtual environment.
python -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
2. Install Dependencies
pip install anthropic tavily-python requests
3. Configure Environment Variables
export ANTHROPIC_API_KEY="your-anthropic-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
4. Usage
python agent.py