Lesson 3 of 11 · Inside an LLM
Instruction Tuning and Chat Models
How a base model becomes an assistant: supervised fine-tuning, chat templates and special tokens, RLHF and DPO, and safety training, with examples in Python.
- 7 min read
- Intermediate
- Reviewed
- Why a base model is not yet an assistant
- How supervised fine-tuning teaches the shape of helpful answers
- What chat templates and special tokens are
- How preference training (RLHF and DPO) and safety training work
From text predictor to assistant
A base model has learned a huge amount, but it only continues text. Ask it a question and it may continue with more questions, a forum argument, or the rest of a web page.
Prompt
What is the capital of France?
Example response
What is the capital of Germany? What is the capital of Italy? What is the capital of Spain? Geography Quiz: Test your knowledge of European capitals with these 20 questions...
A plausible continuation of web text, which is exactly what pretraining taught it.
The stages after pretraining are often called post-training. They use far less data and compute than pretraining, but they decide how the model behaves.
Stage 1: supervised fine-tuning (SFT)
Tens or hundreds of thousands of demonstrations show the model what a good assistant does: answer the question, follow the requested format, ask for clarification when needed, write code with explanations, decline harmful requests politely. This is also called instruction tuning.
Chat templates
A model reads one long stream of tokens, so a conversation must be turned into text with clear markers showing who said what.
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
]
def apply_chat_template(messages):
# Every model family has its own special markers; these are made up
text = ""
for message in messages:
text += f"<|{message['role']}|>\n{message['content']}<|end|>\n"
text += "<|assistant|>\n" # the model continues from here
return text
print(apply_chat_template(messages))During fine-tuning the model sees thousands of conversations in this format, so it learns that after the assistant marker comes a helpful reply, and that it should produce the end marker when the reply is done. Each model family uses its own markers; using the wrong template with an open model is a common cause of strange output.
Stage 2: learning from preferences
Demonstrations show one good answer. But often there are many acceptable answers and the hard part is knowing which is better. So the next stage uses comparisons.
# One item of preference data: a prompt, a better answer and a worse one
example = {
"prompt": "My code says IndexError: list index out of range. Help?",
"chosen": "That error means you asked for a position the list doesn't have. "
"For a list of 3 items, valid positions are 0, 1 and 2. "
"Can you share the line that fails?",
"rejected": "IndexError is an exception in Python.",
}
for key in ["prompt", "chosen", "rejected"]:
print(f"{key.upper()}: {example[key]}")
print()People (and increasingly AI models guided by written principles) compare pairs of answers to the same prompt and pick the better one. The model is then trained to make preferred answers more likely and rejected ones less likely.
Stage 3: safety and character
Models are also trained to decline harmful requests, to be honest about uncertainty and to handle sensitive topics carefully. Some companies write down the principles they want the model to follow and use them to guide training. Anthropic, for example, has published the principles it uses for Claude, an approach it calls Constitutional AI.
Newer models add further stages, such as reasoning training, covered in the next lesson.
Common mistakes
Write a simple chat template: it turns a list of messages into the single block of text the model actually reads.
messages = [
{"role": "system", "content": "Be brief."},
{"role": "user", "content": "Name a colour."},
]
# Turn the messages into one block of text in this format:
# SYSTEM: Be brief.
# USER: Name a colour.
# ASSISTANT:
What does preference training (such as RLHF or DPO) use as its training data?
Summary
- A base model continues text; post-training turns it into an assistant.
- Supervised fine-tuning trains on example conversations, using a chat template with special tokens for each role.
- Preference training (RLHF, DPO) learns from comparisons of better and worse answers.
- Safety and character training shapes honesty, care and refusals.
- Post-training shapes behaviour; most knowledge comes from pretraining.
Next: reasoning models, which learn to think before they answer.
Finished reading? Tick it off to track your progress.