28

From base model to assistant

What turns a text predictor into a useful assistant?

14 minExecutive path

From base model to assistant

With voice. You can pause and jump to any step.

Lesson complete

Subtitles

In lesson twenty-three you saw that a base model only continues text. In the last one, that tuning it with examples changes its behavior. Today: how it becomes an assistant.

Voice

No voice: the lesson advances on its own with subtitles.

The voice for this lesson is being recorded: for now it advances on its own with subtitles.

0:00 / 0:00

What you will achieve

  • Explain why a base model is not an assistant and name the steps that turn it into one: instruction tuning, preferences with a reward model, and RLHF or DPO.
  • Calculate, with the sigmoid, the probability that an answer wins from the points a reward model gives it.
  • Recognize the effects of optimizing human approval (Goodhart’s law, sycophancy, refusing too much or too little) and who decides how an assistant behaves.

Key ideas

  1. A base model only continues text: it was trained to guess the next piece, not to serve a person. Aligning it means getting it to do what the person intends: be helpful, honest, and harmless.
  2. Step one, instruction tuning: the model keeps training on examples of a prompt and its ideal answer, written by people. It teaches the shape of an answer with little data (InstructGPT: about 13 thousand prompts).
  3. Step two: comparing is easier than writing. Thousands of preferences train a reward model, a judge that gives points; the probability that an answer wins is the sigmoid of the difference in points.
  4. Step three, RLHF: the model practices against the judge, with a leash that keeps it close to how it used to write. Under too much pressure it pleases the judge and real quality falls (Goodhart’s law). DPO gets to the same place with no separate judge.
  5. Optimizing human approval can reward sycophancy, and the refusal threshold always costs something. The assistant is the base model plus a layer of behavior, decided by specific people.

What many people believe

Common belief An assistant like ChatGPT is a different model, trained from scratch to chat.

What is true It is a pretrained base model plus a layer of behavior: examples, preferences, and rules. That layer mostly changes how it responds, and in InstructGPT training it took less than two percent of the compute of GPT-3 (not counting the data).

Check yourself

Four questions. No grade, no account: they just show whether the idea stuck.

ConceptWhy does a base model not answer like an assistant?
ConceptWhat is a reward model?
CalculationA reward model gives answer A 1.5 points and answer B −0.5. Using the sigmoid of the difference, what is the probability that A is the preferred one?
ApplicationAfter an update that added users’ thumbs-up as a training signal, your in-house assistant starts agreeing with users even when they are wrong. What do you do first?

Under the hoodFor those who want the math and the code. You do not need it to follow the course.

Step one: supervised fine-tuning (SFT)

With pairs (prompt xx, ideal answer yy), instruction tuning minimizes cross-entropy only over the answer tokens:

LSFT(θ)=−∑t∈answerlog⁡πθ (yt∣x, y<t)\mathcal{L}_{\text{SFT}}(\theta) = -\sum_{t \in \text{answer}} \log \pi_\theta\,(y_t \mid x,\, y_{<t})

The prompt tokens contribute no error (a “loss mask”). It is the loss from lesson 19 with different data, and the fine-tuning from lesson 27. InstructGPT used about 13 thousand prompts with demonstrations written by about 40 contractors (Ouyang et al., 2022). Earlier, FLAN (Wei et al., 2021) had turned dozens of datasets into instructions; later, LIMA (Zhou et al., 2023) fine-tuned a 65-billion-parameter model on only a thousand carefully chosen examples, to argue that almost all knowledge arrives in pretraining and tuning mostly teaches the format.

A caveat: the line between “base” and “assistant” has become blurry. The Qwen2.5 technical report (2024) says its pretraining already uses synthetic data generated by instruction-tuned models; with that base model the contrast does not show. That is why the scene uses TinyLlama 1.1B base (the 3-trillion-token checkpoint) and TinyLlama-1.1B-Chat-v1.0, which according to its model card was fine-tuned on UltraChat (synthetic dialogues) and then aligned with DPO on UltraFeedback (64 thousand prompts with answers ranked by GPT-4, not by people). The outputs are real, greedy-decoded and unedited (build-outputs.py); it is a small model trained mostly on English, and its assistant is imperfect: the “two-line” email does not come out in two lines.

Step two: the reward model (Bradley–Terry)

If ywy_w is the preferred answer and yly_l the rejected one for prompt xx, the Bradley–Terry preference model assumes

P(yw≻yl∣x)=σ(rϕ(x,yw)−rϕ(x,yl))P(y_w \succ y_l \mid x) = \sigma\big(r_\phi(x, y_w) - r_\phi(x, y_l)\big)

and the reward model rϕr_\phi is trained by minimizing −E log⁡σ(rϕ(x,yw)−rϕ(x,yl))-\mathbb{E}\,\log \sigma\big(r_\phi(x,y_w) - r_\phi(x,y_l)\big). Only differences matter: adding a constant to every score changes nothing. In InstructGPT, labelers ranked between 4 and 9 answers per prompt (over about 33 thousand prompts), which gives (K2)\binom{K}{2} comparisons per prompt, and the reward model had 6 billion parameters with a scalar output. Labelers agreed with each other 72.6% of the time (±1.5). While collecting training data they were asked to put helpfulness above truthfulness and harmlessness; for the final evaluations, the other way around.

The idea of learning a reward from human comparisons comes from Christiano et al. (2017), in Atari games and simulated robots; Stiennon et al. (2020) applied it to text summaries, and InstructGPT to general instructions. Preferences do not always come from people: Constitutional AI (Bai et al., 2022) replaces human labels about what is harmful with a written list of principles (RLAIF). In the scene, r(y)=w⋅φ(y)r(y)=\mathbf{w}\cdot\boldsymbol{\varphi}(y) with four hand-assigned traits, and w\mathbf{w} is fit by logistic regression with a small L2 penalty.

Step three: optimizing on a leash (RLHF)

The model πθ\pi_\theta is tuned to maximize the reward without straying from the starting model πref\pi_{\text{ref}}; the “leash” is the KL divergence penalty:

max⁡πθ  Ex, y∼πθ[rϕ(x,y)]−β DKL(πθ(⋅∣x) ∥ πref(⋅∣x))\max_{\pi_\theta}\; \mathbb{E}_{x,\,y\sim\pi_\theta}\big[r_\phi(x,y)\big] - \beta\, D_{\mathrm{KL}}\big(\pi_\theta(\cdot\mid x)\,\|\,\pi_{\text{ref}}(\cdot\mid x)\big)

Its exact solution has a closed form:

π∗(y∣x)=1Z(x) πref(y∣x) exp⁡ ⁣(1β r(x,y))\pi^*(y\mid x) = \frac{1}{Z(x)}\,\pi_{\text{ref}}(y\mid x)\,\exp\!\Big(\tfrac{1}{\beta}\,r(x,y)\Big)

The scene computes that exact distribution; the “pressure” is 1/β1/\beta. In practice it is approximated with reinforcement learning algorithms such as PPO. InstructGPT applied a per-token KL penalty relative to the tuned model and also mixed in pretraining gradients (PPO-ptx) to reduce performance drops on some public tasks.

Optimizing too much: Goodhart’s law

Because rϕr_\phi is an imperfect stand-in, optimizing it too hard hurts real quality. Gao, Schulman, and Hilton (2022) measured it with a “gold” model playing the role of people: with d=DKLd=\sqrt{D_{\mathrm{KL}}}, the gold score follows Rbon(d)=d(α−βd)R_{\text{bon}}(d)=d(\alpha-\beta d) when picking the best of nn samples and RRL(d)=d(α−βlog⁡d)R_{\text{RL}}(d)=d(\alpha-\beta\log d) with reinforcement learning: it rises, peaks, and falls. In their setup, the KL penalty did not measurably improve that frontier (it might depend on hyperparameters): it mainly serves to decide how far to go.

In the toy, the traits are independent in the starting model (q=0.5, 0.3, 0.4, 0.2q = 0.5,\ 0.3,\ 0.4,\ 0.2), and the optimum keeps that independence: the probability of each trait becomes σ(logit⁡qj+wj/β)\sigma(\operatorname{logit} q_j + w_j/\beta). The practice judge has assumed weights w=(2.2, 0.5, 0.3, 0.45)\mathbf{w}=(2.2,\ 0.5,\ 0.3,\ 0.45) and “real quality” is another assumed rule, g=(3, −1.2, 0.3, −2.6)\mathbf{g}=(3,\ -1.2,\ 0.3,\ -2.6), which rewards correctness and penalizes padding and flattery. Real quality improves up to 1/β≈1.251/\beta \approx 1.25 (+0.90) and then falls, down to −0.97 at pressure 8, while the judge’s points always rise (from 0 to +1.89).

DPO: the model is secretly a reward model

Solving the closed form for rr gives r(x,y)=βlog⁡π∗(y∣x)πref(y∣x)+βlog⁡Z(x)r(x,y)=\beta\log\frac{\pi^*(y\mid x)}{\pi_{\text{ref}}(y\mid x)}+\beta\log Z(x). Since Bradley–Terry depends only on differences, Z(x)Z(x) cancels and the loss depends only on the model (Rafailov et al., 2023):

LDPO=−E log⁡σ ⁣(βlog⁡πθ(yw∣x)πref(yw∣x)−βlog⁡πθ(yl∣x)πref(yl∣x))\mathcal{L}_{\text{DPO}} = -\mathbb{E}\,\log\sigma\!\Big(\beta\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)} - \beta\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}\Big)

No sampling from the model is needed during tuning: you start from a fixed set of pairs. In the scene, the model family is πδ∝πref eδ⋅φ\pi_\delta\propto\pi_{\text{ref}}\,e^{\delta\cdot\varphi}; the DPO loss is computed over the 16 trait combinations with their normalization, and with the same regularization the weights β δ\beta\,\delta coincide with the reward model’s: an exact case of the equivalence. In a real model it is an approximation, because the model cannot represent every distribution. With preferences ranked by GPT-4 instead of people, Zephyr (2023) produced a competent assistant without new human annotation; TinyLlama Chat followed that recipe.

What the labs publish: OpenAI says ChatGPT was trained with RLHF using InstructGPT’s methods. Llama 2 used RLHF with rejection sampling and PPO (Touvron et al., 2023); Llama 3 used supervised fine-tuning, rejection sampling, and DPO, and explains that more complex reinforcement algorithms were less stable and harder to scale (Llama Team, 2024). For other commercial assistants the full recipe is not published: do not assume it.

What the layer costs

The “under 2%” comes from the InstructGPT paper: training the 175-billion-parameter tuned model took 4.9 petaflops/s-days, and the final PPO-ptx model, 60, versus 3,640 for GPT-3 (60 / 3,640 ≈ 1.6%; with the 4.9, ≈ 1.8%). It does not include the 6-billion-parameter reward model, data collection, or earlier experiments. In the evaluations, people preferred the outputs of the 175-billion-parameter InstructGPT to those of GPT-3 in 85% of cases (±3), and those of the 1.3-billion-parameter one to those of the 175-billion-parameter GPT-3.

In code

import math

def sigmoid(x):
    return 1 / (1 + math.exp(-x))

# 1) Reward model (Bradley–Terry): points of the preferred and the rejected answer
r_pref, r_rej = 1.8, -1.1
p = sigmoid(r_pref - r_rej)                # probability that the preferred one wins
print(round(p, 3), round(-math.log(p), 3))

# 2) Optimum on a KL leash: pi*(y) ∝ pi_ref(y) · exp(r(y) / beta), with three possible answers
pi_ref = [0.6, 0.3, 0.1]
r = [0.0, 1.0, 3.0]
for beta in (10.0, 1.0, 0.25):
    w = [q * math.exp(ri / beta) for q, ri in zip(pi_ref, r)]
    z = sum(w)
    print(beta, [round(x / z, 3) for x in w])

# 3) DPO: the same loss, but with the model's log-probabilities instead of points
beta = 0.1
logp_w, logp_l = -12.0, -15.0              # log pi_theta of the preferred and the rejected answer
ref_w, ref_l = -12.5, -14.0                # log pi_ref
margin = beta * ((logp_w - ref_w) - (logp_l - ref_l))
print(round(-math.log(sigmoid(margin)), 3))

Expected output:

0.948 0.054
10.0 [0.563, 0.311, 0.127]
1.0 [0.175, 0.238, 0.587]
0.25 [0.0, 0.001, 0.999]
0.621

With a large β\beta (a short leash) the model barely moves; with a small β\beta almost all the probability piles onto the best-scored answer and it loses variety.

What comes next

All of this shapes behavior from preferences. The next lesson uses the same reinforcement machinery, but with rewards that can be verified (the answer to a math problem is right or wrong) to teach the model to reason before it answers.

Decision lens

What this idea means if your job is to decide, not to code.

  • Ask who calibrated the model. The preferences of the people who rated it get baked in: for InstructGPT it was about forty contractors, who agreed with each other about three times out of four, and its authors warn that the model reflects that group, not “human values” in general. Ask who they were, how many, in which languages, and with what instructions.
  • Tone, length, degree of refusal, and agreeableness are product decisions, not technical facts. In InstructGPT, the people labeling training data were asked to put helpfulness above truthfulness and harmlessness. Ask for the behavior policy in writing (some companies publish theirs, like OpenAI’s Model Spec) and how they check that the model follows it.
  • Every provider update can change behavior without changing the model’s name. In April 2025 a GPT-4o update was rolled back for being sycophantic; according to OpenAI, a combination of changes played a part, including a new signal from users’ thumbs. Keep your own test cases (sycophancy, refusals, tone) and run them before adopting each version.
  • “Optimized with human feedback” does not guarantee truthfulness: what was optimized is a score that approximates people’s taste. Ask for evaluations independent of that score (lesson 34), and remember that alignment shapes behavior, it does not remove hallucinations (lesson 33).

Terms in this lesson

Full transcript
CommentsThis lesson’s public conversation: questions, facts to check, ideas.

Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub

Sources