Sub topic 1
Three levers for adapting a model
Prerequisites
This chapter assumes you have:
- Called a large language model through a chat-style API, with system, user, and assistant messages.
- Read Python at a basic level.
- A rough idea of what an embedding (a list of numbers that represents meaning) and a context window (the amount of text a model can read in one request) are.
The problem: a general model, a specific job
A pretrained model is trained on broad public text. Your task is narrower. It might be classifying your support tickets, answering questions about your internal policies, or writing in your house style. The model's first answer is often close but not right.
You have three main ways to close that gap. Each one changes a different part of the system.
The three levers
| Lever | What it changes | Model weights changed? | Typical input |
|---|---|---|---|
| Prompting | The instructions and examples in the request | No | Text you write |
| Retrieval-augmented generation (RAG) | The facts placed in the request at query time | No | A searchable store of documents |
| Fine-tuning | The model's internal weights | Yes | Example inputs paired with desired outputs |
Prompting and RAG both work by changing what the model reads. Fine-tuning changes the model itself.
Prompting
Prompting means writing better instructions, and sometimes adding a few worked examples. It is the cheapest lever. You edit text and test again. This snippet builds a few-shot prompt, which is a prompt that includes example inputs and outputs:
def build_messages(question, examples):
messages = [{
"role": "system",
"content": "Classify the support ticket as billing, bug, or other. Reply with one word.",
}]
for text, label in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": question})
return messages
examples = [
("I was charged twice this month.", "billing"),
("The export button crashes the app.", "bug"),
]
messages = build_messages("My invoice shows the wrong amount.", examples)The examples teach the model the label set and the answer format without any training.
RAG
RAG stands for retrieval-augmented generation. Before calling the model, your code searches a document store for passages related to the question. It then puts the best passages in the prompt and tells the model to answer from them. This gives the model facts it was never trained on, such as private documents or yesterday's news. Because the facts live in the store, you update them by editing documents, not by changing the model.
Fine-tuning
Fine-tuning continues training the model on your own examples. Each example pairs an input with the exact output you want. This is one line of a chat-format training file:
{"messages": [{"role": "system", "content": "You write release notes in our house style."}, {"role": "user", "content": "Fixed crash when exporting empty reports."}, {"role": "assistant", "content": "Fix: exporting an empty report no longer crashes the app."}]}After training, the model produces that style by default. It does so without long instructions or examples in every request. Fine-tuning is best for consistent behavior: tone, format, or a narrow skill. It is a poor way to add facts that change often.
Why order matters
The levers differ in cost, effort, and how easy they are to undo.
- Prompting costs minutes and is fully reversible.
- RAG needs a pipeline (document splitting, embeddings, search), which takes days to build.
- Fine-tuning needs curated training data, a training run, and a retrain whenever requirements change.
The usual practice is to start with the cheapest lever and move on only when evidence says it is not enough. The rest of this tutorial shows how to gather that evidence: diagnosing the failure, building an evaluation set, and comparing the levers.
Exercise
A model answers questions about your company's refund policy using outdated rules. The policy document changes monthly. Which lever changes the facts the model sees, and why is it a better fit than fine-tuning here?
Show answer
RAG. It places the current policy text in each request, so updating the document updates the answers. Fine-tuning would bake one version of the policy into the weights and require retraining after every change.