react-agents icon indicating copy to clipboard operation
react-agents copied to clipboard

๐Ÿค– Advanced AI agent system combining ReAct reasoning and Plan-Execute strategies with unified memory, reflection patterns, and browser automation tools. Built with LangGraph, LangChain, and Google Ge...

Hybrid ReAct Agent Service Check Documentation

A sophisticated AI agent system that combines ReAct (Reasoning and Acting) and Plan-Execute paradigms with advanced memory management and context sharing. Built with LangGraph, LangChain, and Google Gemini for intelligent task execution and reasoning.

๐Ÿš€ Key Features

Core Capabilities

  • ๐Ÿง  Hybrid Intelligence: Automatically chooses between ReAct and Plan-Execute strategies based on task complexity
  • ๐Ÿ”„ ReAct Pattern: Implements Thought-Action-Observation cycles for step-by-step reasoning
  • ๐Ÿ“‹ Plan-Execute Mode: Creates and executes complex multi-step plans for sophisticated tasks
  • โšก Real-time Thinking: Watch the agent think and reason in real-time as it processes requests
  • ๐ŸŽฏ Streaming Interface: Live updates showing thought processes, tool execution, and decision making
  • ๐Ÿง  Advanced Memory System: Multi-layered memory with episodic, vector, and contextual storage
  • ๐Ÿ”— Context Sharing: Persistent context across tool interactions and sessions
  • ๐Ÿ› ๏ธ Extensible Tool System: Modular architecture supporting custom tool integration

Intelligence Modes

  1. ReAct Mode: Best for simple queries requiring immediate reasoning and action
  2. Plan-Execute Mode: Optimal for complex multi-step tasks requiring upfront planning
  3. Hybrid Mode: Intelligently selects the best approach based on query complexity

Built-in Tools

  • ๐Ÿ“Š Database Tool: Persistent data storage with caching (CRUD operations)
  • ๐Ÿ“š Wikipedia Tool: Research and information retrieval
  • ๐Ÿ” Web Search Tool: Real-time web search capabilities (Serper API)
  • ๐Ÿงฎ Calculator Tool: Mathematical computations and functions
  • โšก C++ Executor Tool: Code compilation and execution
  • ๐Ÿ”ง Custom Tools: Easy extension framework for domain-specific tools

Memory & Context

  • Episodic Memory: Stores complete interaction episodes for learning
  • Vector Memory: Semantic similarity search for relevant past experiences
  • Context Manager: Maintains session state and tool interaction history
  • Memory Store: Multi-type storage (conversational, episodic, tool-specific)

๐Ÿง  Adaptive Thinking Flow

The hybrid React Agent features sophisticated adaptive thinking that intelligently switches between different reasoning approaches based on real-time evaluation and learning from past experiences.

Key Innovation: Dynamic Approach Switching

  • Intelligent Decision Making: Uses query complexity analysis and episodic memory similarity to choose optimal approach
  • Real-time Evaluation: Continuously assesses execution success and adapts strategy mid-flow
  • Adaptive Replanning: Creates new plans when initial execution fails, with intelligent fallback mechanisms
  • Memory-Informed Learning: Leverages past episodes to improve decision making over time

Adaptive Flow Features

  1. Approach Selection: Automatically determines whether to use Plan-Execute or ReAct based on:

    • Query complexity indicators (multiple steps, calculations, comparisons)
    • Similarity to successful past episodes
    • Tool usage patterns from historical data
  2. Execution Evaluation: After plan execution, evaluates:

    • Success rate (โ‰ฅ70% threshold for completion)
    • Task completion status
    • Quality of results
  3. Adaptive Replanning: When plans fail:

    • Generates new strategies with different approaches
    • Can switch from Plan-Execute to ReAct mid-execution
    • Prevents infinite loops with attempt limits
    • Graceful degradation to simpler approaches
  4. Seamless Integration: ReAct and Plan-Execute modes work together:

    • ReAct serves as fallback for failed plans
    • Plan-Execute handles complex multi-step reasoning
    • Memory system learns from both approaches

Visual Flow & Documentation

  • Adaptive React Agent Thinking Flow: Detailed Mermaid diagram showing the complete decision and execution flow
  • Adaptive Replanning Analysis: In-depth analysis of the replanning mechanisms and strategies

Benefits

  • Robustness: Multiple fallback mechanisms prevent complete failures
  • Efficiency: Uses the most appropriate approach for each query type
  • Learning: Improves over time based on execution history
  • Flexibility: Can handle both simple and complex queries effectively

๐Ÿ“ Project Architecture

react-agent-service/
โ”œโ”€โ”€ agent/                    # Core agent implementation
โ”‚   โ”œโ”€โ”€ react_agent.py      # Main hybrid agent with mode switching
โ”‚   โ”œโ”€โ”€ planner.py           # Planning system for complex tasks
โ”‚   โ”œโ”€โ”€ executor.py          # Plan execution engine
โ”‚   โ”œโ”€โ”€ agent_state.py       # State management and data structures
โ”‚   โ”œโ”€โ”€ tool_manager.py      # Tool orchestration and management
โ”‚   โ””โ”€โ”€ planner_execution.mmd # Execution flow diagram
โ”œโ”€โ”€ tools/                   # Tool implementations
โ”‚   โ”œโ”€โ”€ base_tool.py        # Abstract base class for all tools
โ”‚   โ”œโ”€โ”€ database_tool.py    # Persistent data operations
โ”‚   โ”œโ”€โ”€ wikipedia_tool.py   # Wikipedia search and retrieval
โ”‚   โ”œโ”€โ”€ web_search_tool.py  # Web search capabilities
โ”‚   โ”œโ”€โ”€ calculator_tool.py  # Mathematical computations
โ”‚   โ””โ”€โ”€ cpp_executor_tool.py # Code execution environment
โ”œโ”€โ”€ memory/                  # Advanced memory system
โ”‚   โ”œโ”€โ”€ memory_store.py     # Core memory storage
โ”‚   โ”œโ”€โ”€ vector_memory.py    # Semantic search capabilities
โ”‚   โ”œโ”€โ”€ context_manager.py  # Session and context management
โ”‚   โ””โ”€โ”€ episodic_memory.py  # Episode storage and retrieval
โ”œโ”€โ”€ examples/               # Usage examples and demos
โ”‚   โ”œโ”€โ”€ hybrid_agent_demo.py # Comprehensive demonstration
โ”‚   โ””โ”€โ”€ example_usage.py    # Basic usage examples
โ”œโ”€โ”€ extensions/             # Future extensions and frameworks
โ”‚   โ””โ”€โ”€ multiagent_framework.py # Multi-agent system foundation
โ”œโ”€โ”€ web_interface.py        # Streamlit web UI
โ”œโ”€โ”€ chatbot.py             # Console interface
โ”œโ”€โ”€ llm_manager.py         # LLM session management
โ”œโ”€โ”€ config.py              # Configuration management
โ””โ”€โ”€ requirements.txt       # Dependencies

๐Ÿ”ง Installation & Setup

Prerequisites

  • Python 3.8+
  • Google Gemini API key
  • Optional: Serper API key for web search

Quick Start

  1. Clone and Install
git clone <repository-url>
cd react-agent-service
pip install -r requirements.txt
  1. Configure Environment
cp .env.example .env
# Edit .env and add your API keys:
# GOOGLE_API_KEY=your_gemini_api_key
# SERPER_API_KEY=your_serper_api_key (optional)
  1. Run the Agent
# Interactive launcher (recommended)
python3 launch_ui.py

# Real-time thinking web interface
streamlit run web_interface_streaming.py

# Standard web interface
streamlit run web_interface.py

# Console interface
python3 chatbot.py

# Streaming demo (console)
python3 demo_streaming.py

# Hybrid demo
python3 examples/hybrid_agent_demo.py

๐Ÿ’ก Usage Examples

Basic Programmatic Usage

import asyncio
from agent.react_agent import ReactAgent

async def main():
    # Create hybrid agent (auto-selects best mode)
    agent = ReactAgent(verbose=True, mode="hybrid")
    
    # Simple query (will use ReAct)
    response = await agent.run("What is 2 + 2?")
    print(f"Result: {response['output']}")
    
    # Complex query (will use Plan-Execute)
    complex_query = """
    Calculate the square root of 144, then search for information 
    about that number in mathematics, and store both results 
    in the database
    """
    response = await agent.run(complex_query)
    print(f"Success: {response['success']}")
    print(f"Mode used: {response['metadata']['chosen_approach']}")

asyncio.run(main())

Mode-Specific Usage

# Force ReAct mode for step-by-step reasoning
react_agent = ReactAgent(mode="react")

# Force Plan-Execute mode for complex planning
planner_agent = ReactAgent(mode="plan_execute")

# Hybrid mode (recommended) - auto-selects best approach
hybrid_agent = ReactAgent(mode="hybrid")

Advanced Memory Features

# Access memory statistics
stats = await agent.get_memory_stats()
print(f"Episodes stored: {stats.get('episodes', 0)}")

# Memory persists across sessions for context sharing
response1 = await agent.run("Store my name as Alice")
response2 = await agent.run("What's my name?")  # Remembers Alice

Real-time Streaming Usage

import asyncio
from streaming_agent import StreamingChatbot, EventType

async def watch_agent_think():
    chatbot = StreamingChatbot(verbose=False, mode="hybrid")
    
    query = "Calculate 144 squared root and tell me about it"
    
    async for event in chatbot.chat_stream(query):
        if event.type == EventType.THINKING:
            print(f"๐Ÿค” Agent is thinking: {event.data['thought']}")
        
        elif event.type == EventType.ACTION_START:
            print(f"๐Ÿ”ง Executing: {event.data['action']}")
        
        elif event.type == EventType.COMPLETE:
            print(f"โœ… Final answer: {event.data['response']['output']}")
            break

asyncio.run(watch_agent_think())

๐Ÿ—๏ธ Architecture Deep Dive

Adaptive Execution Flow (Hybrid Mode)

flowchart TD
    START([๐Ÿš€ Query Input]) --> DECIDE{๐Ÿค” Decide Approach}
    
    %% Decision Logic with Memory Integration
    DECIDE -->|Complex Query<br/>+ Past Success| PLAN[๐Ÿ“‹ Plan]
    DECIDE -->|Simple Query<br/>+ Direct Action| THINK[๐Ÿ’ญ Think]
    
    %% Plan-Execute Path with Evaluation
    PLAN --> EXECUTE[โšก Execute Plan]
    EXECUTE --> EVALUATE{๐Ÿ“Š Evaluate Execution}
    
    %% Smart Evaluation Outcomes
    EVALUATE -->|Success โ‰ฅ70%<br/>Complete| FINISH([โœ… Response])
    EVALUATE -->|Plan Failed<br/>Replan Needed| REPLAN[๐Ÿ”„ Adaptive Replan]
    EVALUATE -->|Unsatisfactory<br/>Switch Mode| THINK
    
    %% Adaptive Replanning with Strategy Switching
    REPLAN -->|New Plan<br/>Retry| PLAN
    REPLAN -->|Switch Strategy<br/>to ReAct| THINK
    REPLAN -->|Max Attempts<br/>Fallback| THINK
    REPLAN -->|No Solution<br/>Give Up| FINISH
    
    %% Enhanced ReAct Loop
    THINK -->|Need Action<br/>Continue| ACT[๐Ÿ”ง Act]
    THINK -->|Complete<br/>or Max Steps| FINISH
    
    ACT --> OBSERVE[๐Ÿ‘๏ธ Observe]
    OBSERVE --> THINK
    
    %% Memory Integration (shown as influence)
    MEMORY[(๐Ÿง  Episodic<br/>Memory)] -.->|Similarity<br/>Matching| DECIDE
    MEMORY -.->|Learning<br/>from Episodes| REPLAN
    
    %% Styling
    classDef startEnd fill:#e1f5fe,stroke:#01579b,stroke-width:3px
    classDef decision fill:#fff3e0,stroke:#ef6c00,stroke-width:2px
    classDef planPath fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px
    classDef reactPath fill:#fce4ec,stroke:#c2185b,stroke-width:2px
    classDef adaptive fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
    classDef memory fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px
    
    class START,FINISH startEnd
    class DECIDE,EVALUATE decision
    class PLAN,EXECUTE planPath
    class THINK,ACT,OBSERVE reactPath
    class REPLAN adaptive
    class MEMORY memory

Memory Architecture

graph LR
    Query --> CM[Context Manager]
    CM --> MS[Memory Store]
    MS --> EM[Episodic Memory]
    MS --> VM[Vector Memory]
    VM --> Embeddings[Vector Embeddings]
    EM --> Episodes[Stored Episodes]
    CM --> Tools[Tool Contexts]

Tool Integration Pattern

from tools.base_tool import BaseTool, ToolResult

class CustomTool(BaseTool):
    def __init__(self):
        super().__init__("custom_tool", "My custom tool description")
    
    async def execute(self, query: str, **kwargs) -> ToolResult:
        # Your tool logic here
        result = process_query(query)
        return ToolResult(success=True, data=result)
    
    def get_schema(self):
        return {
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "Input query"}
            }
        }

๐ŸŽฏ Use Cases

Simple Queries (ReAct Mode)

  • Mathematical calculations
  • Information lookup
  • Single-step operations
  • Quick fact retrieval

Complex Tasks (Plan-Execute Mode)

  • Multi-step research projects
  • Data processing pipelines
  • Complex calculations with dependencies
  • Workflow automation

Hybrid Intelligence

  • Travel planning with budget calculations
  • Research with data storage
  • Code generation and execution
  • Multi-domain problem solving

โš™๏ธ Configuration

Key settings in config.py:

# Model settings
GEMINI_MODEL = "gemini-2.0-flash-lite"
TEMPERATURE = 0.1
MAX_TOKENS = 1000

# Agent behavior
MAX_ITERATIONS = 10
VERBOSE = True

# Memory settings
CACHE_TTL = 3600  # 1 hour
MAX_CACHE_SIZE = 1000

๐Ÿงช Testing & Examples

Run the comprehensive demo:

python examples/hybrid_agent_demo.py

Test specific components:

python test_agent.py
python test_memory_integration.py

๐Ÿ” Monitoring & Debugging

Verbose Mode

Enable detailed logging:

agent = ReactAgent(verbose=True)

Memory Statistics

stats = await agent.get_memory_stats()
print(json.dumps(stats, indent=2))

Execution Metadata

response = await agent.run("query")
print(f"Mode used: {response['metadata']['chosen_approach']}")
print(f"Steps taken: {len(response['steps'])}")
print(f"Execution time: {response['metadata']['execution_time']:.2f}s")

๐Ÿšง Extending the System

Adding Custom Tools

  1. Inherit from BaseTool
  2. Implement execute() and get_schema() methods
  3. Register in ToolManager

Custom Memory Types

  1. Extend MemoryStore with new memory types
  2. Implement storage and retrieval logic
  3. Integrate with ContextManager

Multi-Agent Extensions

from extensions.multiagent_framework import MultiAgentSystem

system = MultiAgentSystem()
system.add_agent("researcher", researcher_agent)
system.add_agent("analyzer", analyzer_agent)

result = await system.distribute_task({
    "type": "research_and_analyze",
    "query": "Latest AI developments"
})

๐Ÿ“Š Performance Considerations

  • Memory Optimization: Automatic cleanup of old sessions
  • Caching: Built-in result caching for database operations
  • Parallel Execution: Plan-Execute mode supports parallel step execution
  • Session Management: Efficient LLM session handling

๐Ÿ”’ Security & Privacy

  • API keys stored in environment variables
  • No persistent storage of sensitive data
  • Session isolation for multi-user environments
  • Configurable cache TTL for data expiration

๐Ÿ“š References & Research

This implementation is based on:

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

MIT License - see LICENSE file for details.

๐Ÿ†˜ Troubleshooting

Common Issues

ImportError with langchain packages

pip install --upgrade langchain langgraph langchain-google-genai

API Key Issues

# Check environment variables
echo $GOOGLE_API_KEY

Memory/Performance Issues

# Adjust configuration
Config.MAX_CACHE_SIZE = 500
Config.CACHE_TTL = 1800

Debug Mode

import logging
logging.basicConfig(level=logging.DEBUG)

agent = ReactAgent(verbose=True)

๐ŸŽ‰ What's Next?

  • [ ] Multi-modal tool support (image, audio)
  • [ ] Distributed multi-agent systems
  • [ ] Fine-tuning capabilities
  • [ ] Integration with external APIs
  • [ ] Performance optimization
  • [ ] Advanced planning algorithms

Built with โค๏ธ using LangGraph, LangChain, and Google Gemini