Skip to content

Latest commit

Β 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“˜ 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

▢️ Running the Project

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...

About

Retrieval-Augmented Generation pipeline using DPR + FAISS + GPT-2

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages