Skip to content

OUANZOUGUIAbdelhak/TP_RAG_CENTRALE_CASABLANCA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RAG System - Retrieval Augmented Generation

A comprehensive RAG (Retrieval Augmented Generation) system built with LlamaIndex, featuring document indexing, semantic search, question-answering, and an interactive chatbot interface. The system supports multiple LLM providers with automatic fallback mechanisms.

🌟 Features

Core Capabilities

  • πŸ“š Document Indexing (Q1): Advanced document processing with metadata extraction

    • Support for PDF, DOCX, and Markdown files
    • Intelligent text chunking with configurable overlap
    • Advanced RAG pipeline with title and Q&A extraction
    • ChromaDB vector store for efficient storage
  • πŸ” Semantic Search (Q2): Powerful document retrieval

    • Vector similarity search with HuggingFace embeddings
    • Configurable top-k results
    • Similarity scoring and ranking
    • Metadata filtering support
  • πŸ’¬ Question Answering (Q3): LLM-powered Q&A system

    • Context-aware answer generation
    • Source citation and confidence scoring
    • Custom prompt templates optimized for RAG
    • Document-specific filtering
  • πŸ“Š Evaluation (Q4): System performance assessment

    • Relevance metrics
    • Accuracy evaluation
    • Quality checks
  • πŸ€– Interactive Chatbot (Q5): Conversational interface

    • Multi-session support
    • Conversation history management
    • Context-aware responses
    • Session persistence

Advanced Features

  • πŸ”„ LLM Fallback System: Automatic failover between Groq and Gemini
  • 🌐 RESTful API: FastAPI backend with Swagger documentation
  • πŸ’» Modern Frontend: React-based web interface
  • βš™οΈ Flexible Configuration: YAML-based configuration system
  • πŸ“ File Management: Upload, organize, and manage documents via API

πŸ—οΈ Architecture

The system follows a modular architecture with five core modules:

RAG System Architecture
β”œβ”€β”€ Q1: Document Indexer (indexer.py)
β”‚   β”œβ”€β”€ Document loading and parsing
β”‚   β”œβ”€β”€ Text chunking with overlap
β”‚   β”œβ”€β”€ Advanced metadata extraction (titles, Q&A)
β”‚   └── Vector embedding and storage
β”‚
β”œβ”€β”€ Q2: Document Retriever (retriever.py)
β”‚   β”œβ”€β”€ Vector similarity search
β”‚   β”œβ”€β”€ Top-k retrieval
β”‚   └── Similarity scoring
β”‚
β”œβ”€β”€ Q3: QA System (qa_system.py)
β”‚   β”œβ”€β”€ Context retrieval
β”‚   β”œβ”€β”€ LLM-based answer generation
β”‚   └── Source citation
β”‚
β”œβ”€β”€ Q4: Evaluator (evaluator.py)
β”‚   β”œβ”€β”€ Relevance metrics
β”‚   └── Accuracy assessment
β”‚
└── Q5: Chatbot (chatbot.py)
    β”œβ”€β”€ Conversation management
    β”œβ”€β”€ Session handling
    └── Context building

Technology Stack

Backend:

  • Python 3.9+
  • LlamaIndex (vector store and RAG framework)
  • ChromaDB (vector database)
  • FastAPI (REST API)
  • HuggingFace (embeddings)
  • Groq & Gemini (LLM providers)

Frontend:

  • React + Vite
  • Tailwind CSS
  • Axios (API client)

πŸ“¦ Installation

Prerequisites

  • Python 3.9 or higher
  • Node.js 16+ (for frontend)
  • pip package manager

Backend Setup

  1. Clone the repository

    git clone <repository-url>
    cd TP_RAG_CENTRALE_CASABLANCA
  2. Create a virtual environment (recommended)

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

    pip install -r requirements.txt
  4. Configure the system Edit Config.yaml and add your API keys:

    groq:
      api_key: "your_groq_api_key"
    
    gemini:
      api_key: "your_gemini_api_key"

Frontend Setup

  1. Navigate to frontend directory

    cd frontend
  2. Install dependencies

    npm install
  3. Start development server

    npm run dev

βš™οΈ Configuration

The system is configured via Config.yaml. Key settings:

Paths

paths:
  data_dir: "./data/files"          # Documents to index
  vectorstore_dir: "./data/vectorstore"  # Vector database
  chat_sessions_dir: "./data/chat_sessions"  # Chat history

Embedding Model

embedding:
  model_name: "BAAI/bge-large-en-v1.5"  # HuggingFace model

Document Processing

document_processing:
  chunk_size: 1024      # Characters per chunk
  chunk_overlap: 128    # Overlap between chunks

LLM Settings

groq:
  api_key: "your_key"
  model: "llama-3.3-70b-versatile"
  temperature: 0.7

gemini:
  api_key: "your_key"
  model: "gemini-2.0-flash"
  temperature: 0.7

πŸš€ Usage

Command Line Interface

The system provides a CLI for all operations:

1. Build Index (Q1)

python Cli.py build

This will:

  • Load documents from data/files/
  • Process and chunk documents
  • Extract metadata (if advanced RAG enabled)
  • Generate embeddings
  • Store in ChromaDB

2. Search Documents (Q2)

python Cli.py search "your query" -k 10

Returns top-k most relevant documents with similarity scores.

3. Ask Questions (Q3)

python Cli.py ask "What is machine learning?"

Generates an answer using retrieved context and LLM.

4. Evaluate System (Q4)

python Cli.py evaluate --quick

Runs quality checks and performance metrics.

5. Interactive Chat (Q5)

python Cli.py chat

Starts an interactive chatbot session.

REST API

Start the API server:

# From project root
python -m src.backend.api

# Or from src/backend
cd src/backend
python api.py

The API will be available at http://127.0.0.1:8000

Key Endpoints

File Management:

  • GET /api/files - List all files and folders
  • POST /api/upload - Upload a document
  • POST /api/create-folder - Create a folder
  • DELETE /api/delete?path=<file_path> - Delete file/folder

RAG Operations:

  • POST /api/build-index - Build or rebuild the index
  • POST /api/search - Search documents
    {
      "query": "your search query",
      "k": 10
    }

Chat:

  • POST /api/chat - Send a chat message
    {
      "message": "your question",
      "session_id": "optional_session_id"
    }
  • GET /api/sessions - List all chat sessions
  • POST /api/session/new - Create a new session
  • GET /api/session/{id} - Get session history

Interactive API Documentation:

Frontend Usage

  1. Start the backend API (see above)

  2. Start the frontend

    cd frontend
    npm run dev
  3. Access the web interface

  4. Use the interface

    • Upload documents via the file explorer
    • Build the index
    • Search documents
    • Chat with the AI assistant

πŸ”„ LLM Fallback Mechanism

The system includes an intelligent fallback system for LLM providers:

  1. Primary: Attempts to use Groq API
  2. Fallback: Automatically switches to Gemini if Groq fails
  3. Transparent: No code changes needed - works automatically

The fallback is configured in Config.yaml and works across all modules (Q1, Q3, Q5).

πŸ“ Project Structure

TP_RAG_CENTRALE_CASABLANCA/
β”œβ”€β”€ Config.yaml                 # Main configuration file
β”œβ”€β”€ Cli.py                      # Command-line interface
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ README.md                   # This file
β”‚
β”œβ”€β”€ data/                       # Data directory
β”‚   β”œβ”€β”€ files/                  # Documents to index
β”‚   β”œβ”€β”€ vectorstore/            # ChromaDB storage
β”‚   └── chat_sessions/         # Chat history
β”‚
β”œβ”€β”€ src/                        # Source code
β”‚   β”œβ”€β”€ backend/
β”‚   β”‚   β”œβ”€β”€ api.py             # FastAPI backend
β”‚   β”‚   └── README.md          # Backend documentation
β”‚   β”‚
β”‚   β”œβ”€β”€ indexer.py             # Q1: Document indexing
β”‚   β”œβ”€β”€ retriever.py           # Q2: Document retrieval
β”‚   β”œβ”€β”€ qa_system.py           # Q3: Question answering
β”‚   β”œβ”€β”€ evaluator.py           # Q4: System evaluation
β”‚   β”œβ”€β”€ chatbot.py             # Q5: Interactive chatbot
β”‚   β”œβ”€β”€ rag_system.py          # Complete RAG system
β”‚   └── llm_fallback.py        # LLM fallback mechanism
β”‚
└── frontend/                   # React frontend
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/        # React components
    β”‚   β”œβ”€β”€ services/         # API client
    β”‚   └── App.jsx           # Main app component
    β”œβ”€β”€ package.json
    └── vite.config.js

πŸ”§ Development

Running Tests

# Test CLI commands
python Cli.py build
python Cli.py search "test query"

# Test API endpoints
curl http://127.0.0.1:8000/api/health

Adding New Features

  1. New LLM Provider: Extend llm_fallback.py
  2. New Document Type: Update indexer.py document loader
  3. New API Endpoint: Add to src/backend/api.py
  4. Frontend Component: Add to frontend/src/components/

πŸ“ API Examples

Upload a Document

curl -X POST http://127.0.0.1:8000/api/upload \
  -F "file=@document.pdf"

Build Index

curl -X POST http://127.0.0.1:8000/api/build-index

Search Documents

curl -X POST http://127.0.0.1:8000/api/search \
  -H "Content-Type: application/json" \
  -d '{"query": "machine learning", "k": 5}'

Chat

curl -X POST http://127.0.0.1:8000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "What is AI?", "session_id": "session_1"}'

πŸ› Troubleshooting

Common Issues

Import Errors:

  • Ensure all dependencies are installed: pip install -r requirements.txt
  • Check Python version: python --version (should be 3.9+)

API Key Issues:

  • Verify API keys in Config.yaml
  • Check API key permissions and quotas
  • The system will automatically fallback to Gemini if Groq fails

Index Not Found:

  • Build the index first: python Cli.py build or POST /api/build-index
  • Ensure documents exist in data/files/

Port Already in Use:

  • Change port in src/backend/api.py or kill the process using port 8000

πŸ“„ License

This project is part of an academic assignment.

πŸ‘₯ Team

  • OUANZOUGUI Abdelhak
  • BELLMIR Omar
  • BOURHAIM Ayoub
  • DAHHASSI Chaymae
  • AIT BIHI Laila
  • EL ABDI Ibrahim

πŸ™ Acknowledgments

  • LlamaIndex for the RAG framework
  • ChromaDB for vector storage
  • HuggingFace for embedding models
  • Groq and Google for LLM APIs

πŸ“š Additional Resources


Happy RAG-ing! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 7