MNK/portfolio
GenAI & agents

RAG Custom Engine

~20% precision gain over dense baselines

A complete Retrieval-Augmented Generation pipeline built entirely from scratch — no LangChain, no vector database. Custom HNSW vector store, Okapi BM25 and Reciprocal Rank Fusion, a 3-stage Self-RAG gate (retrieval decision, relevance grading, hallucination check) and contextual compression, all in pure Python. ~20% precision gain over dense-vector baselines.

Problem

Understand retrieval-augmented generation end to end by building it without a framework or a vector database.

Approach

  1. Custom HNSW vector store and Okapi BM25 keyword index in pure Python
  2. Hybrid retrieval with Reciprocal Rank Fusion
  3. 3-stage Self-RAG gate: retrieval decision, relevance grading and hallucination check
  4. Contextual compression and a dual-layer memory system
  5. GPT-4o-mini for generation
RAG Custom Engine workflow: 5 stages from input to result
Workflow

Screens

RAG Custom Engine screenshot 1 of 8RAG Custom Engine screenshot 2 of 8RAG Custom Engine screenshot 3 of 8RAG Custom Engine screenshot 4 of 8RAG Custom Engine screenshot 5 of 8RAG Custom Engine screenshot 6 of 8RAG Custom Engine screenshot 7 of 8RAG Custom Engine screenshot 8 of 8

Results

  • ~20% precision gain over dense-vector baselines

Stack

  • Python
  • HNSW
  • BM25
  • Self-RAG
  • FastAPI
  • GPT-4o-mini

By Muzammil Nawaz Khan, AI Engineer.