The result
Recorded held-out perplexity fell from 48.85 to 2.49 after LoRA adaptation.
Comparing model adaptation with source-grounded retrieval
Academic experiment

Reduced held-out language-model perplexity from 48.85 to 2.49 through LoRA adaptation, alongside a separate retrieval workflow over 23 lecture transcripts.
From the repository
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.
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
Llama-3.2-1B-Instruct in 4-bit (bitsandbytes), probes where it hallucinates, and steers it with chat templates and system roles.Qwen2.5-7B-Instruct works as a teacher model and turns cleaned transcript chunks into exam-style question/answer pairs.| Experiment | Result |
|---|---|
| Held-out perplexity, base → LoRA fine-tuned | 48.85 → 2.49 (−94.9%) |
| Retrieval Precision@3, fixed-size vs. sentence-level chunks | 0.40 vs. 0.87 |
| Factual accuracy on domain questions | RAG 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.
PyTorch · Hugging Face Transformers · PEFT (LoRA) · TRL · bitsandbytes (4-bit) · Datasets · Haystack · Elasticsearch (Elastic Cloud) · Hugging Face Inference API · Streamlit · NLTK
The notebook targets Google Colab:
pip install -r requirements.txt
jupyter lab llm_finetuning_vs_rag.ipynb
You'll be asked for:
meta-llama/Llama-3.2-1B-Instruct modelCredentials 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
Continue exploring · 34 projects

Fullstack

ML & AI

ML & AI

ML & AI
Distributed Systems

Fullstack

ML & AIViewing
Distributed Systems

Distributed Systems
Distributed Systems
Distributed Systems

Data Engineering
Data Engineering

Data Engineering

Data Engineering

Data Engineering

ML & AI
ML & AI
Data Engineering

Fullstack

Fullstack
Fullstack

Fullstack

Fullstack
Data Engineering
Computing Foundations

Fullstack

Fullstack
ML & AI
ML & AI

ML & AI
ML & AI
ML & AI
ML & AI