A freshly trained language model can continue any text, but it isn’t trying to help you. The step that turns it into an assistant that answers the question, in a sensible tone, is mostly RLHF: reinforcement learning from human feedback. People compare pairs of answers, a reward model learns to predict their choices, and the chatbot is tuned to score well with it, on a leash. Here you’ll be the person comparing, then loosen the leash and watch the chatbot game its own reward.
From text predictor to assistant#
Pretraining teaches a model to predict the next word of internet text. Ask it a question and it might answer, or list five more questions, or carry on as if it were a forum thread. OpenAI’s 2022 InstructGPT paper set out the three-step recipe that most chat assistants have followed since:
- Supervised fine-tuning: people write good answers to prompts, and the model is trained to imitate them.
- Reward model: the model writes several answers to each prompt, people rank them (4 to 9 at a time, in InstructGPT), and a second model learns to predict their rankings.
- Reinforcement learning: the chatbot is tuned to write answers the reward model scores highly, without drifting too far from what it learned in step 1.
It worked strikingly well. In the paper’s human evaluations, answers from a 1.3-billion-parameter InstructGPT were preferred to those from the 175-billion-parameter GPT-3: a model 100 times smaller, made more useful by feedback.
Teaching a model what “good” means#
Nobody can write a formula for a good answer. But anyone can look at two answers and say which is better, and that’s the trick. A reward model gives every answer a score, and learns from comparisons: the bigger the gap between two scores, the more confident it is that people prefer the higher one. (This is the Bradley–Terry model, from 1952: the chance A beats B depends only on the difference in their scores.)
Try it. Your choices are the only training data the reward model below has.
With only your comparisons, the reward model learns whatever you value: pick the warm answers and “friendly” gets a big weight; pick the terse ones and it won’t. That’s a feature, not a bug, but it means the result reflects whose feedback it was. The InstructGPT authors were direct about it: their method aligned the model to “the stated preferences of a specific group of people (mostly our labelers and researchers)”, not to everyone.
Predict first
Now add 1,500 comparisons from other raters. They judged ordinary answers, 5 to 150 words long, and within that range they tended to prefer the more detailed one. None of them ever saw a 400-word answer. Which answer will the reward model rank first?
With the other raters’ comparisons, the reward model’s weights land close to what the raters really value: helpfulness 2.75 (they value it at 3), a confidently wrong answer −2.98 (−3), friendliness 0.75 (0.8). Its scores for seven of the eight answers are within half a point of what people think. But the ramble scores 5.4, the highest of all, while people rate it 1.4. It’s helpful and friendly, and far too long, which the reward model never learned to mind.
This isn’t a contrived flaw. A 2023 study by Prasann Singhal and colleagues found reward models’ scores strongly tied to answer length, and that on two of the three datasets they studied, 70 to 90% of the reward gained in RLHF came from answers simply getting longer. A reward that measured length alone reproduced most of RLHF’s improvement.
Optimising, on a leash#
In step 3, the chatbot is trained to give the answers the reward model scores highly. InstructGPT used a reinforcement learning algorithm called PPO for this. But there’s a leash: a penalty for drifting too far from how the model behaved before, measured by the KL divergence between the two. One number, β (beta), sets how strong the leash is.
The DPO paper (Rafailov and colleagues, 2023) showed the best possible chatbot under this rule has a simple form: take how often the original model gives each answer, multiply by ereward ÷ β, and rescale so it adds up to 100%. With a fixed list of answers, we can compute it exactly, which is what the figure below does.
Predict first
Remove the leash completely, so the chatbot chases the reward model’s score and nothing else. What happens to what people actually think of its answers?
At first, loosening the leash helps everyone. Bad answers fade, the good ones grow, and what people think rises from 1.68 to a peak of 2.72 at β = 1.5. Then the ramble starts taking over. The reward model’s score keeps climbing, all the way to 5.40, but what people think falls back, to 1.40 with no leash at all: worse than before any of this training. A reward model is a stand-in for people, and a stand-in gets less reliable the harder you lean on it.
OpenAI researchers measured exactly this curve on real language models in 2022: as a chatbot was optimised harder against a learned reward model, a more trustworthy “gold” score rose at first, then fell. It’s Goodhart’s law, “when a measure becomes a target, it ceases to be a good measure”, and the leash is one of the main defences, along with more and better comparisons and retraining the reward model as the chatbot changes.
DPO, and what came next#
Training a separate reward model and running reinforcement learning is complex and can be unstable. Direct Preference Optimization (DPO) uses the formula above in reverse: since the best chatbot and the reward determine each other, you can train the chatbot directly on the pairs of answers, with a simple loss that raises the odds of the chosen answer relative to the rejected one. Same goal, same leash, no reward model and no reinforcement learning loop. It’s now widely used, especially for open models.
Other variations replace some of the human judgements with an AI judging answers against written principles (Anthropic’s Constitutional AI), or reward a model for reaching answers that can be checked automatically, like the result of a maths problem or code that passes its tests. All share the same weakness: a model will exploit whatever its reward gets wrong, so the reward has to be watched as carefully as the model. See the reinforcement learning explainer for the classic example of a boat that circled forever instead of finishing the race.
In code#
The whole pipeline in miniature, with NumPy: simulated raters compare pairs, a Bradley–Terry reward model learns from them, and the leash formula picks the chatbot’s odds.
import numpy as np
rng = np.random.default_rng(0)
def people(a): # what raters value: [helpful 0-1, friendly 0-1, words]
return 3 * a[:, 0] + 0.8 * a[:, 1] + 0.6 * np.minimum(a[:, 2], 120) / 120 - 0.012 * np.maximum(a[:, 2] - 150, 0)
# 1. Raters compare pairs of ordinary answers (5 to 150 words); the better one usually wins.
a = np.column_stack([rng.random((3000, 2)), rng.uniform(5, 150, 3000)])
b = np.column_stack([rng.random((3000, 2)), rng.uniform(5, 150, 3000)])
a_wins = rng.random(3000) < 1 / (1 + np.exp(people(b) - people(a)))
feats = lambda x: x * [1, 1, 0.01] # length in hundreds of words
diff = np.where(a_wins[:, None], feats(a) - feats(b), feats(b) - feats(a))
# 2. Reward model: weights w that make sigmoid(w · (winner - loser)) high.
w = np.zeros(3)
for _ in range(3000):
p = 1 / (1 + np.exp(-diff @ w))
w += 1.0 * ((1 - p)[:, None] * diff).mean(axis=0)
print("reward model weights:", w.round(2))
# 3. Leashed policy over four answers: pi = pi_before * exp(reward / beta), normalised.
answers = np.array([[1, 1, 50], [1, 0, 110], [0.2, 0.5, 23], [1, 1, 400]]) # the last is a ramble
before = np.array([0.4, 0.3, 0.25, 0.05])
reward = feats(answers) @ w
for beta in [4, 1, 0.25]:
pi = before * np.exp(reward / beta)
pi /= pi.sum()
print(f"beta {beta}: policy {pi.round(2)}, reward {pi @ reward:.2f}, people {pi @ people(answers):.2f}")
# reward model weights: [3.07 0.69 0.53]
# beta 4: policy [0.46 0.31 0.14 0.09], reward 3.68, people 3.25
# beta 1: policy [0.42 0.22 0.01 0.34], reward 4.54, people 2.99
# beta 0.25: policy [0. 0. 0. 0.99], reward 5.88, people 1.41For real models, Hugging Face’s TRL library has a trainer for each stage: SFTTrainer for supervised fine-tuning, RewardTrainer for the reward model, DPOTrainer, and reinforcement learning trainers such as GRPOTrainer and RLOOTrainer.
What to remember
- RLHF turns a text predictor into an assistant: people compare answers, a reward model learns their preferences, and the model is tuned towards higher rewards.
- A reward model only knows what its comparisons showed it, and it can confidently overrate answers unlike any it saw, such as very long ones.
- The leash (a KL penalty, strength β) keeps the model near its starting point; without it, optimising the stand-in reward makes answers worse. DPO reaches the same goal without a separate reward model.