Instruction Tuning (SFT)

A base model predicts the next token. That's it. It doesn't follow instructions, answer questions, or refuse harmful requests. SFT is the bridge between a token predictor and a useful assistant. Every model you've ever talked to -- Claude, GPT, Llama Chat -- went through this step.

Build 1.5 hr Python (with numpy)

Prerequisites: Phase 10, Lesson 04 (Pre-Training a Mini GPT)

#Learning Objectives

  • Implement supervised fine-tuning (SFT) that converts a base language model into an instruction-following assistant
  • Format training data using chat templates with system, user, and assistant roles, and mask loss on non-assistant tokens
  • Explain why SFT is necessary: base models continue text rather than answer questions
  • Evaluate SFT quality by comparing base model vs fine-tuned model responses on a held-out instruction set
Full Access lesson

Keep going. The rest of this lesson is one step away.

Unlock all 503 lessons, video, code, quizzes and every future update. Pay in your own currency with Paystack.

The text and code of this lesson are based on AI Engineering from Scratch by Rohit Ghumare, MIT licensed.