Skip to content
Vibhrav Jha.Let’s talk

Fine-Tuning vs. RAG

Comparing model adaptation with source-grounded retrieval

Held-out perplexity
48.85 → 2.49LoRA adaptation on held-out project material.

My contributionPrepared the corpus, fine-tuned the model, implemented retrieval and built the inspection interface.

FieldML & AI / Data Engineering

View repository

Academic experiment

Study assistant interface with retrieved source context

The work
behind the interface.

Reduced held-out language-model perplexity from 48.85 to 2.49 through LoRA adaptation, alongside a separate retrieval workflow over 23 lecture transcripts.

  • PyTorch
  • Hugging Face
  • LoRA
  • Haystack
  • Elasticsearch
  • Streamlit

The result

Recorded held-out perplexity fell from 48.85 to 2.49 after LoRA adaptation.

The problem

I built parallel fine-tuning and retrieval paths over 23 lecture transcripts to investigate what each contributes to study assistance.

A decision worth examining

Keep retrieved passages and scores visible in the Streamlit interface, and evaluate language modeling separately from retrieval.

Read the README.

View original README
READMEArchitecture, setup and usage

LLM Fine-Tuning vs. Retrieval-Augmented Generation

Can a small (1B-parameter) LLM answer domain-specific questions better after LoRA fine-tuning, or when it's given retrieved context? This project builds both approaches over a corpus of 23 lecture transcripts on data management (SQL, MongoDB, Elasticsearch, Docker, deployment) and compares them head to head.

Architecture

flowchart TB
    T[23 lecture transcripts] --> C1[Chunking]
    subgraph FT [Fine-tuning path]
        C1 --> Q[Qwen2.5-7B-Instruct<br/>synthesizes QA pairs]
        Q --> L[LoRA fine-tune<br/>Llama-3.2-1B-Instruct · 4-bit]
        L --> PPL[Perplexity eval]
    end
    subgraph RAG [RAG path]
        T --> FX[Fixed-size chunks] --> ES1[(ES index<br/>transcripts_fixed)]
        T --> SN[Sentence chunks] --> ES2[(ES index<br/>transcripts_sentence)]
        ES2 --> BM25[BM25 retriever · top-3]
        BM25 --> PB[Prompt builder] --> LLM[Llama-3.2-1B-Instruct<br/>HF Inference API]
        LLM --> UI[Streamlit chat app]
    end
    L --> CMP{Head-to-head<br/>comparison}
    LLM --> CMP

What's inside

  1. Baseline behavior: loads Llama-3.2-1B-Instruct in 4-bit (bitsandbytes), probes where it hallucinates, and steers it with chat templates and system roles.
  2. Synthetic training data: Qwen2.5-7B-Instruct works as a teacher model and turns cleaned transcript chunks into exam-style question/answer pairs.
  3. LoRA fine-tuning: a parameter-efficient fine-tune of the quantized 1B model on those pairs (PEFT + TRL), evaluated qualitatively and with held-out perplexity.
  4. RAG with Haystack + Elasticsearch: transcripts are indexed twice (fixed-size vs. sentence-level chunks), and the two strategies are compared on hand-labeled Precision@3.
  5. Streamlit app: a chat UI over the RAG pipeline that shows the retrieved chunks and their BM25 scores.
  6. Fine-tuning vs. RAG: the same prompts go through both systems.

Results

ExperimentResult
Held-out perplexity, base → LoRA fine-tuned48.85 → 2.49 (−94.9%)
Retrieval Precision@3, fixed-size vs. sentence-level chunks0.40 vs. 0.87
Factual accuracy on domain questionsRAG beat fine-tuning; the fine-tuned model still hallucinated specifics

The takeaway: fine-tuning taught the model the style and vocabulary of the domain (hence the big perplexity drop), but grounding answers in retrieved text is what made them correct. Sentence-level chunks keep each passage on a single topic, so BM25's keyword matching lands on relevant text far more often.

Tech stack

PyTorch · Hugging Face Transformers · PEFT (LoRA) · TRL · bitsandbytes (4-bit) · Datasets · Haystack · Elasticsearch (Elastic Cloud) · Hugging Face Inference API · Streamlit · NLTK

Running it

The notebook targets Google Colab:

  • Sections 1–2 (inference and fine-tuning) need a T4 GPU; the free tier works.
  • Section 3 (RAG) runs on CPU.
pip install -r requirements.txt
jupyter lab llm_finetuning_vs_rag.ipynb

You'll be asked for:

  • a Hugging Face token with access to the gated meta-llama/Llama-3.2-1B-Instruct model
  • an Elastic Cloud Cloud ID and API key (the free trial works)

Credentials are read interactively with getpass, and the Streamlit app reads them from environment variables (ES_CLOUD_ID, ES_API_KEY). Nothing is stored in the notebook.

Data: the transcript corpus isn't redistributed. To reproduce the pipeline, point it at any folder of .txt transcripts; the loader expects files named like 12 en-English-SQL window functions.txt.


Date finished: April 26, 2026

Distributed Weather Data System

  1. Aranyaka wildlife-image application interface

    Fullstack

    Aranyaka

  2. DermaNet hackathon prototype showing classification output and a generated explanation

    ML & AI

    DermaNet

  3. AI-generated concept illustration of source documents connected to retrieved evidence and an answer

    ML & AI

    Private-Data RAG

  4. Before It Breaks dashboard separating real weather from synthetic equipment telemetry and estimated costs

    ML & AI

    Before It Breaks

  5. Architecture diagram of simulated stock records flowing through Kafka to HDFS

    Distributed Systems

    Stock Streaming Engine

  6. Vyaya dashboard with clearly labeled synthetic demonstration data

    Fullstack

    Vyaya

  7. Study assistant interface with retrieved source context

    ML & AIViewing

    Fine-Tuning vs. RAG

  8. Architecture diagram of NOAA data, a Cassandra cluster and a query service

    Distributed Systems

    Distributed Weather Data System

  9. Distributed Property Lookup Service project image

    Distributed Systems

    Distributed Property Lookup Service

  10. Architecture diagram connecting MySQL, a gRPC service and HDFS storage

    Distributed Systems

    HDFS & SQL Pipeline

  11. Architecture diagram of Spark processing, SQL and analysis

    Distributed Systems

    Spark Analytics

  12. Multi-Source ELT Pipeline project image

    Data Engineering

    Multi-Source ELT Pipeline

  13. Wisconsin schools in their geographic context — explanatory architecture diagram

    Data Engineering

    GCP Geospatial Pipeline

  14. Chinook Music Store SQL Analytics project image

    Data Engineering

    Chinook Music Store SQL Analytics

  15. Yelp Geospatial Analytics project image

    Data Engineering

    Yelp Geospatial Analytics

  16. E-Commerce Semantic Search Engine project image

    Data Engineering

    E-Commerce Semantic Search Engine

  17. Retail Sales Forecasting project image

    ML & AI

    Retail Sales Forecasting

  18. Making tool dependencies inspectable — explanatory architecture diagram

    ML & AI

    AI Agent Tool Dependency Graph

  19. From public records to operational questions — explanatory architecture diagram

    Data Engineering

    Palantir Foundry and AIP Build Challenge

  20. StudyFlow study-session dashboard

    Fullstack

    StudyFlow

  21. Habit Command Center planning dashboard

    Fullstack

    Habit Command Center

  22. Interfaces across web and mobile — explanatory architecture diagram

    Fullstack

    React Web & Mobile

  23. Startup Scout project image

    Fullstack

    Startup Scout

  24. Inventory-Based Recipe Planner project image

    Fullstack

    Inventory-Based Recipe Planner

  25. Concurrent Word Count and Performance Benchmarking project image

    Data Engineering

    Concurrent Word Count

  26. Data structures matched to their assumptions — explanatory architecture diagram

    Computing Foundations

    Algorithm Design

  27. Campus Navigator project image

    Fullstack

    Campus Navigator

  28. i-Songly: Song Management Engine project image

    Fullstack

    i-Songly

  29. An experiment that can resume — explanatory architecture diagram

    ML & AI

    CIFAR-100 with LeNet

  30. Fashion-MNIST Neural Network Classifier project image

    ML & AI

    Fashion-MNIST Classifier

  31. PCA Eigenfaces and Image Reconstruction project image

    ML & AI

    PCA Eigenfaces

  32. Two routes to the same regression objective — explanatory architecture diagram

    ML & AI

    Lake Mendota Ice Regression

  33. Bayesian Language Detection project image

    ML & AI

    Bayesian Language Detection

  34. Explanatory diagram of the video-fingerprint benchmark: 25 sources, 75 transformed variants and 1,875 comparisons.

    ML & AI

    Video-Fingerprint Benchmark

All projects