Skip to content

patchen0518/Agent_Oracle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

60 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Oracle: An Intelligent Conversational AI Agent

Project Status: MVP 1.3 Complete LangChain Integration Backend Tests Frontend Tests

Oracle is an intelligent, conversational AI agent designed to provide context-aware responses with session memory. Built with a modern Python FastAPI backend and a responsive React frontend, it demonstrates production-ready architecture and comprehensive testing.

๐ŸŽฏ Project Goal

The primary goal is to build a robust, well-designed, and maintainable software project that replicates and expands upon the core functionalities of modern AI assistants like Google Gemini or ChatGPT. This includes integrating with a large language model (LLM), managing conversational memory, and enabling the agent to use external tools like web search.


๐Ÿ—บ๏ธ Project Status & Roadmap

โœ… MVP 1.0: Core Chat (COMPLETE)

  • Backend: FastAPI server with comprehensive error handling and logging
  • Frontend: React (with Vite) single-page chat application with responsive UI
  • Core Logic: Full integration with Gemini API with configurable models
  • Memory: Stateless conversation history passed with each request
  • Testing: Comprehensive test suite with 80%+ coverage
  • Features:
    • Real-time chat interface with message history
    • Configurable AI personality via system instructions
    • Model selection via environment variables
    • Production-ready error handling and monitoring
    • Health check endpoints and service diagnostics

โœ… MVP 1.1: Session Management & Persistence (COMPLETE)

  • Database: โœ… SQLite integration with SQLModel for type-safe database operations
  • Session Storage: โœ… Persistent chat sessions with message history stored server-side
  • Performance: โœ… Significant reduction in API token usage and improved response times
  • API Migration: โœ… Complete replacement of stateless endpoints with session-based architecture
  • Frontend Migration: โœ… Complete UI overhaul to support session management
  • Features:
    • โœ… Create, manage, and switch between multiple chat sessions
    • โœ… Persistent conversation history stored in database
    • โœ… Session metadata (title, creation date, message count, model used)
    • โœ… Optimized token usage by maintaining server-side context
    • โœ… Basic health monitoring with session metrics
    • Frontend UI Updates:
      • โœ… Session sidebar for managing multiple conversations
      • โœ… Session creation, switching, and management interface
      • โœ… Session metadata display (title, message count, last activity)
      • โœ… Removal of client-side conversation history storage
      • โœ… Session-based chat interface with context headers
      • โœ… Mobile-responsive session management

โœ… MVP 1.2: Optimized Session Management (COMPLETE)

  • Session Management: Optimized Gemini API sessions with intelligent memory management
  • Performance Optimization: 60-80% reduction in API token usage and 30-50% faster response times
  • Context Handling: Efficient context restoration using recent message history (last 10 messages)
  • Memory Management: Smart session caching with automatic cleanup and configurable limits
  • Database Integration: All conversations reliably stored in SQLite with proper indexing
  • Features:
    • โœ… Optimized Gemini chat sessions cached in memory for active conversations
    • โœ… Automatic session cleanup with 1-hour expiration (configurable)
    • โœ… Context restoration from database using recent message history
    • โœ… Standardized error handling with custom exception hierarchy
    • โœ… Database performance optimizations (95% faster message counting)
    • โœ… Simplified configuration with essential environment variables only

โœ… MVP 1.3: LangChain Integration & Smart Memory (COMPLETE)

  • LangChain Integration: ChatGoogleGenerativeAI with intelligent conversation management
  • Memory Management: Smart memory strategies (buffer, summary, entity, hybrid) for optimal context handling
  • Context Optimization: Automatic summarization and relevance-based context selection
  • Entity Extraction: Remember important facts and preferences within sessions
  • Performance Enhancement: 60-80% reduction in API token usage through intelligent memory strategies
  • Fallback Mechanisms: Graceful degradation when advanced features fail
  • Features:
    • โœ… Intelligent conversation summarization for long chats
    • โœ… Entity extraction and fact retention within sessions
    • โœ… Optimized context selection for better AI responses
    • โœ… Multiple memory strategies (buffer, summary, entity, hybrid)
    • โœ… Enhanced conversation continuity and relevance
    • โœ… Comprehensive error handling and fallback mechanisms
    • โœ… Feature flag support for gradual rollout

๐Ÿ”ฎ Future Phases (Planned)

MVP 1.4: Web Search & Tool Use

  • Capability: Agent will perform internet searches to answer questions
  • Mechanism: Gemini API's function calling (tool use) feature integrated with sessions
  • Context: Tools will have access to session history and extracted entities

MVP 1.5: Agentic Reflection (Self-Correction)

  • Capability: Agent will review search results before responding
  • Mechanism: Self-correction loop managed with LangChain framework
  • Session Integration: Reflection results stored in session context

MVP 2.0: Advanced Session Features

  • Multi-User Support: User authentication and personal session management
  • Session Sharing: Ability to share sessions between users
  • Session Templates: Pre-configured session types for different use cases
  • Export/Import: Session backup and restoration capabilities

MVP 3.0: Long-Term Memory & RAG

  • Vector Database: Integration with vector database for semantic memory
  • Cross-Session Memory: User preferences and facts remembered across all sessions
  • RAG Integration: Retrieval-augmented generation for enhanced responses
  • Knowledge Base: Personal knowledge base built from conversation history

๐Ÿ›๏ธ Architecture & Implementation

The project implements a production-ready, decoupled frontend/backend architecture.

Current Architecture (MVP 1.3)

Backend (FastAPI)

  • API Layer: RESTful session-based endpoints with standardized error handling
  • Business Logic: Modular service layer with custom exception hierarchy
  • AI Integration: Optimized Gemini API client with in-memory session caching (max 50 sessions)
  • Database: SQLite with SQLModel for persistent storage and optimized queries
  • Memory Management: Hybrid approach - database persistence + in-memory caching for performance
  • Context Optimization: Intelligent context restoration using recent message history (last 10 messages)
  • Monitoring: Basic health checks with session metrics
  • Configuration: Simplified environment-based configuration with essential variables only

Frontend (React + Vite)

  • UI Components: Session-aware chat interface with multi-session management
  • State Management: React hooks for session state and error handling
  • Session Management: Complete session sidebar with creation, switching, and deletion
  • Memory Management: Server-side conversation history (no client-side storage)
  • API Communication: Session-based service layer with error recovery
  • Testing: Component and integration tests with React Testing Library

Future Architecture (MVP 1.4+)

Enhanced Backend (MVP 1.4+)

  • Memory Optimization: Intelligent context management with LangChain integration
  • Conversation Summarization: Automatic summarization of long conversations
  • Entity Extraction: Smart extraction and retention of important facts
  • Advanced Features: Cross-session memory, conversation summarization, and smart context selection

Enhanced Frontend (Future)

  • Advanced Session Features: Session templates, sharing, and collaboration
  • Enhanced UX: Advanced search, conversation export, and session organization
  • Performance Optimization: Lazy loading, virtual scrolling, and caching
  • Accessibility: Enhanced screen reader support and keyboard navigation

Key Features Implemented

  • Session Management: Complete multi-session support with persistent storage and in-memory caching
  • Database Integration: SQLite with SQLModel for type-safe operations and optimized queries
  • Configurable AI Models: Switch between Gemini models via environment variables
  • System Instructions: Multiple AI personality types (default, professional, technical, creative, educational)
  • Performance Optimization: 60-80% reduction in API token usage through intelligent session caching
  • Error Handling: Standardized exception hierarchy with production-grade error handling
  • Logging & Monitoring: Structured logging with session context and basic health monitoring
  • Testing: Full test coverage for both backend (128 tests) and frontend (137 tests) components

โœจ Design Principles

  • Modularity: The frontend, backend, and agent logic are strictly decoupled to allow for independent development, testing, and scaling.
  • Simplicity: We prioritize the simplest, cleanest solution that meets the requirements for the current MVP, avoiding over-engineering.
  • Performance: We use high-performance, asynchronous frameworks (FastAPI, uvicorn) and efficient frontend libraries (React) to ensure a responsive user experience.
  • Scalability: The architecture is designed to be stateless (where possible) to support future scaling (e.g., containerization, serverless deployment).
  • Testability: Logic, especially in the backend, is written in a way that encourages unit and integration testing.
  • Current Documentation: All API integrations must reference the latest official documentation via Context 7 (MCP) to ensure implementations follow current best practices.

๐Ÿ“š Documentation

Core Documentation

LangChain Integration Documentation

Configuration and Setup

  • Memory Strategies: Buffer, summary, entity, and hybrid memory management
  • Context Optimization: Automatic summarization and relevance-based selection
  • Feature Flags: Gradual rollout and rollback procedures
  • Performance Monitoring: Token usage tracking and optimization metrics

๐Ÿ“ Project Structure

The project follows a clean, modular architecture with clear separation between frontend and backend:

oracle/
โ”œโ”€โ”€ backend/                    # FastAPI backend with LangChain integration
โ”‚   โ”œโ”€โ”€ api/v1/                # Session-based API endpoints
โ”‚   โ”œโ”€โ”€ config/                # Database and system configuration
โ”‚   โ”œโ”€โ”€ models/                # SQLModel data models
โ”‚   โ”œโ”€โ”€ services/              # Business logic and LangChain integration
โ”‚   โ”œโ”€โ”€ tests/                 # Comprehensive test suite (128+ tests)
โ”‚   โ”œโ”€โ”€ utils/                 # Logging and utility modules
โ”‚   โ””โ”€โ”€ main.py               # FastAPI application entry point
โ”œโ”€โ”€ frontend/                  # React frontend with session management
โ”‚   โ”œโ”€โ”€ src/components/       # Session-aware UI components
โ”‚   โ”œโ”€โ”€ src/hooks/            # Custom React hooks for session management
โ”‚   โ”œโ”€โ”€ src/services/         # API communication layer
โ”‚   โ””โ”€โ”€ src/test/            # Frontend test suite (137+ tests)
โ”œโ”€โ”€ docs/                     # Comprehensive documentation
โ”œโ”€โ”€ scripts/                  # Development and deployment scripts
โ””โ”€โ”€ .kiro/                    # Kiro IDE configuration and specs

๐Ÿ› ๏ธ Tech Stack

Component Technology Package(s) / Tool(s) Status
Backend Python 3.14+ fastapi, uvicorn, google-genai, python-dotenv โœ… Implemented
Database SQLite sqlmodel, sqlalchemy โœ… Implemented
Frontend JavaScript / React react, vite, axios โœ… Implemented
Testing Python pytest, pytest-mock, pytest-asyncio โœ… Implemented
Testing JavaScript vitest, @testing-library/react โœ… Implemented
Package Mgmt Python uv โœ… Implemented
Package Mgmt Node.js npm โœ… Implemented
Logging Python Custom structured logging โœ… Implemented
Session Mgmt Python SQLModel + SQLite โœ… Implemented
Memory & AI Python langchain, langchain-google-genai, langchain-core โœ… Implemented

๏ฟฝ Develop ment Standards

Library and API Integration Requirements

MANDATORY: Before implementing any library integration or external API, developers must:

  1. Use Context 7 (MCP) to lookup the latest official documentation
  2. Verify current best practices and check for any recent changes or deprecations
  3. Implement based on latest specifications found in the documentation
  4. Document the library/API version and retrieval date in code comments

This ensures all implementations follow current standards and avoid issues with outdated patterns.

Key Libraries and APIs Requiring Context 7 Lookup

Backend

  • FastAPI: Routing, middleware, validation patterns
  • Gemini API: Authentication, chat sessions, message formatting, error handling
  • SQLModel: Database model definitions, relationships, and query patterns
  • SQLAlchemy: Database operations, migrations, and connection management
  • SQLite: Database configuration, optimization, and best practices
  • Pydantic: Model validation and serialization
  • pytest: Testing patterns and fixtures

Frontend

  • React: Hooks, components, state management patterns
  • Vite: Build configuration and development setup
  • Axios: HTTP client configuration and best practices
  • React Testing Library: Component testing patterns

Future Integrations

  • LangChain (MVP 1.3+): Agent orchestration and framework patterns
  • Web Search APIs (MVP 1.4+): Integration and usage patterns

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.14+ and uv (as specified in .python-version)
  • Node.js 18+ and npm
  • A Gemini API Key from Google AI Studio
  • Context 7 (MCP) configured for API documentation lookup

Configuration

  1. Clone this repository
  2. Copy the environment template and configure your settings:
# Backend configuration
cp backend/.env.example backend/.env

# Frontend configuration (optional)
cp frontend/.env.example frontend/.env
  1. Edit backend/.env with your configuration:
# Required: Gemini API key from https://aistudio.google.com/app/apikey
GEMINI_API_KEY=your_gemini_api_key_here

# Optional: Application configuration
ENVIRONMENT=development
LOG_LEVEL=info
LOG_FILE=logs/backend.log

# Optional: Choose your AI model
GEMINI_MODEL=gemini-2.5-flash

# Optional: Set AI personality
SYSTEM_INSTRUCTION_TYPE=default

# Optional: Database configuration
DATABASE_URL=sqlite:///./oracle_sessions.db

# Testing configuration (used by test suite)
TESTING=false

Quick Setup

# Run the automated setup script
./scripts/dev-setup.sh

# Edit backend/.env and add your Gemini API key
# GEMINI_API_KEY=your_api_key_here

Database Management

Oracle includes a database management script for development:

# View database statistics
uv run python backend/scripts/manage_db.py stats

# Clean all sessions and messages (with confirmation)
uv run python backend/scripts/manage_db.py clean

# Reset database completely (with confirmation)
uv run python backend/scripts/manage_db.py reset

# Initialize database tables
uv run python backend/scripts/manage_db.py init

Note: Tests use separate in-memory databases and won't affect your main database.

Manual Setup

# Create virtual environment and install backend dependencies
uv venv
uv pip install -e ".[dev]"

# Set up environment files
cp backend/.env.example backend/.env
cp frontend/.env.example frontend/.env
# Edit backend/.env with your Gemini API key

# Install frontend dependencies
cd frontend
npm install
cd ..

Running the Application

# Terminal 1: Start backend
cd backend
uv run uvicorn main:app --reload --host 0.0.0.0 --port 8000

# Terminal 2: Start frontend
cd frontend
npm run dev

The application will be running on http://localhost:5173.

API Documentation

When the backend is running, FastAPI automatically generates interactive API documentation:

Testing

The project includes comprehensive test coverage for both backend and frontend.

Backend Tests

# Run all tests
uv run pytest

# Run with coverage report
uv run pytest --cov=backend --cov-report=html

# Run specific test categories
uv run pytest backend/tests/test_main.py -v                    # API integration tests
uv run pytest backend/tests/test_gemini_integration.py -v      # Service layer tests
uv run pytest backend/tests/test_models.py -v                 # Data model tests
uv run pytest backend/tests/test_e2e_integration.py -v        # End-to-end tests

# Run tests with detailed output
uv run pytest -v --tb=short

Frontend Tests

cd frontend

# Run all tests once
npm test

# Run tests in watch mode (development)
npm run test:watch

# Run tests with UI (interactive)
npm run test:ui

# Run specific test files
npm test -- ChatInterface.test.jsx

Test Coverage

  • Backend: 128 tests passing - Complete session management and API coverage
  • Frontend: 137 tests passing - Full UI component and integration coverage
  • E2E: Complete user workflow testing from session creation to chat
  • API: Comprehensive endpoint testing with error scenarios and edge cases
  • Database: Full model validation and relationship testing
  • Session Management: Complete CRUD operations and cascade deletion testing

๐Ÿ”Œ API Endpoints

Session Management Endpoints

POST /api/v1/sessions/

Create a new chat session.

Request Body:

{
  "title": "My New Session",
  "model_used": "gemini-2.0-flash-exp",
  "session_metadata": {}
}

Response:

{
  "id": 1,
  "title": "My New Session",
  "model_used": "gemini-2.0-flash-exp",
  "session_metadata": {},
  "created_at": "2025-01-27T12:00:00Z",
  "updated_at": "2025-01-27T12:00:00Z",
  "message_count": 0
}

GET /api/v1/sessions/

List all sessions with pagination support.

Query Parameters:

  • skip (optional): Number of sessions to skip (default: 0)
  • limit (optional): Maximum sessions to return (default: 50)

GET /api/v1/sessions/{session_id}

Get details for a specific session.

DELETE /api/v1/sessions/{session_id}

Delete a session and all its messages.

Session Chat Endpoints

POST /api/v1/sessions/{session_id}/chat

Send a message within a session context.

Request Body:

{
  "message": "What is FastAPI?"
}

Response:

{
  "user_message": {
    "id": 1,
    "session_id": 1,
    "role": "user",
    "content": "What is FastAPI?",
    "timestamp": "2025-01-27T12:00:00Z"
  },
  "assistant_message": {
    "id": 2,
    "session_id": 1,
    "role": "assistant",
    "content": "FastAPI is a modern, fast web framework for building APIs with Python 3.7+...",
    "timestamp": "2025-01-27T12:00:01Z"
  },
  "session": {
    "id": 1,
    "title": "My New Session",
    "message_count": 2,
    "updated_at": "2025-01-27T12:00:01Z"
  }
}

GET /api/v1/sessions/{session_id}/messages

Get message history for a session with pagination support.

Query Parameters:

  • skip (optional): Number of messages to skip (default: 0)
  • limit (optional): Maximum messages to return (default: 50)

Monitoring Endpoints

GET /health

Overall system health check with session metrics.

Response:

{
  "status": "healthy",
  "timestamp": "2025-01-27T12:00:00Z",
  "services": {
    "gemini_api": "configured",
    "database": "connected",
    "logging": "active"
  },
  "session_metrics": {
    "total_sessions": 5,
    "active_sessions": 2,
    "total_messages": 47
  },
  "version": "1.3.0"
}

GET /health

Basic system health check with session metrics.

GET /

API status endpoint.

Configuration Options

Available AI Models

  • gemini-2.5-flash (default) - Balanced performance and speed
  • gemini-2.5-flash-lite - Faster responses, lighter processing
  • gemini-1.5-pro - Enhanced reasoning capabilities
  • gemini-1.5-flash - Fast responses with good quality

Available AI Personalities

  • default - General purpose helpful assistant
  • professional - Business and productivity focused
  • technical - Software development specialist
  • creative - Creative and engaging conversational style
  • educational - Teaching and learning focused

Set via SYSTEM_INSTRUCTION_TYPE in your .env file.


๐Ÿš€ Current Features (MVP 1.3)

LangChain-Enhanced Session Management

  • Multi-Session Support: Create, manage, and switch between multiple independent chat sessions
  • Intelligent Memory Strategies: Buffer, summary, entity, and hybrid memory types for optimal context handling
  • Context Optimization: Automatic summarization and relevance-based context selection
  • Entity Extraction: Remembers important facts and preferences within sessions
  • Persistent Storage: All conversation history stored server-side in SQLite database with memory coordination
  • Performance Optimization: 60-80% reduction in API token usage through smart memory strategies
  • Fallback Mechanisms: Graceful degradation when advanced features fail

LangChain-Enhanced Chat Functionality

  • Real-time Conversation: Seamless chat interface with intelligent memory management
  • Smart Context Awareness: AI maintains conversation context using LangChain memory strategies
  • Conversation Summarization: Automatic summarization of long conversations for token efficiency
  • Entity Memory: Extracts and remembers important facts, names, and preferences
  • Multiple AI Models: Switch between different Gemini models via configuration
  • AI Personalities: Choose from 5 different AI personality types (default, professional, technical, creative, educational)
  • Intelligent Error Recovery: Comprehensive error handling with fallback mechanisms

LangChain Memory Architecture

  • Smart Memory Strategies: Buffer, summary, entity, and hybrid memory types
  • Context Optimization: Automatic summarization and relevance-based context selection
  • Entity Extraction: Intelligent extraction and retention of important facts
  • Token Efficiency: 60-80% reduction in API token usage through smart memory management
  • Hybrid Persistence: Database storage coordinated with LangChain memory strategies
  • Performance Benefits: Optimized context handling with improved conversation quality

User Interface

  • Session Sidebar: Dedicated panel for managing multiple conversations
  • Session Controls: Create, switch, rename, and delete sessions with intuitive controls
  • Mobile Responsive: Adaptive session management for mobile devices
  • Real-Time Updates: Live session synchronization and message updates
  • Accessibility: Built with accessibility best practices and ARIA support

Technical Features

  • LangChain Integration: ChatGoogleGenerativeAI with intelligent conversation management
  • Database Integration: SQLite with SQLModel coordinated with LangChain memory strategies
  • Smart Memory Management: Multiple memory strategies with automatic optimization
  • Context Optimization: Automatic summarization and relevance-based context selection
  • Error Handling: Comprehensive exception hierarchy with fallback mechanisms
  • Production Ready: Comprehensive logging, monitoring, and graceful error handling
  • Feature Flags: Gradual rollout and rollback capabilities for LangChain features
  • Comprehensive Testing: Full test coverage (128+ backend + 137+ frontend tests)
  • API Documentation: Auto-generated OpenAPI/Swagger documentation with LangChain details
  • Advanced Monitoring: LangChain-specific health checks and performance metrics

๐Ÿ”ฎ Upcoming Features (MVP 1.4+)

Web Search & Tool Use (MVP 1.4)

  • Internet Search Integration: Agent will perform web searches to answer questions
  • Function Calling: Gemini API's tool use feature integrated with LangChain sessions
  • Context-Aware Tools: Tools will have access to session history and extracted entities
  • Search Result Processing: Intelligent processing and integration of search results

Agentic Reflection (MVP 1.5)

  • Self-Correction Loop: Agent will review and improve responses before delivery
  • Quality Assessment: Automatic evaluation of response quality and accuracy
  • Iterative Improvement: Multi-step reasoning and response refinement

ion management

Completed: LangChain Integration & Smart Memory (MVP 1.3)

Objective: โœ… COMPLETE - Implemented intelligent conversation memory with LangChain integration

Implemented Changes:

  • โœ… LangChain Integration: ChatGoogleGenerativeAI with intelligent conversation management
  • โœ… Smart Memory Strategies: Buffer, summary, entity, and hybrid memory types for optimal context handling
  • โœ… Context Optimization: Automatic summarization and relevance-based context selection
  • โœ… Entity Extraction: Remembers important facts and preferences within sessions
  • โœ… Performance Enhancement: 60-80% reduction in API token usage through intelligent memory strategies
  • โœ… Fallback Mechanisms: Graceful degradation when advanced features fail
  • โœ… Feature Flag Support: Gradual rollout and rollback procedures

Achieved Benefits:

  • โœ… Intelligent conversation summarization for long chats
  • โœ… Entity extraction and fact retention within sessions
  • โœ… Optimized context selection for better AI responses
  • โœ… Multiple memory strategies for different conversation types
  • โœ… Enhanced conversation continuity and relevance
  • โœ… Comprehensive error handling and fallback mechanisms

๐Ÿค Contributing

This project follows strict development standards:

  1. Context 7 Documentation Lookup: All library integrations must reference latest official documentation
  2. Comprehensive Testing: Maintain 80%+ test coverage for new features
  3. Code Standards: Follow the established patterns in .kiro/steering/ guidelines
  4. Security First: All secrets via environment variables, input validation, and error handling

See the development guidelines in .kiro/steering/ for detailed standards.


๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Agent that will have its own memory about you

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published