In twenty twenty three, researchers found a shortcut. You can train on preferences directly, with no reward model, and no reinforcement learning. It's called DPO.
DPO stands for direct preference optimisation. It uses two copies of the model. The one being trained, and a frozen reference copy. The loss pushes the chosen answer to become more likely than in the reference, and the rejected answer less likely.
One setting matters most. Beta. It controls how far the model may move away from the reference. A small beta, like zero point one, allows big changes. A large beta keeps the model close to where it started. With LoRA, switching the adapter off gives the reference model for free.
Here's our preference data. Two hundred pairs. Each has a customer message. The chosen answer is our friendly reply. The rejected answer is curt, and unhelpful. Run it. That's the whole idea. Same message, a better answer, and a worse one.
Pairs: 200
Customer: Hello! The driver was rude when delivering Dune.
Chosen: I'm so sorry about your Dune order. I've passed this to our manager, w
Rejected: Not our problem.
Also uses prefs.py
from datasets import load_datasetSYSTEM = "Write a short reply to the customer."def to_pair(row): prompt = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": row["message"]}] return {"prompt": prompt, "chosen": [{"role": "assistant", "content": row["chosen"]}], "rejected": [{"role": "assistant", "content": row["rejected"]}]}pairs = load_dataset("json", data_files="prefs_train.jsonl")["train"]pairs = pairs.map(to_pair, remove_columns=["message"])
Let's recap. DPO trains on preferences directly. It compares the model with a frozen reference copy. And beta controls how far it may move. Next, we train with DPO, using TRL.