Skip to content

Expert: deep learning and AI · Building with generative AI, and MLOps · topic 54 of 62 · 30 min

Fine-tuning, and when not to

Explanation

Fine-tuning continues training a pre-trained model on your own examples so it learns a style, format or narrow task. It does not reliably teach new facts; RAG is usually better for knowledge. Try good prompting first, then RAG, and fine-tune when you have many high-quality examples and a stable task. LoRA makes fine-tuning cheaper by training small extra matrices.

Three ways to adapt an LLM

ApproachWhat changesBest forCost
Prompt engineeringOnly the input textMost tasks; quick experimentsLowest
RAGThe input gets retrieved documentsAnswers from changing or private knowledgeMedium (indexing and retrieval)
Fine-tuningThe model's weightsA consistent style, format or narrow skill; shorter prompts at scaleHighest 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.

json
{"prompt": "Classify: parcel not delivered after 7 days", "completion": "delivery"}
{"prompt": "Classify: charged twice for order 5521", "completion": "billing"}
Two lines of a JSONL training file. Each provider or library defines its own exact field names; check its docs.

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.

Tip: Fine-tuning and RAG work well together: a fine-tuned model can learn your answer format and tone, while RAG supplies the current facts at question time.

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

Prompt · RAG · Fine-tune

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)

Task 1 · Code · Python

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

Task 2 · Code · Python

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

Task 3 · Practice

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

Question 1

1.Your chatbot must know this week's updated price list. Which approach fits best?

Question 2

2.What does LoRA do?

Question 3

3.What should you try before fine-tuning?

Question 4

4.Your 2,000 training examples contain many inconsistent labels. What will fine-tuning likely produce?

Question 5

5.What is JSONL?

Read more (free)

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

From Tuning profiles with tuned, and time with chrony

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

  1. 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.
  2. Module 11 exam: Building with generative AI, and MLOps · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Ethics, privacy and careers. 62 topics left in the course before the final exam.

← Previous: Retrieval-augmented generation (RAG)