Llama 2-Chat is built from the foundation model Llama 2 through the following steps (Figure 4 and Section 3 of the paper).
- Supervised fine-tuning (SFT): Train on high-quality prompts and responses written by people.
- Collect human comparison data: People write prompts and choose the better of two responses produced by the model.
- Train reward models: Train models that return a numerical score for response quality, separately for helpfulness and safety.
- Train with rejection sampling: Generate K responses for each prompt and train on the one with the highest reward as the target.
- Train with PPO: Update the model with the reinforcement learning method PPO to maximize the reward.
- Iterate: Collect new comparison data from the new model's responses, update the reward models, and repeat steps 2β5 (RLHF-V1 through V5).
The figure below shows this flow. The left side is training the chat model; the right side is training the reward models.

1. SFT (Section 3.1): The team initially started from public instruction data, but found that quality mattered more than quantity, so instead of millions of third-party examples they used high-quality data they collected themselves. They collected 27,540 examples, and tens of thousands were enough to reach high-quality results. During training, each prompt and response are concatenated into a sequence of 4096 tokens, and the loss on the prompt tokens is set to zero so that only the response tokens are learned. The learning rate is 2Γ10β5, for 2 epochs.
2β3. Reward models (Sections 3.2.1 and 3.2.2): Besides choosing the better of two responses, annotators also select how much better it is (significantly better, better, slightly better, or negligibly better). Meta collected 1,418,091 comparisons. The reward model has the same architecture as Llama 2-Chat, with the next-token prediction head replaced by a head that outputs a single number (the reward). With rΞΈβ(x,y) as the reward for prompt x and response y, ycβ as the chosen response, and yrβ as the rejected response, it is trained with the following loss (Eq. (2) of the paper).
Lrankingβ=βlog(Ο(rΞΈβ(x,ycβ)βrΞΈβ(x,yrβ)βm(r)))
Ο is the sigmoid function. The larger the chosen response's reward is relative to the rejected response's, the smaller the loss. m(r) is a margin that depends on the size of the preference gap, so pairs rated "significantly better" are pushed to have a larger reward difference.
4. Rejection sampling (Section 3.2.3): The largest 70B model generates K responses per prompt, the response with the highest reward-model score is selected, and the model is trained on it in the same way as SFT. Smaller models (7B, 13B, etc.) are trained on responses selected by 70B, effectively a form of knowledge distillation that transfers the larger model's capabilities to the smaller ones.
5. PPO (Section 3.2.3): It maximizes the reward of the generation g produced by the model Ο for prompt p. The reward includes a penalty (KL divergence) that keeps the model from drifting too far from the original model Ο0β (Eqs. (3)(4)).
argΟmaxβΒ EpβΌD,gβΌΟβ[R(gβ£p)],R(gβ£p)=R~cβ(gβ£p)βΞ²DKLβ(ΟΞΈβ(gβ£p)β₯Ο0β(gβ£p))
R~cβ combines the two reward models. If the prompt is likely to elicit an unsafe response, or the safety reward Rsβ is below 0.15, the safety reward is used; otherwise the helpfulness reward Rhβ is used. Ξ² is 0.01 for 7B and 13B and 0.005 for 34B and 70B. Up to RLHF-V4, only rejection sampling was used; after that, PPO was applied on top of rejection sampling.
Ghost Attention (GAtt) (Section 3.3): The first RLHF models forgot an initial instruction such as "act as ..." after a few turns. With GAtt, the instruction is attached to every user message in a training dialogue when generating responses; during training, the instruction is kept only in the first message and the loss on earlier turns is set to zero. This let the model follow the instruction for more than 20 turns.