Skip to content

CrossBorder-Co/memory_agent

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Trip Advisor - AI Agent - Multi-LLM Travel Assistant

A sophisticated AI agent featuring multiple LLM provider support (Google Gemini, OpenAI, Groq-DeepSeek), optimized memory management, and real-time streaming capabilities through a Flask API. The agent specializes in providing city information including weather, time, and facts.

๐Ÿš€ Features

  • Multi-LLM Support: Support for Google Gemini, OpenAI, and Groq-DeepSeek models
  • LLM Factory Pattern: Easy switching between different LLM providers
  • City Information Tools: Weather, time, facts, and city visit planning tools
  • Real-time Streaming: Token-by-token streaming responses via Server-Sent Events (SSE)
  • Session Management: Multiple conversation sessions with isolated memory
  • Memory Optimization: Automatic memory cleanup to prevent memory bloat
  • Dynamic Provider Switching: Switch between LLM providers at runtime
  • Web Interface: Modern React-based frontend for interaction
  • API Endpoints: RESTful API for provider management and chat

๐Ÿ—๏ธ Architecture

Core Components

  1. LLM Factory: Factory pattern for creating different LLM providers
  2. TripAgent: Main agent class with memory management
  3. Multi-Provider Support: Google Gemini, OpenAI, and Groq-DeepSeek integration
  4. Flask API: RESTful API with streaming endpoints
  5. Session Management: Thread-safe session handling
  6. City Information Tools: Specialized tools for city-related queries

Memory Management Strategy

  • Session-based Memory: Each conversation session has isolated memory
  • Automatic Optimization: Keeps only recent messages (configurable limit)
  • Thread-safe Operations: Concurrent session handling with locks
  • Memory Status Monitoring: Real-time memory usage tracking

๐Ÿ“‹ Prerequisites

  • Python 3.8+
  • At least one LLM provider API key:
    • Google API Key (for Gemini access)
    • OpenAI API Key (for GPT models)
    • Groq API Key (for DeepSeek models)
  • Modern web browser (for the client interface)

๐Ÿ› ๏ธ Installation

  1. Clone or navigate to the project directory:

    cd /Users/davidbong/Documents/agentic_projects/memory_agentic
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Set up environment variables:

    cp .env.example .env

    Edit .env and add your API keys (at least one is required):

    # LLM Provider Configuration
    GOOGLE_API_KEY=your_google_api_key_here
    OPENAI_API_KEY=your_openai_api_key_here
    GROQ_API_KEY=your_groq_api_key_here
    
    # Default provider (google_gemini, openai, or groq)
    DEFAULT_LLM_PROVIDER=google_gemini
    
    # Model configurations
    GOOGLE_MODEL=gemini-1.5-flash
    OPENAI_MODEL=gpt-3.5-turbo
    GROQ_MODEL=deepseek-r1-distill-llama-70b
    

๐Ÿ”‘ Getting API Keys

Google Gemini API Key

  1. Go to Google AI Studio
  2. Create a new API key
  3. Copy the key and add it to your .env file

OpenAI API Key

  1. Go to OpenAI Platform
  2. Create a new API key
  3. Copy the key and add it to your .env file

Groq API Key

  1. Go to Groq Console
  2. Create a new API key
  3. Copy the key and add it to your .env file

๐Ÿš€ Usage

Starting the Server

python app.py

The server will start on http://localhost:5000

Using the Web Interface

  1. Open client.html in your web browser
  2. The interface will automatically connect to the server
  3. Start chatting with the AI agent

API Endpoints

Chat with Streaming

POST /chat
Content-Type: application/json

{
  "message": "What's the weather like in Paris?",
  "session_id": "user123"
}

Get Available LLM Providers

GET /llm/providers

Switch LLM Provider

POST /llm/switch
Content-Type: application/json

{
  "provider": "openai"
}

Check Memory Status

GET /memory/status/{session_id}

Clear Session Memory

DELETE /memory/clear/{session_id}

Health Check

GET /health

๐Ÿ”ง Configuration

Memory Optimization

You can adjust memory settings in app.py:

# Maximum messages to keep in memory per session
self._optimize_memory(session_id, max_messages=20)

Model Configuration

self.llm = ChatGoogleGenerativeAI(
    model="gemini-pro",
    temperature=0.7,  # Adjust creativity
    streaming=True
)

๐Ÿ› ๏ธ Available Tools

The agent comes with built-in tools:

  1. Search Tool: Simulated web search functionality
  2. Calculator Tool: Mathematical calculations
  3. Memory Info Tool: Conversation memory statistics

Adding Custom Tools

Extend the _create_tools() method in MemoryOptimizedAgent:

def custom_tool(input_text: str) -> str:
    """Your custom tool implementation"""
    return f"Custom result for: {input_text}"

tools.append(Tool(
    name="custom_tool",
    description="Description of what your tool does",
    func=custom_tool
))

๐Ÿ“Š Memory Management Details

Session Isolation

  • Each session has its own ChatMessageHistory
  • Sessions are identified by unique session IDs
  • Memory is isolated between different users/sessions

Automatic Optimization

  • Prevents memory bloat by limiting message history
  • Configurable message limits per session
  • Maintains conversation context while managing resources

Thread Safety

  • Uses threading locks for concurrent access
  • Safe for multiple simultaneous users
  • Session data integrity guaranteed

๐Ÿ” Monitoring and Debugging

Memory Status

Monitor memory usage through the API:

curl http://localhost:5000/memory/status/your_session_id

Logs

The Flask app runs in debug mode and provides detailed logs:

  • Agent reasoning steps
  • Tool executions
  • Memory operations
  • API requests

๐Ÿšจ Troubleshooting

Common Issues

  1. "Google API Key not found"

    • Ensure your .env file contains the correct API key
    • Verify the key is valid and has Gemini API access
  2. "Connection refused"

    • Make sure the Flask server is running
    • Check if port 5000 is available
  3. "Streaming not working"

    • Ensure your browser supports Server-Sent Events
    • Check browser console for JavaScript errors
  4. "Memory not persisting"

    • Verify session IDs are consistent
    • Check server logs for memory optimization triggers

Performance Tips

  • Adjust max_messages based on your memory requirements
  • Use shorter session IDs for better performance
  • Monitor memory usage in production environments
  • Consider implementing persistent storage for long-term memory

๐Ÿ”ฎ Future Enhancements

  • Persistent memory storage (Redis/Database)
  • Advanced memory summarization
  • Custom tool marketplace
  • Multi-modal capabilities
  • Advanced session analytics
  • WebSocket support for real-time updates

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.


Built with โค๏ธ using LangChain, Google Gemini, and Flask# ๐Ÿ™๏ธ Trip Advisor - AI Agent

An intelligent AI agent that helps users gather factual information about cities worldwide. This assistant demonstrates advanced agentic capabilities including tool orchestration, function calling, contextual dialogue handling, streaming API interface, and transparent reasoning.

โœจ Features

Core Capabilities

  • ๐ŸŒค๏ธ Weather Information: Get current weather conditions for any city
  • ๐Ÿ• Local Time: Check the current time in different cities worldwide
  • ๐Ÿ“ City Facts: Learn about city demographics, location, and interesting facts
  • ๐Ÿ—บ๏ธ Visit Planning: Comprehensive city visit planning using multiple tools

Technical Features

  • Tool Orchestration: Seamless integration of multiple specialized tools
  • Function Calling: Structured output with reasoning transparency
  • Multi-turn Dialogue: Context-aware conversations with memory
  • Streaming API: Real-time response streaming
  • Transparent Reasoning: See the agent's thinking process

๐Ÿ› ๏ธ Architecture

Backend (Python/Flask)

  • LangChain React Agent: Advanced reasoning and tool orchestration
  • Google Gemini Integration: Powered by Gemini-1.5-flash model
  • Memory Management: Persistent conversation history
  • RESTful API: Clean endpoints for frontend integration

Frontend (React/Next.js)

  • Modern UI: Beautiful, responsive interface
  • Real-time Chat: Streaming responses with typing indicators
  • Session Management: Multiple conversation sessions
  • Memory Visualization: View conversation history and memory status

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8+
  • Node.js 18+
  • Google API Key (for Gemini)

Backend Setup

  1. Clone and navigate to the project:

    git clone https://github.com/bitlabsdevteam/memory_agent.git
    cd memory_agent
  2. Set up environment:

    cp .env.example .env
    # Edit .env and add your GOOGLE_API_KEY
  3. Install dependencies and start:

    chmod +x start.sh
    ./start.sh

    The backend will be available at http://localhost:5001

Frontend Setup

  1. Navigate to frontend directory:

    cd frontend
  2. Install dependencies:

    npm install
  3. Start development server:

    npm run dev

    The frontend will be available at http://localhost:3000

๐Ÿ”ง Tools & APIs

The assistant implements the following specialized tools:

Tool Purpose Implementation
WeatherTool Get current weather for a city Mock data (production: OpenWeatherMap API)
TimeTool Get current time in a city Timezone calculations with UTC offsets
CityFactsTool Get basic facts about a city Mock data (production: GeoDB Cities API)
PlanMyCityVisitTool Composite tool for visit planning Orchestrates multiple tools with reasoning

๐Ÿ’ฌ Example Interactions

Simple Queries

User: "What's the weather like in Paris?"
Assistant: Uses WeatherTool โ†’ "Current weather in Paris: 23ยฐC, clear skies, humidity 65%"

Complex Planning

User: "Plan my visit to Tokyo"
Assistant: Uses PlanMyCityVisitTool โ†’ Orchestrates multiple tools:
1. Gets city facts about Tokyo
2. Fetches current weather
3. Checks local time
4. Provides comprehensive visit summary

Follow-up Questions

User: "What about the weather there?"
Assistant: Uses conversation context โ†’ Provides weather for previously mentioned city

๐Ÿ“ Project Structure

memory_agent/
โ”œโ”€โ”€ app.py                 # Main Flask application
โ”œโ”€โ”€ config.py             # Configuration management
โ”œโ”€โ”€ requirements.txt      # Python dependencies
โ”œโ”€โ”€ start.sh             # Startup script
โ”œโ”€โ”€ .env.example         # Environment template
โ”œโ”€โ”€ frontend/            # React/Next.js frontend
โ”‚   โ”œโ”€โ”€ src/app/
โ”‚   โ”‚   โ”œโ”€โ”€ components/  # React components
โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx     # Main page
โ”‚   โ”‚   โ””โ”€โ”€ layout.tsx   # App layout
โ”‚   โ””โ”€โ”€ package.json     # Node dependencies
โ””โ”€โ”€ README.md           # This file

๐Ÿ”‘ Configuration

Key configuration options in config.py:

  • GOOGLE_MODEL: AI model (default: gemini-1.5-flash)
  • AGENT_TEMPERATURE: Response creativity (0.0-1.0)
  • MEMORY_MAX_MESSAGES: Conversation memory limit
  • STREAMING_ENABLED: Real-time response streaming
  • FLASK_PORT: Backend server port

๐ŸŒ API Endpoints

  • GET /health - Health check
  • POST /chat - Main chat endpoint (streaming)
  • GET /memory/status/<session_id> - Memory status
  • DELETE /memory/clear/<session_id> - Clear memory

๐Ÿงช Testing

Test the assistant with various queries:

  1. Weather queries: "What's the weather in London?"
  2. Time queries: "What time is it in Sydney?"
  3. City facts: "Tell me about New York"
  4. Planning: "Plan my visit to Berlin"
  5. Follow-ups: "What about the weather there?"

๐Ÿš€ Production Deployment

For production use:

  1. Replace mock data with real APIs:

    • OpenWeatherMap for weather
    • World Time API for time zones
    • GeoDB Cities API for city facts
  2. Environment setup:

    • Use production WSGI server (gunicorn)
    • Set up proper environment variables
    • Configure CORS for your domain
  3. Frontend deployment:

    • Build optimized bundle: npm run build
    • Deploy to Vercel, Netlify, or similar

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This project is open source and available under the MIT License.

About

Memory Agentic Solution

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 98.4%
  • Shell 1.3%
  • Dockerfile 0.3%