π Retrieval-Augmented Generation (RAG) From Scratch
This project implements a Retrieval-Augmented Generation (RAG) pipeline from scratch, without using LangChain or LlamaIndex abstractions. It demonstrates a clear, modular, and educational implementation of RAG using Dense Passage Retrieval (DPR), FAISS, and GPT-2.
The project is designed to help learners and practitioners understand the core mechanics of RAG systems used in modern LLM-based applications. π Key Features
π Dense Retrieval using Facebook DPR (context & question encoders)
π¦ Vector Indexing with FAISS
π§ Text Generation using GPT-2
π§© Fully modular Python architecture
π» Runs locally on CPU
π Clean separation of:
data loading
embedding
retrieval
generation
π§ RAG Architecture (Conceptual) User Question β βΌ Question Encoder (DPR) β βΌ FAISS Vector Search β βΌ Top-K Relevant Contexts β βΌ Prompt Construction β βΌ GPT-2 Generator β βΌ Final Answer π Project Structure
rag-from-scratch/ β βββ data/ β βββ companyPolicies.txt # Input knowledge base β βββ src/ β βββ data_loader.py # Download & chunk documents β βββ embeddings.py # Context encoder logic β βββ retrieval.py # Question encoder + FAISS search β βββ index.py # FAISS index creation β βββ generation.py # GPT-2 loading & answer generation β βββ config.py # Device configuration (CPU/GPU) β βββ init.py β βββ run_rag.py # Main execution script βββ requirements.txt # Python dependencies βββ README.md # Project documentation βββ .gitignore π οΈ Technologies Used
Python 3.9+
PyTorch
Hugging Face Transformers
FAISS
Dense Passage Retrieval (DPR)
GPT-2
βοΈ Installation & Setup 1οΈβ£ Clone the Repository git clone https://github.com//rag-from-scratch.git cd rag-from-scratch
2οΈβ£ Create Virtual Environment python -m venv rag-env rag-env\Scripts\activate # Windows
3οΈβ£ Install Dependencies pip install -r requirements.txt
Execute the main pipeline:
python run_rag.py
You should see:
Document loading
Embedding creation
FAISS index building
Context retrieval
Generated answer
Example output:
Question: What is the mobile phone policy? Answer: The mobile phone policy outlines acceptable use, security, compliance, and responsibilities related to company-issued mobile devices...