Expert: deep learning and AI · Building with generative AI, and MLOps · topic 54 of 62 · 30 min
Fine-tuning, and when not to
Explanation
Three ways to adapt an LLM
| Approach | What changes | Best for | Cost |
|---|---|---|---|
| Prompt engineering | Only the input text | Most tasks; quick experiments | Lowest |
| RAG | The input gets retrieved documents | Answers from changing or private knowledge | Medium (indexing and retrieval) |
| Fine-tuning | The model's weights | A consistent style, format or narrow skill; shorter prompts at scale | Highest effort (data, training, evaluation) |
A simple rule: if the model does not know something, give it the information (RAG). If it knows but behaves wrongly (wrong tone, wrong format, unreliable on a narrow task), show it how (prompting first, fine-tuning if prompting is not enough).
Supervised fine-tuning (SFT)
You prepare pairs of input and ideal output, often hundreds to thousands, in a file such as JSONL (one JSON object per line). Training nudges the weights so the model produces outputs like yours. Examples: replying to customers in your brand's tone, converting free-text addresses into a fixed format, classifying medical codes the way your team does.
{"prompt": "Classify: parcel not delivered after 7 days", "completion": "delivery"}
{"prompt": "Classify: charged twice for order 5521", "completion": "billing"}Parameter-efficient fine-tuning: LoRA
Updating all billions of weights needs huge GPUs. LoRA (low-rank adaptation) freezes the original weights and trains small extra matrices added to some layers, often well under 1% of the parameters. QLoRA also stores the frozen model in 4-bit form to save memory. This makes fine-tuning open models possible on a single good GPU, and the small adapter files are easy to store and swap.
Data quality beats data size
- Every example should be correct and in exactly the format you want; the model copies mistakes too.
- Cover the real variety: short and long inputs, Hindi and English, hard and edge cases.
- Remove duplicates and personal data you are not allowed to use.
- Keep a separate test set and compare the fine-tuned model with the base model plus a good prompt.
Distillation is a related idea: a large model's outputs are used to train a smaller, cheaper model for one task. Check the larger model's licence and terms before doing this.
Common mistakes
- Fine-tuning to add facts (like this year's policy). The model may still mix them up; use RAG.
- Fine-tuning before trying a good prompt with a few examples.
- No baseline comparison, so nobody knows whether fine-tuning helped.
- Training on messy or inconsistent examples and getting a messy model.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Fine-tuning, and when not to · 1/6
There are three ways to adapt a language model: prompting, RAG and fine-tuning.
Prefer a video by a teacher? Find free videos on “fine tuning vs RAG vs prompt engineering LoRA explained” on YouTube ↗
In real life: Standardising addresses for a courier company
A courier company needs free-text addresses ("near Hanuman mandir, 2nd cross, Jayanagar 4th block, Blr") converted into structured fields. A prompt with examples worked for 90% of cases but often broke the output format. The team fine-tuned a small open model with LoRA on 8,000 carefully corrected addresses. It now produces the exact format almost every time, runs cheaply on their own servers, and keeps customer addresses in-house.
Practice (0/3 done)
Write to_jsonl(pairs) that turns a list of (prompt, completion) tuples into JSONL text: one line per pair made with json.dumps({"prompt": p, "completion": c}, ensure_ascii=False), lines joined with "\n".
Write clean_pairs(pairs) that removes bad training examples from a list of (prompt, completion) tuples: drop pairs where the prompt or the completion is empty after .strip(), and drop any pair whose prompt (after .strip()) was already seen earlier. Keep the order of what remains, and return the pairs unchanged (not stripped).
For each need, choose prompting, RAG or fine-tuning and give one reason: (1) answer staff questions from a 300-page HR manual that changes every quarter, (2) make every reply follow the company's exact JSON format and tone at very high volume, (3) a one-off task of summarising 40 meeting notes.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Evaluating LLM appsBuilding with generative AI, and MLOps · same module
- AI safety and responsible deploymentBuilding with generative AI, and MLOps · same module
- Tuning profiles with tuned, and time with chronyLinux and Red Hat (RHCSA to RHCE) · Processes, services and logs
- How large language models workNLP, embeddings and large language models
- Train, validation and test setsEvaluating models
- Deep learning tools and hardwareNeural networks and deep learning
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Your chatbot must know this week's updated price list. Which approach fits best?
Answer: RAG over the latest price list
Changing facts belong in retrieved documents.
What does LoRA do?
Answer: Freezes the original weights and trains small extra matrices
It makes fine-tuning much cheaper.
What should you try before fine-tuning?
Answer: A good prompt with clear instructions and a few examples
Prompting is cheaper and often enough.
Your 2,000 training examples contain many inconsistent labels. What will fine-tuning likely produce?
Answer: A model that copies the inconsistency
The model learns whatever patterns are in the examples, including mistakes.
What is JSONL?
Answer: A text format with one JSON object per line
It is a common format for training examples.
In 5-fold cross-validation, how many times is the model trained?Related
What is the best way to improve a prompt?Related
Answer: Test on a set of real examples and change one thing at a time
A small test set turns prompting into engineering.
From Prompting well
Why can wrong time break logins in a company domain?Related
Answer: Kerberos rejects tickets when clocks differ too much
Kerberos allows only a small clock difference (often 5 minutes).
Besides answer quality, what should an LLM app track?Related
Answer: Latency, cost per request and safety failures
A correct answer that is too slow or too costly can still fail the product.
From Evaluating LLM apps
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Building with generative AI, and MLOps” before its exam: Evaluating LLM apps, AI safety and responsible deployment, MLOps: from notebook to production.
- Module 11 exam: Building with generative AI, and MLOps · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Ethics, privacy and careers. 62 topics left in the course before the final exam.