Chapter 18: From Language Model to ChatGPT (The Three Training Stages)

By ·

Chapter 18: From Language Model to ChatGPT (The Three Training Stages)

The Critical Missing Piece

You now understand how to train a transformer (Chapter 11), but there's something crucial we haven't told you yet:

You DON'T train a ChatGPT from scratch for every task!

Imagine this scenario:

You want to build a medical diagnosis chatbot. A beginner might think:

"Okay, I'll train a GPT from scratch using medical textbooks!"

This would:

  • ✗ Cost millions of dollars
  • ✗ Take months of compute time
  • ✗ Require billions of medical documents
  • ✗ Still produce a worse model than fine-tuning

The REAL way modern AI works:

  1. Pre-training: Train once on ALL internet text (GPT-3 style) - costs $10 million
  2. Fine-tuning: Adapt to specific tasks (medical chatbot) - costs $1,000
  3. Instruction tuning/RLHF: Make it helpful and safe - costs $50,000

This three-stage process is a useful mental model for how systems like ChatGPT are built, and it captures a big part of the story for many modern assistants. But as we'll note later, real production pipelines can add extra alignment and post-training techniques on top of these core stages.

Let's understand each stage so you know what's actually happening in the real world.


Part 1: Pre-Training (Building the Foundation)

The Encyclopedia Analogy

Imagine creating an encyclopedia from scratch:

Stage 1: Read EVERYTHING (Pre-training)

Read: All books in the library
Read: All newspapers from the last 20 years
Read: All websites on the internet
Read: All scientific papers
Read: All Reddit discussions

Time: 5 years
Cost: Hire 100 researchers, $10 million
Result: One person who has read EVERYTHING!

This is pre-training: Train a massive model on ALL available text to learn:

  • General language patterns
  • World knowledge
  • How sentences are structured
  • Relationships between concepts
  • Common sense reasoning
  • Math, science, history, culture, everything!

What Pre-Training Looks Like

Training objective: Predict the next word!

Input: "The capital of France is"
Target: "Paris"

Input: "2 + 2 equals"
Target: "4"

Input: "function bubbleSort(array)"
Target: "{"

... billions of examples from all domains

Pre-training a GPT-3 scale model:

  • Dataset: 45TB of text (books, websites, papers)
  • Compute: 3,640 petaflop-days
  • Time: Several months on thousands of GPUs
  • Cost: ~$4-12 million
  • Result: Base model that can predict next words

The base model after pre-training:

  • ✅ Knows language patterns
  • ✅ Has general world knowledge
  • ✅ Can complete sentences
  • ⚠ But it's not a helpful assistant yet!
  • ⚠ Doesn't follow instructions well
  • ⚠ Might generate toxic/harmful content

Example of base GPT-3:

You: "Write me a poem about dogs"
Base model: "Write me a poem about dogs and cats and then write another poem about horses and..."
(Just continues the pattern, doesn't follow the instruction!)

Who does pre-training?

  • OpenAI (GPT-3, GPT-4)
  • Meta (LLaMA)
  • Anthropic (Claude base models)
  • Google (PaLM, Gemini)
  • Big companies with $$$

You probably won't do this! Pre-training is expensive. Instead, you'll use their pre-trained models!


Part 2: Fine-Tuning (Specialization)

The Medical School Analogy

After reading everything (pre-training), now specialize:

Stage 2: Medical School (Fine-tuning)

You've read everything, but now focus on medicine
Read: Medical textbooks (smaller, focused dataset)
Practice: Medical diagnosis examples
Time: 1 year
Cost: Much cheaper than reading everything!
Result: Expert in medicine (but still knows general knowledge!)

This is fine-tuning: Take the pre-trained model and continue training on a specific domain or task.

What Fine-Tuning Looks Like

You start with: Pre-trained GPT-3 (already knows language)

You continue training on: Domain-specific data

Example: Creating a Python coding assistant

Dataset: 10,000 Python code examples
Input: "def reverse_string"
Target: "(s):\n    return s[::-1]"

Input: "Create a function to sort"
Target: "def sort_list(arr):\n    return sorted(arr)"

Train for: Few days on 1-4 GPUs Cost: $100-$1,000 Result: Model that's MUCH better at Python code!

Why Fine-Tuning Works So Well

The learning transfer:

  • Pre-training taught: General language, patterns, reasoning
  • Fine-tuning adds: Specialized knowledge on top!

It's like:

  • Pre-training = Learning to drive
  • Fine-tuning = Learning to drive a truck (you already know the basics!)

Much faster than learning from scratch!

Real-world examples:

  • Fine-tune GPT on medical papers → Medical AI assistant
  • Fine-tune GPT on legal documents → Legal research assistant
  • Fine-tune GPT on your company's documentation → Company-specific chatbot
  • Fine-tune GPT on code → GitHub Copilot

Parameters updated:

  • You CAN freeze early layers (keep general knowledge)
  • Or update all layers (more adaptation)
  • Typical: Update all, but with small learning rate

Part 3: Instruction Tuning & RLHF (Making It Helpful)

The Critical Gap: Why ChatGPT ≠ GPT-3

Here's what confuses everyone:

GPT-3 (base model):

You: "Explain quantum physics"
GPT-3: "Explain quantum physics to me again and again and again..." 
(Continues the pattern, doesn't actually explain!)

ChatGPT (after instruction tuning + RLHF):

You: "Explain quantum physics"
ChatGPT: "Quantum physics is the study of matter and energy at the atomic scale..."
(Actually follows the instruction and provides helpful answer!)

What happened between GPT-3 and ChatGPT? Two more training stages!

Stage 3a: Instruction Tuning (SFT - Supervised Fine-Tuning)

The problem: Base models just predict next words. They don't understand "instructions."

The solution: Train on instruction-following examples!

Dataset format:

Instruction: "Translate to French: I love pizza"
Response: "J'aime la pizza"

Instruction: "Summarize this article: [long text]"
Response: "[concise summary]"

Instruction: "Write a poem about dogs"
Response: "[actual poem, not just continuing the prompt]"

... 10,000-100,000 high-quality instruction-response pairs

Training: Fine-tune the base model on these examples

Result: Model that follows instructions!

The teacher-student analogy:

  • Pre-training: Read every book (general knowledge)
  • Fine-tuning: Specialize in a subject (domain expertise)
  • Instruction tuning: Learn to be a TEACHER who answers questions clearly
    • Not just knowing information
    • But presenting it helpfully when asked!

Stage 3b: RLHF (Reinforcement Learning from Human Feedback)

The problem: Even after instruction tuning, the model might:

  • Give correct but unhelpful answers
  • Be rude or inappropriate
  • Hallucinate (make up facts)
  • Not match human preferences

The solution: Humans rate responses, model learns to generate better ones!

The process:

Step 1: Collect human preferences

Prompt: "Explain photosynthesis"

Response A: "Photosynthesis is when plants make food from sunlight using chlorophyll..."
Response B: "Photosynthesis? It's like, um, plants doing stuff with light, I think..."

Human raters: "A is much better!" ✓

Do this for thousands of prompts → Dataset of ranked responses

Step 2: Train a reward model

  • Learns to predict human preferences
  • Input: (prompt, response)
  • Output: Score (how good is this response?)

Step 3: Use RL to optimize for high rewards

  • Model generates responses
  • Reward model scores them
  • Model learns: "Generate responses that score high!"
  • Uses PPO (Proximal Policy Optimization) algorithm

Result: A ChatGPT-style assistant: a model that:

  • ✅ Follows instructions
  • ✅ Gives helpful, harmless, honest responses
  • ✅ Matches human preferences
  • ✅ Declines inappropriate requests

The Three Stages Visualized

STAGE 1: PRE-TRAINING
Huge text dataset -> predict the next token -> base language model
Result: strong language ability, broad knowledge, text continuation

STAGE 2: INSTRUCTION TUNING
Prompt/response examples -> supervised fine-tuning -> instruction follower
Result: answers questions and follows user requests more directly

STAGE 3: PREFERENCE ALIGNMENT / RLHF
Human preference rankings -> reward model -> optimize assistant behavior
Result: responses become more helpful, safer, and better matched to human preferences

Think of the stages like building a talented student:

  • Pre-training teaches broad language and world patterns.
  • Instruction tuning teaches the student to answer the assignment instead of merely continuing text.
  • Preference alignment teaches which good-looking answers humans actually prefer.

What This Means for You

As a learner/job seeker, you will MOST LIKELY:

  1. Use a pre-trained model (GPT-3, LLaMA, etc.)

    • Download from Hugging Face or OpenAI API
    • Don't pay $10M to pre-train!
  2. Fine-tune it for your task

    • Collect 1K-100K examples for your domain
    • Train for hours/days on 1-4 GPUs
    • Cost: $100-$10,000
  3. Maybe do instruction tuning/RLHF

    • If building a production assistant
    • Collect human feedback
    • Cost: $10K-$100K

The reality: 99% of ML engineers do fine-tuning, not pre-training!

Common Misconceptions Corrected

❌ Myth: "I need to train a GPT from scratch for my chatbot" ✅ Reality: Fine-tune a pre-trained model (1000× cheaper and faster!)

❌ Myth: "ChatGPT is just GPT-3" ✅ Reality: ChatGPT-style assistants start from a base GPT-style model, then add instruction tuning and alignment methods such as RLHF

❌ Myth: "Fine-tuning changes everything about the model" ✅ Reality: Fine-tuning adapts the last layers most; early layers (basic language) barely change

❌ Myth: "I need billions of examples to fine-tune" ✅ Reality: 1K-100K examples often enough (you're adapting, not learning from scratch!)

The Cost Comparison

PRE-TRAINING (from scratch):
- Data: 45TB (entire internet)
- Compute: Thousands of GPUs for months
- Cost: $4-12 million
- Who does this: OpenAI, Meta, Google

FINE-TUNING (your use case):
- Data: 1GB (your domain)
- Compute: 1-4 GPUs for days
- Cost: $100-$10,000  
- Who does this: You! Companies, startups, researchers

INFERENCE (using the model):
- Compute: 1 GPU
- Cost: $0.01 per 1000 tokens (OpenAI pricing)
- Who does this: End users, applications

See the massive difference? Fine-tuning is accessible! Pre-training is not (for most people).


Part 4: Practical Implications

What You'll Actually Do in a Job

Scenario 1: Startup wants a customer service chatbot

Step 1: Choose pre-trained model (LLaMA 2 - free!)
Step 2: Collect 5,000 customer service conversations
Step 3: Fine-tune for 2 days on 1 GPU ($100)
Step 4: Deploy!

NOT: Train from scratch ($10M ✗)

Scenario 2: Company wants code completion for their codebase

Step 1: Use GPT-3.5 API or CodeLlama
Step 2: Fine-tune on company's code (10K examples)
Step 3: Fine-tune for 1 day
Step 4: Integrate into IDE

NOT: Build a new language model from scratch

Scenario 3: Research project on medical text

Step 1: Download BioBERT (BERT pre-trained on medical text)
Step 2: Fine-tune on your specific medical task (classification/extraction)
Step 3: Train for hours
Step 4: Publish results

NOT: Create new medical AI from scratch

The Building Analogy

Pre-training = Building a house's foundation and basic structure

  • Expensive, time-consuming, need expert contractors
  • Do it once, very well
  • Required before anything else

Fine-tuning = Interior decorating and customization

  • Much cheaper and faster
  • Customize for your needs (office vs home vs restaurant)
  • Builds on the solid foundation

Instruction tuning = Training staff to be helpful

  • Teach how to interact with customers properly
  • Moderate cost

RLHF = Getting customer feedback and improving

  • Continuous refinement based on real-world usage

You don't rebuild the house for each new tenant - you just redecorate!


Part 5: The Timeline of Modern LLMs

Let's trace how ChatGPT was actually made:

2018-2020: GPT-1, GPT-2, GPT-3 (Pre-training Era)

OpenAI pre-trains on internet:

  • GPT-1 (2018): 117M parameters
  • GPT-2 (2019): 1.5B parameters
  • GPT-3 (2020): 175B parameters

Result: Base models that complete text well, but don't follow instructions

2022: InstructGPT (Adding Instruction Tuning)

OpenAI fine-tunes GPT-3 on instructions:

  • Dataset: 13K instruction-response pairs (human-written)
  • Result: InstructGPT - follows instructions much better!

Then adds RLHF:

  • Humans rank responses
  • Train reward model
  • Use RL to optimize

Result: InstructGPT - GPT-3's smarter, more helpful sibling

November 2022: ChatGPT (Public Release)

OpenAI releases InstructGPT as ChatGPT:

  • Based on GPT-3.5 (improved version of GPT-3)
    • Instruction tuning
    • RLHF
  • = Viral sensation!

The world discovers: "Wow, AI can actually help me with tasks!"

2023 Onward: GPT-4, Claude, Gemini, and the Modern Era

A useful simplified picture is that many modern systems follow something LIKE this three-stage pattern:

  • Pre-train massive base models
  • Instruction tune
  • RLHF to align with human values

Reality check: Modern assistants can add extra techniques on top of this, like constitutional AI, DPO-style preference optimization, or more elaborate alignment pipelines. So think of the three stages as the core mental model, not the only exact recipe used in the wild.

Result: Today's helpful AI assistants!


Part 6: Key Takeaways

What You Need to Remember

1. Almost NO ONE trains transformers from scratch (except big labs)

  • Pre-training costs millions
  • You'll use pre-trained models (GPT, LLaMA, etc.)

2. YOU will do fine-tuning (this is where jobs are!)

  • Adapt existing models to specific tasks
  • Much cheaper ($100-$10K vs $10M)
  • This is what companies need!

3. ChatGPT = Base Model + Two More Training Stages

  • Not magic, just clever training strategy
  • Instruction tuning: Follow instructions
  • RLHF: Be helpful and safe

4. The three stages serve different purposes:

  • Pre-training: Learn language (foundation)
  • Fine-tuning: Learn your task (specialization)
  • RLHF: Learn to be helpful (alignment)

5. Your career path:

  • Learn transformer architecture ✓ (you just did!)
  • Learn to fine-tune (load pre-trained model + train on your data)
  • Maybe learn RLHF (advanced, but growing field!)
  • You probably won't pre-train (unless you join OpenAI/Meta/Google!)

The Empowerment Message

The GREAT NEWS: You don't need millions of dollars to work with transformers!

You CAN:

  • Download LLaMA 2 (free, open-source)
  • Fine-tune on your laptop (with smaller models)
  • Or use cloud GPUs for $1-5/hour
  • Build real applications with $100-$1000 budget

Pre-trained models are like:

  • Free college education (the foundation is given to you!)
  • You just need to add your specialization

This democratizes AI! Anyone with a laptop and some domain data can build powerful AI systems. You don't need Google-scale resources!


Course navigation: Previous: Chapter 17 - Putting It All Together | Next: Chapter 19 - Three Transformer Architectures