§7.1–7.2Instruction Fine-Tuning: Data & Prompt Styles

Part III pp. 108–110 · ~5 min read

  • prompt style

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 (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 template. Follow the book’s “Ocassion” example through both famous templates:

{'{'} "instruction": "Identify the correct spelling of the following word.", "input": "Ocassion", "output": "The correct spelling is 'Occasion.'" {'}'} Alpaca template Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Identify the correct spelling of the following word. ### Input: Ocassion ### Response: The correct spelling is 'Occasion'. Phi-3 template <|user|> Identify the correct spelling of the following word: 'Ocassion' <|assistant|> The correct spelling is 'Occasion'. same content, far less scaffolding — ~17% faster to train, similar scores
1

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.

1 / 3

One dataset entry, two prompt styles (book p. 110). The book adopts Alpaca; Phi-3 shows how much templates can differ.

AspectAlpacaPhi-3
MarkupBoilerplate + ### Instruction / ### Input / ### Response<|user|> / <|assistant|> tags+
Sequence lengthLonger (verbose scaffolding)Shorter+
Resulting qualityReferenceSimilar score+
Used in this book✓ (classic, well-documented)+
The two prompt styles compared — click rows for detail.
</> format_input — flattening an entry into the Alpaca prompt
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 / 5
  1. 1.What makes instruction fine-tuning "supervised"?

  2. 2.Why must dataset entries be run through a prompt-style template at all?

  3. 3.Predict: format_input is called on an entry whose "input" field is empty. What does the resulting prompt contain?

  4. 4.Why does the Phi-3 template train ~17% faster than Alpaca on the same data?

  5. 5.During training, what does the full text for each example consist of?