Skip to main content

Pretrained models, fine-tuning and RAG

Why This Matters

Few teams train a large model from scratch. They take a pretrained model and adapt it, by fine-tuning it or by giving it their own documents to work with (RAG). Each route moves the risk to a different place, and you test a system whose biggest component was built by someone else, on data you have never seen.

What you will learn

  • What a pretrained model is, and what it brings along
  • How fine-tuning works, and when it works well
  • How retrieval-augmented generation (RAG) works, and what to test in it

Syllabus: AI-3.1.4.

Pretrained models

A pretrained model has already been trained, at great cost, on a large general dataset: millions of images, or a large part of the public internet. It has learned general skills (edges and shapes in images; grammar, facts and reasoning in text). You start from those skills instead of from zero.

It also brings along whatever it learned that you did not want: the biases and vulnerabilities of a pretrained model carry over into everything built on it. If the base model associates certain jobs with one gender, your fine-tuned CV screener may too. The syllabus is explicit: systems built on pretrained models need testing for reliability and fairness, whatever you did on top.

There are two ways to adapt a pretrained model, and they can be combined.

Fine-tuning

Fine-tuning continues training a pretrained neural network on a smaller dataset for your task. You can tune:

  • the whole network: all weights may change
  • specific layers, typically the ones near the output, while the early layers that hold the general skills are frozen
  • added layers: new layers on top of the pretrained network, trained for the new task

It needs far less data, time and computing power than training from scratch. How well it works depends on how similar the new task is to what the model was pretrained on. A model trained to recognise cat breeds adapts well to dog breeds: the same kind of images, the same kind of features. Adapting a speech model trained on standard accents to strong regional accents works less well: the sounds it needs were barely in the original data.

For testers: the model itself changed, so test it as a new model: performance on your task, and whether it lost general abilities it needed (it can “forget”). Check the fine-tuning data like any training data (module 6), and test for bias carried over from the base model.

Retrieval-augmented generation (RAG)

RAG gives a large language model access to your own information without changing the model:

                 ┌───────────────────────┐
your documents ─>│ made searchable       │
                 │ (split, indexed)      │
                 └──────────┬────────────┘
                            │ 2. retrieve the most relevant pieces
question ──1─────────────────┤
                            ▼
                 ┌───────────────────────┐
                 │ prompt = instructions │
                 │ + retrieved pieces    │──3──> LLM ──> answer
                 │ + question            │
                 └───────────────────────┘
  1. The user asks a question.
  2. The system searches your documents and retrieves the most relevant pieces.
  3. Those pieces are added to the prompt, and the LLM writes an answer based on them.

The model is unchanged. To update its knowledge you update the documents, not the model, which is why RAG is popular for company knowledge that changes often: policies, product manuals, support articles.

For testers: a RAG system can fail in each step, and the failures look alike from the outside (a wrong answer). Test them separately:

  • Retrieval: for a question, are the right documents found? Measure it directly, with a set of questions and the documents that should be found.
  • Grounding: does the answer stick to the retrieved documents, or does the model add facts from elsewhere (hallucination)?
  • The documents themselves: are they correct and up to date? Could someone plant a document with hidden instructions? That is indirect prompt injection through a poisoned RAG document (lesson 29).
  • Missing information: when the documents do not contain the answer, does the system say so?

Comparing the routes

Use the pretrained model as isFine-tuningRAG
What changesNothingThe model’s weightsThe prompt (documents are added)
Data neededNoneA labelled set for the taskA collection of documents
Updating knowledgeNot possibleFine-tune againUpdate the documents
Main new risksUnknown training data, inherited biasPoor tuning data, forgetting, inherited biasWrong retrieval, hallucination beyond the documents, poisoned documents

A system can use both: a model fine-tuned for a tone of voice and a domain, plus RAG for the facts.

Exercise

An insurance company wants a chatbot that answers customer questions about their policy conditions. The conditions change every quarter. Would you use fine-tuning, RAG or both? For your choice, list four things you would test.

Show suggested answer

RAG, because the conditions change every quarter: updating documents is much cheaper and faster than fine-tuning again, and the answer can point to the exact clause it is based on. Fine-tuning could be added for tone of voice or insurance terminology, but it is not needed for the facts.

Things to test, for example:

  1. Retrieval: for a set of 100 real customer questions, the right clause is among the retrieved pieces.
  2. Grounding: answers contain only what the retrieved conditions say; check for invented coverage (“yes, that is covered”) that is not in the text.
  3. Versions: after the quarterly update, answers follow the new conditions and not the old ones; customers with an older policy get the conditions of their own version.
  4. Missing answers and injection: a question the conditions do not cover gets “I can’t find that in your conditions” instead of a guess; a document with hidden instructions does not change the bot’s behaviour.

Exam-style questions

Quiz

1. What is a characteristic of retrieval-augmented generation (RAG)?

  • A. The LLM’s weights are updated with the company’s documents
  • B. Relevant documents are retrieved and added to the prompt; the model itself is not changed
  • C. It requires more training data than fine-tuning
  • D. It can only be used with image models
Show answer
B. RAG augments the prompt; fine-tuning (A) changes the model.
Quiz

2. In which case is fine-tuning most likely to work well?

  • A. Adapting a model that classifies cat breeds to classify dog breeds
  • B. Adapting an image model to translate text
  • C. Adapting a speech model trained on standard accents to strong regional accents
  • D. Fine-tuning never works better than training from scratch
Show answer
A. Fine-tuning works best when the new task is similar to the original one.
Quiz

3. Why should a system built on a pretrained model be tested for fairness, even if the team’s own data is unbiased?

  • A. Pretrained models are always biased against minorities
  • B. Biases and vulnerabilities in the pretrained model can carry over to the new system
  • C. Fairness testing is required only for pretrained models
  • D. Fine-tuning always introduces bias
Show answer
B. Whatever the pretrained model learned, including bias, carries over.