# โš™๏ธ Enterprise RAG System โ€” Chat with PDF [![Live Demo](https://img.shields.io/badge/๐Ÿš€%20Live%20Demo-Streamlit%20Cloud-FF4B4B?style=for-the-badge&logo=streamlit)](https://ragsystem-chatpadf.streamlit.app/) [![Python](https://img.shields.io/badge/Python-3.10+-3776AB?style=for-the-badge&logo=python&logoColor=white)](https://python.org) [![LangChain](https://img.shields.io/badge/LangChain-Framework-1C3C3C?style=for-the-badge)](https://langchain.com) [![ChromaDB](https://img.shields.io/badge/ChromaDB-Vector%20Store-orange?style=for-the-badge)](https://trychroma.com) A production-grade **Retrieval-Augmented Generation (RAG)** pipeline that allows users to upload multiple PDFs and hold grounded, conversational, citation-backed interactions with their contents. Built with a highly modular architecture, a real-time stream validation engine, a multi-LLM resilient fallback chain, and an interactive Streamlit UI dashboard. > ๐Ÿ”— **Try it live:** [ragsystem-chatpadf.streamlit.app](https://ragsystem-chatpadf.streamlit.app/) ![Main App Dashboard](assets/image.png) --- ## ๐Ÿ“Œ Project Overview Most LLMs hallucinate or return generic answers when questioned about private or domain-specific documentation. This system solves that problem through a structured sequence: it compresses conversational context window queries into search terms, retrieves the most semantically relevant chunks from a persistent vector index, filters them mathematically based on geometric distance thresholds, and grounds the active model's response strictly within that context. Upload multiple PDFs โž” ask questions โž” get real-time streaming answers complete with page-level citations and mathematical validation metrics. --- ## ๐Ÿ—๏ธ Architecture & Operational Workflow ```text โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ USER (Streamlit UI) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Upload Multiple PDFs + Chat โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ INGESTION PIPELINE โ”‚ โ”‚ โ”‚ โ”‚ loader.py chunker.py embeding.py โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Extract โ”‚ โ”€โ”€โ”€โ–บ โ”‚ NLTK โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ all-MiniLM โ”‚ โ”‚ โ”‚ โ”‚ & Clean โ”‚ โ”‚ Sentence โ”‚ โ”‚ -L6-v2 โ”‚ โ”‚ โ”‚ โ”‚ PDF Text โ”‚ โ”‚ Splitter โ”‚ โ”‚ (HuggingFace)โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ–ผ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ vectore_store.py โ”‚ โ”‚ โ”‚ โ”‚ ChromaDB Index โ”‚ โ”‚ โ”‚ โ”‚ (Continuous/Disk)โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ CONVERSATIONAL QUERY CONDENSER & TRANSFORMER LAYER โ”‚ โ”‚ โ”‚ โ”‚ qa_pipeline.py โž” condense_user_query() โ”‚ โ”‚ Transforms user prompt + chat history window into optimal โ”‚ โ”‚ database search keyword vectors (Filters conversational noise)โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Optimized Keywords Target โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ RETRIEVAL & GENERATION PIPELINE โ”‚ โ”‚ โ”‚ โ”‚ retriever.py qa_pipeline.py โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Similarity Search โ”‚ โ”‚ Grounded Prompt Core โ”‚ โ”‚ โ”‚ โ”‚ + Score Metric Calc โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ โ”‚ + Token-Probed โ”‚ โ”‚ โ”‚ โ”‚ (threshold: 0.85) โ”‚ โ”‚ Fallback Stream Loopโ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ 1. Ollama (local) โ”‚ โ”‚ โ”‚ โ”‚ 2. HuggingFace Hub โ”‚ โ”‚ โ”‚ โ”‚ 3. OpenAI GPT-4o-mini โ”‚ โ”‚ โ”‚ โ”‚ 4. Google Gemini โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ Live Word Token Stream + Source Citations + Confidence Display ``` --- ## โœ… Features - **Multi-Document Appending Matrix** โ€” Upload and read several documents consecutively without wiping older context out of the vector database index - **Persistent State Tracker** โ€” Detects duplicate data files via the Streamlit UI to avoid redundant vector embedding transformations - **Conversational Memory Query Rewriting** โ€” Compiles recent context windows to extract semantic search keywords, filtering out conversational filler like "what is" or "tell me about" - **Sentence-Aware Token Chunking** โ€” Employs NLTK tokenizers to process documents along natural sentence structures (chunk size: 800 tokens, overlap: 150 tokens) - **Distance-Filtered Retrieval Guard** โ€” Rejects irrelevant background context chunks that fall outside the configured mathematical distance threshold - **Grounded System Prompt Architecture** โ€” Instructs models to restrict responses to the source material and return an explicit fallback text if the query is unanswerable from the context - **Token-Probed Multi-LLM Cascading Fallback Chain** โ€” Routes traffic down a secure pipeline (Ollama โž” Hugging Face โž” OpenAI โž” Google Gemini). The pipeline probes the stream's first token to capture errors (like 429 rate limits or quota issues) instantly, falling back automatically without crashing the user interface - **Real-Time Stream Rendering** โ€” Displays text generation token-by-token directly inside the chat workspace - **Source Citation Analytics** โ€” Expands detailed source dropdown cards detailing the document origin name, target page location, raw vector distance score, and precise text snippet matching - **Dynamic Retrieval Confidence Scoring** โ€” Translates database Euclidean distance calculations into a user-friendly percentage rating on the dashboard interface --- ## ๐ŸŽฌ Demo ๐Ÿ“บ **Video Walkthrough** โ€” Full pipeline demonstration: PDF upload โ†’ chunking โ†’ retrieval โ†’ streaming answer with citations. > โ–ถ๏ธ Watch the demo video to see the multi-LLM fallback chain, real-time streaming, and citation panel in action. ๐Ÿ”— **Live App:** [ragsystem-chatpadf.streamlit.app](https://ragsystem-chatpadf.streamlit.app/) --- ## ๐Ÿ–ผ๏ธ User Interface Captures ### 1. Ingestion Control Centre Manages document loading buffers and tracking indexes inside the active sidebar database monitor. ### 2. Live Conversational Streaming Engine Displays text generation token-by-token using the optimized multi-vendor routing stack. ### 3. Source Citations & Retrieval Confidence Scoring Metrics Translates raw distance metrics into analytical confidence scores and includes page-level citations. --- ## ๐Ÿ› ๏ธ Tech Stack | Layer | Technology | |----------------------------|-----------------------------------------------------------------------------------------------| | **Frontend Framework** | Streamlit | | **PDF Extraction Engine** | pypdf | | **Segmentation Layer** | LangChain Text Splitters + NLTK Tokenizers | | **Embedding Matrix** | HuggingFace `all-MiniLM-L6-v2` via sentence-transformers | | **Vector Index Database** | ChromaDB (Persistent Local Storage Architecture) | | **Local LLM Client** | Ollama Engine Instance (llama3 / llama3.2) | | **Cloud LLM Providers** | OpenAI API (`gpt-4o-mini`), Google GenAI SDK (`gemini-2.5-flash`), Hugging Face Inference API | | **Environment Control** | python-dotenv | | **Language Profile** | Python 3.10+ / 3.12 | --- ## ๐Ÿ“ฆ Installation 1. **Clone the repository** ```bash git clone https://github.com/ajme-abes/RAG-Multimodal-SafeAI.git cd RAG-Multimodal-SafeAI/rag-system **2. Create and activate a virtual environment** ```bash python -m venv venv # Windows venv\Scripts\activate # macOS / Linux source venv/bin/activate ``` **3. Install dependencies** ```bash pip install -r requirements.txt ``` **4. Set up environment variables** Create a `.env` file in the `rag-system/` directory: ```env OPENAI_API_KEY=your_openai_key_here GOOGLE_API_KEY=your_google_key_here HF_API_KEY=your_huggingface_key_here ``` > Note: At least one cloud vendor API credential key or a running local instance of Ollama is required for inference processing. **5. Set up local inference models (Optional)** Download from [ollama.com](https://ollama.com) then pull the model: ```bash ollama pull llama3 ``` --- ## ๐Ÿš€ Usage Guide **Launching the Dashboard Web UI Application** ```bash cd rag-system/app streamlit run app.py ``` Then: 1. Open `http://localhost:8501` in your browser 2. Upload a PDF using the sidebar 3. Wait for the ingestion pipeline to complete 4. Ask questions in the chat input **Run the CLI pipeline** ```bash cd rag-system/app python main.py ``` **Running the Terminal Command Line Interface Core** ```bash python rag-system/test/inspect_chunk.py cd rag-system/app python main.py ``` **Running Pipeline Integrity Diagnostics** ```bash # Inspect chunk quality python rag-system/test/inspect_chunk.py # Test semantic search python rag-system/test/test_search.py ``` --- ## ๐Ÿ“ Project Structure ``` rag-system/ โ”œโ”€โ”€ app/ โ”‚ โ”œโ”€โ”€ app.py # Streamlit Application Workspace Entrypoint โ”‚ โ”œโ”€โ”€ main.py # Local command line CLI system orchestration loop โ”‚ โ”œโ”€โ”€ loader.py # PDF text extraction and character normalizing โ”‚ โ”œโ”€โ”€ chunker.py # NLTK sentence boundary segmentation engine โ”‚ โ”œโ”€โ”€ embeding.py # HuggingFace dense vector conversion manager โ”‚ โ”œโ”€โ”€ vectore_store.py # ChromaDB transaction and persistence handlers โ”‚ โ”œโ”€โ”€ retriever.py # Geometric similarity matching and score filtering โ”‚ โ””โ”€โ”€ qa_pipeline.py # Prompt grounding construction + cascading stream fallback routing โ”œโ”€โ”€ assets/ โ”‚ โ”œโ”€โ”€ image.png # Dashboard layout overview capture โ”‚ โ”œโ”€โ”€ image_pdfup.png # Ingestion tracking system window screenshot โ”‚ โ”œโ”€โ”€ image_scor.png # Evaluation score components screenshot โ”‚ โ””โ”€โ”€ image_chat.png # Active conversational response capture โ”œโ”€โ”€ data/ โ”‚ โ””โ”€โ”€ ArtificiaL_.pdf # Sample testing asset document โ”œโ”€โ”€ test/ โ”‚ โ”œโ”€โ”€ inspect_chunk.py # Segmentation output formatting auditor โ”‚ โ”œโ”€โ”€ test_search.py # Database index semantic matching integrity tool โ”‚ โ””โ”€โ”€ testdb_load.py # Persistent disk read/write operational verification test โ”œโ”€โ”€ notebooks/ โ”‚ โ””โ”€โ”€ chunk_inspect.ipynb# Visual parsing inspection workspace โ”œโ”€โ”€ requirements.txt # Production dependency pinning profile โ””โ”€โ”€ README.md # Project documentation profile ``` --- ## ๐Ÿ–ผ๏ธ Interface Walkthrough ### 1. Main Dashboard Overview The main entry point showing the full layout of the system interface. ![Main App Dashboard](assets/image.png) --- ### 2. Document Ingestion Panel Tracks uploaded files and ensures duplicates are not processed twice. ![Document Ingestion Panel](assets/image_pdfup.png) --- ### 3. Active Conversational Response Streaming engine routes text tokenโ€‘byโ€‘token using local or cloud fallback models. ![Conversational Chat Interface](assets/image_chat.png) --- ### 4. Metrics and Citation Expanders Custom calculation block translates raw Euclidean vector distances into a clean confidence percentage, complete with document page numbers. ![Metrics and Citation Expanders](assets/image_scor.png) ## ๐Ÿ”ฎ Future Improvements - [ ] **Cross-Encoder Reranking** โ€” Use a `cross-encoder/ms-marco-MiniLM` model to rerank retrieved chunks before LLM generation - [ ] **OCR Fallback** โ€” Handle scanned PDFs using `pytesseract` or `pymupdf` when `pypdf` returns empty text - [ ] **FastAPI Backend** โ€” Expose the pipeline as a REST API for integration with other services - [ ] **Docker Deployment** โ€” Containerize the full stack with a multi-stage Dockerfile for cloud scaling - [ ] **RAG Evaluation** โ€” Integrate `ragas` toolkit to benchmark faithfulness, semantic relevancy, and context recall accuracy