Skip to content

Latest commit

 

History

26 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 RAG System with ML Intelligence

Advanced Retrieval-Augmented Generation system with Machine Learning optimization

Python 3.9+ License: MIT Streamlit

Course: CSAI 302 - Advanced Database | Institution: ZC-UST | Year: 2024-2025


📑 Table of Contents


🎯 Project Overview

An intelligent Retrieval-Augmented Generation (RAG) system that combines semantic search with Large Language Models to provide accurate, context-aware answers from your documents. Enhanced with Machine Learning to continuously improve through user feedback.

Key Highlights

  • 🔍 Semantic Search - Find relevant information using meaning, not just keywords
  • 🤖 LLM Integration - Google Gemini for intelligent answer generation
  • 🧠 ML Optimization - Learns from feedback to improve retrieval parameters
  • 📊 User Feedback - Star ratings and continuous improvement loop
  • 🎨 Modern UI - Streamlit interface with real-time chat
  • 💾 Persistent Storage - ChromaDB vector database with local storage
  • 🚀 Production Ready - Complete error handling, logging, and testing

Technology Stack

Component Technology Purpose
Embeddings all-MiniLM-L6-v2 384-dim sentence embeddings (local)
Vector DB ChromaDB Persistent vector storage
LLM Google Gemini Answer generation
ML Framework XGBoost + scikit-learn Parameter optimization
UI Streamlit Web interface
Feedback JSONL User feedback storage

✨ Features

Core Features

✅ Document Processing

  • TXT file ingestion
  • Intelligent text chunking (recursive/fixed/semantic)
  • Metadata extraction and storage
  • Batch processing support

✅ Semantic Search

  • Dense retrieval using sentence transformers
  • Cosine similarity scoring
  • Top-K retrieval with threshold filtering
  • Context-aware chunk selection

✅ Answer Generation

  • Google Gemini API integration
  • Context-aware prompting
  • Temperature and parameter control
  • Source attribution

✅ Web Interface

  • Modern chat-style UI
  • Real-time query processing
  • Source document display
  • Parameter tuning controls
  • Conversation history

Advanced Features (Bonus +10%)

🤖 Machine Learning Integration (+5%)

  • Parameter Optimizer - Predicts optimal top_k and threshold per query
  • Difficulty Predictor - Identifies challenging queries (85-90% accuracy)
  • Quality Predictor - Predicts user rating before delivery
  • Feature Engineering - 40+ features from queries and responses
  • Continuous Learning - Auto-retrains every 100 feedbacks
  • ML Insights - Shows optimization decisions in UI

📊 Feedback System (+5%)

  • Star rating system (1-5 stars)
  • Quick thumbs up/down buttons
  • Optional text comments
  • Real-time analytics dashboard
  • Feedback enforcement (locks after 5 unrated queries)
  • Improvement report generation
  • Low-rated query tracking

🏗️ Architecture

System Flow

┌─────────────────────────────────────────────────────────────────┐
│                    RAG SYSTEM ARCHITECTURE                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Documents → Chunking → Embeddings → ChromaDB                   │
│                                         ↓                       │
│  Query → Embeddings → Retrieval → Context                       │
│                          ↓                                      │
│                    [ML Optimizer]                               │
│                          ↓                                      │
│                   Gemini LLM → Answer                           │
│                          ↓                                      │
│                    User Feedback                                │
│                          ↓                                      │
│                    ML Training                                  │
│                          ↓                                      │
│                 Improved Parameters                             │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

ML Enhancement Flow

Query → Feature Extraction → ML Models → Optimized Parameters
                                  ↓
                            - top_k (1-10)
                            - threshold (0.3-0.9)
                            - difficulty score
                                  ↓
                        Enhanced Retrieval → Better Results

For detailed architecture, see docs/ARCHITECTURE.md


⚡ Quick Start

Prerequisites

  • Python 3.9 or higher
  • Google Gemini API key (Get it here)
  • 2GB RAM minimum
  • 1GB disk space

5-Minute Setup

# 1. Clone and navigate
git clone https://github.com/ahmedm0ssad/RAG-Optimization-System.git
cd RAG-Optimization-System

# 2. Create virtual environment
python -m venv venv
venv\Scripts\activate  # Windows
# source venv/bin/activate  # Linux/Mac

# 3. Install dependencies
pip install -r requirements.txt

# 4. Configure API key
copy .env.example .env  # Windows
# cp .env.example .env  # Linux/Mac

# Edit .env and add your GOOGLE_API_KEY

# 5. Launch UI
streamlit run ui/streamlit_app.py

First Query

from src.generation.rag_pipeline import RAGPipeline

# Initialize (auto-indexes documents on first run)
pipeline = RAGPipeline()

# Query
result = pipeline.query("What is machine learning?")
print(result['answer'])

📥 Installation

Step 1: Environment Setup

# Create and activate virtual environment
python -m venv venv

# Windows
venv\Scripts\activate

# Linux/Mac
source venv/bin/activate

Step 2: Install Dependencies

# Install all packages
pip install -r requirements.txt

Core Dependencies:

  • sentence-transformers>=2.2.2 - Embeddings
  • chromadb>=0.4.18 - Vector database
  • google-generativeai>=0.2.0 - Gemini API
  • streamlit>=1.29.0 - Web UI
  • scikit-learn>=1.3.0 - ML framework
  • xgboost>=2.0.0 - ML models

Step 3: Configure Environment

Create .env file:

# Required
GOOGLE_API_KEY=your_gemini_api_key_here

# Optional ML Settings
ML_ENABLED=true
ML_AUTO_OPTIMIZE=true
ML_MIN_TRAINING_SAMPLES=10

Step 4: Prepare Data

# Add your documents to data/raw/
# Supported: .txt files

# Example structure:
data/raw/
├── machine_learning.txt
├── databases.txt
└── python_programming.txt

Step 5: Verify Installation

# Test setup
python -c "from sentence_transformers import SentenceTransformer; print('✅ Setup OK')"

# Run demo
python scripts/demo.py

🎮 Usage

Command Line Interface

from src.generation.rag_pipeline import RAGPipeline

# Initialize with ML enabled
pipeline = RAGPipeline(use_ml=True)

# Index documents (first time only)
stats = pipeline.index_documents("./data/raw")
print(f"Indexed {stats['total_chunks']} chunks")

# Query with custom parameters
result = pipeline.query(
    question="How do neural networks work?",
    top_k=5,
    temperature=0.7
)

# Access results
print(f"Answer: {result['answer']}")
print(f"Sources: {len(result['retrieved_chunks'])} chunks")
print(f"Scores: {result['scores']}")

# ML insights (if available)
if result.get('ml_insights'):
    print(f"ML optimized top_k: {result['ml_insights']['parameter_optimization']['optimized_top_k']}")

Web UI

# Launch Streamlit interface
streamlit run ui/streamlit_app.py

Features:

  • 💬 Chat interface with conversation history
  • 📚 Source document display with confidence scores
  • ⚙️ Adjustable parameters (top_k, temperature)
  • ⭐ Feedback system with star ratings
  • 🤖 ML insights visualization
  • 📊 Real-time analytics dashboard

Demo Script

# Run complete demonstration
python scripts/demo.py

Demonstrates:

  • Document indexing
  • Query processing
  • ML optimization
  • Feedback collection
  • System analytics

🤖 ML Integration

Overview

The system uses Machine Learning to automatically optimize retrieval parameters based on user feedback, improving answer quality over time.

ML Models

  1. Parameter Optimizer

    • Predicts optimal top_k (1-10) and threshold (0.3-0.9)
    • Based on query complexity and historical performance
    • XGBoost multi-output regression
    • RMSE ~0.5 (within ±1 of optimal)
  2. Difficulty Predictor

    • Classifies queries as easy/difficult
    • Adjusts resources for challenging queries
    • 85-90% accuracy
    • Binary XGBoost classifier
  3. Quality Predictor

    • Predicts user rating (1-5 stars)
    • Pre-delivery quality assessment
    • MAE ~0.5 stars
    • XGBoost regressor

Training ML Models

# Step 1: Collect feedback (10+ samples minimum, 50+ recommended)
streamlit run ui/streamlit_app.py
# Ask questions and rate responses

# Step 2: Train models
python tests/train_ml_models.py

# Step 3: Models auto-load on next query

Using ML Features

# ML enabled by default
pipeline = RAGPipeline(use_ml=True)

# Query uses ML optimization automatically
result = pipeline.query("Complex question here")

# Check ML insights
insights = result['ml_insights']
print(f"Optimized top_k: {insights['parameter_optimization']['optimized_top_k']}")
print(f"Query difficulty: {insights['parameter_optimization']['difficulty']}")

Expected Performance

Metric Without ML With ML Improvement
Avg Rating 3.2⭐ 3.7-4.2⭐ +0.5-1.0
Low Ratings (<3) 30% 10-15% -15-20%
User Satisfaction 65% 80-85% +15-20%

For detailed ML guide, see docs/ML_INTEGRATION_GUIDE.md


📚 Documentation

Core Documentation

API Reference

RAGPipeline

pipeline = RAGPipeline(use_ml=True)

# Index documents
pipeline.index_documents(document_path: str) -> Dict

# Query system
pipeline.query(
    question: str,
    top_k: int = None,  # ML overrides if None
    temperature: float = None
) -> Dict

Feedback Collection

from src.feedback.feedback_collector import FeedbackCollector

collector = FeedbackCollector()

# Record feedback
collector.record_feedback(
    query: str,
    answer: str,
    retrieved_chunks: List[str],
    rating: int,  # 1-5
    feedback_type: str,  # positive/negative/rating
    comment: str = None
)

# Get statistics
stats = collector.get_feedback_stats()

ML Training

from src.ml.trainer import train_models_from_feedback

# Train all models
metrics = train_models_from_feedback(
    feedback_file="./outputs/feedback/feedback_log.jsonl",
    model_dir="./models/ml",
    min_samples=10
)

📂 Project Structure

RAG-Optimization-System/
├── README.md                    # This file
├── requirements.txt             # Python dependencies
├── setup.py                     # Package setup
├── .env.example                 # Environment template
├── .gitignore                   # Git ignore rules
│
├── src/                         # Source code
│   ├── data_ingestion/            # Document loading & processing
│   ├── embeddings/                # Sentence transformers
│   ├── vector_db/                 # ChromaDB operations
│   ├── retrieval/                 # Semantic search
│   ├── generation/                # LLM integration
│   ├── feedback/                  # Feedback system
│   ├── ml/                        # ML models & training
│   │   ├── feature_extractor.py   # 40+ feature extraction
│   │   ├── trainer.py             # Training pipeline
│   │   ├── predictor.py           # Inference interface
│   │   └── models/                # ML model implementations
│   └── utils/                     # Shared utilities
│
├── tests/                       # Test suite
│   ├── test_data_ingestion.py
│   ├── test_rag_system.py
│   ├── test_ml_system.py
│   ├── test_feedback_system.py
│   └── train_ml_models.py
│
├── ui/                          # Web interface
│   ├── streamlit_app.py           # Main UI application
│   ├── mock_backend.py            # Demo backend
│   └── styles.css                 # UI styling
│
├── scripts/                     # Utility scripts
│   └── demo.py                    # Complete demonstration
│
├── data/                        # Data directory
│   ├── raw/                       # Source documents (.txt)
│   ├── cache/                     # Embeddings cache
│   └── vector_store/              # ChromaDB storage
│
├── models/                      # Trained models
│   └── ml/                        # ML model storage
│
├── outputs/                     # System outputs
│   ├── logs/                      # Application logs
│   └── feedback/                  # User feedback data
│
└── docs/                        # Documentation
    ├── ARCHITECTURE.md            # System architecture
    ├── WORKFLOW.md                # Development workflow
    ├── FEEDBACK_SYSTEM.md         # Feedback documentation
    ├── UI_FEEDBACK_GUIDE.md       # UI integration guide
    └── ML_INTEGRATION_GUIDE.md    # ML implementation guide

🤝 Contributing

Development Setup

# Clone repository
git clone https://github.com/ahmedm0ssad/RAG-Optimization-System.git
cd RAG-Optimization-System

# Setup environment
python -m venv venv
source venv/bin/activate  # or venv\Scripts\activate on Windows
pip install -r requirements.txt

# Run tests
python tests/test_ml_system.py
python tests/test_feedback_system.py

Code Style

  • Follow PEP 8 guidelines
  • Use type hints
  • Add docstrings to functions
  • Write unit tests for new features
  • Update documentation

Adding New Features

  1. Create feature branch
  2. Implement changes with tests
  3. Update documentation
  4. Submit pull request

Running Tests

# Run all tests
python -m pytest tests/

# Run specific test
python tests/test_ml_system.py

# Run with coverage
pytest --cov=src tests/

🎓 Academic Context

Grading Rubric Alignment

Component Weight Implementation Location
Vector Database 20% ✅ ChromaDB with persistence src/vector_db/
Retrieval System 20% ✅ Dense semantic search src/retrieval/
LLM Generation 20% ✅ Gemini API integration src/generation/
Code Architecture 15% ✅ Modular, documented Entire src/
Execution Examples 15% ✅ Demo + UI + Tests scripts/, ui/
Documentation 10% ✅ Comprehensive docs docs/
Bonus: Web UI +5% ✅ Streamlit interface ui/streamlit_app.py
Bonus: Feedback +5% ✅ Full feedback system src/feedback/
Bonus: ML +5% ✅ ML optimization src/ml/

Total Possible: 115% ✨

Team Contributions

  • Member 1: Data Ingestion (src/data_ingestion/)
  • Member 2: Embeddings (src/embeddings/)
  • Member 3: Vector Database (src/vector_db/)
  • Member 4: Retrieval (src/retrieval/)
  • Member 5: Generation & Integration (src/generation/, ui/)
  • Bonus: ML Integration (src/ml/)

🐛 Troubleshooting

Common Issues

ChromaDB Installation

pip uninstall chromadb
pip install chromadb==0.4.18 --no-cache-dir

API Key Errors

# Verify API key is set
python -c "import os; from dotenv import load_dotenv; load_dotenv(); print(os.getenv('GOOGLE_API_KEY'))"

ML Models Not Loading

# Check models exist
ls models/ml/

# Retrain if needed
python tests/train_ml_models.py

Insufficient Training Data

# Check feedback count
python -c "import json; print(sum(1 for _ in open('./outputs/feedback/feedback_log.jsonl')))"

# Need 10+ samples minimum, 50+ recommended

Getting Help

  1. Check docs/ for detailed guides
  2. Review error logs in outputs/logs/
  3. Run demo script: python scripts/demo.py
  4. Check API quota: https://makersuite.google.com/

📊 Performance Metrics

System Performance

  • Indexing Speed: ~100 documents/minute
  • Query Response: 2-3 seconds average
  • Embedding Generation: ~500 texts/second (CPU)
  • Vector Search: <100ms for 10K documents
  • ML Overhead: +100ms per query

Resource Usage

  • Memory: ~500MB base + ~1GB per 10K documents
  • Disk: ~10MB per 1K documents
  • API Costs: Gemini free tier (15 requests/minute)

📜 License

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

MIT License

Copyright (c) 2025 ZC-UST Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction...

🙏 Acknowledgments

  • Course: CSAI 302 - Advanced Database
  • Institution: Zewail City University of Science and Technology
  • Technologies: Sentence Transformers, ChromaDB, Google Gemini, Streamlit
  • ML Frameworks: scikit-learn, XGBoost
  • Open Source: Built with open-source tools and libraries

📞 Contact

Course: CSAI 302 - Advanced Database
Institution: ZC-UST
Academic Year: 2024-2025
Last Updated: December 23, 2025


🚀 Next Steps

  1. ✅ Install dependencies
  2. ✅ Configure API key
  3. ✅ Launch UI and test queries
  4. ✅ Collect feedback (10+ samples)
  5. ✅ Train ML models
  6. ✅ Enjoy ML-powered optimization!

Happy Querying! 🎉

About

Advanced RAG system with an ML optimization layer - semantic search over ChromaDB, Google Gemini answer generation, XGBoost-based retrieval parameter optimization and query-difficulty/quality prediction, a star-rating feedback loop with continuous learning, and a Streamlit UI.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages