The final chapter takes the roadmap’s last branch: turning the pretrained GPT into a personal assistant that follows natural-language instructions. Where chapter 6’s classifier answers with one of two labels, this model must generate an appropriate free-form response to whatever it’s asked — summarize, rewrite, answer, convert. The training recipe is supervised instruction fine-tuning instruction fine-tuning Fine-tuning on (instruction, response) pairs so the model learns to follow natural-language instructions (see ch7). defined in ch. 1 — open in glossary (7.1): train on a dataset where the input-output pairs are explicitly provided.
7.2 The dataset — and how to flatten it
The raw material is instruction-data.json (from the book’s repository):
entries with three fields — an instruction, an optional input, and the
desired output. A GPT consumes flat token streams, not JSON, so each entry
must pass through a prompt style prompt style The template that flattens (instruction, input, response) into one training text — e.g. Alpaca's ### sections or Phi-3's <|user|>/<|assistant|> tags.
defined in ch. 7 — open in glossary
template.
Follow the book’s “Ocassion” example through both famous templates:
A raw dataset entry
Three fields: the instruction (the task), an optional input (the material to work on — here a misspelled word), and the output (the desired response). Some entries have no input at all.
One dataset entry, two prompt styles (book p. 110). The book adopts Alpaca; Phi-3 shows how much templates can differ.
| Aspect⇅ | Alpaca⇅ | Phi-3⇅ | |
|---|---|---|---|
| Markup | Boilerplate + ### Instruction / ### Input / ### Response | <|user|> / <|assistant|> tags | + |
| Sequence length | Longer (verbose scaffolding) | Shorter | + |
| Resulting quality | Reference | Similar score | + |
| Used in this book | ✓ (classic, well-documented) | — | + |
def format_input(entry):
instruction_text = (
f"Below is an instruction that describes a task. "
f"Write a response that appropriately completes the request."
f"\n\n### Instruction:\n{entry['instruction']}" #A
)
input_text = f"\n\n### Input:\n{entry['input']}" if entry["input"] else "" #B
return instruction_text + input_text #C - #A Fixed boilerplate + the entry's instruction under its ### header.
- #B The ### Input: section exists only when the entry HAS input text — entries like "Name three capital cities" skip it entirely.
- #C Note what's NOT here: the response. format_input builds the PROMPT half; training (next unit) appends "\n\n### Response:\n" + output to form the full text, and inference stops here and lets the model generate the rest.
Key idea — the template IS the interface
Instruction fine-tuning doesn’t change what a GPT fundamentally does — it still predicts next tokens. The prompt style creates a convention: train on thousands of texts where### Response: is always followed by a good answer,
and the model learns that continuing past that marker means “now respond”.
Batching those variable-length texts efficiently — with a custom collate
function and loss masking — is the next unit’s craft.📝 Check yourself: instruction data & prompt styles
0 / 51.What makes instruction fine-tuning "supervised"?
2.Why must dataset entries be run through a prompt-style template at all?
3.Predict: format_input is called on an entry whose "input" field is empty. What does the resulting prompt contain?
4.Why does the Phi-3 template train ~17% faster than Alpaca on the same data?
5.During training, what does the full text for each example consist of?