Real chats go back and forth. The customer asks something, the assistant answers, then the customer asks a follow up. To teach that, examples need several turns.
Here's a conversation with two turns. The customer asks about Dune. Then, how much is it? The word it only makes sense because of the first turn. We print it through the chat template. Run it. Every message is wrapped in its own start and end tokens. The model sees the whole conversation, so it can learn what it refers to.
app.pypython
from transformers import AutoTokenizertok = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")conversation = [ {"role": "system", "content": "You help customers of Riverside Books."}, {"role": "user", "content": "Do you have Dune?"}, {"role": "assistant", "content": "Yes, we have 3 copies of Dune."}, {"role": "user", "content": "How much is it?"}, {"role": "assistant", "content": "Dune is £9.99 in paperback."},]print(tok.apply_chat_template(conversation, tokenize=False))
Output
<|im_start|>system
You help customers of Riverside Books.<|im_end|>
<|im_start|>user
Do you have Dune?<|im_end|>
<|im_start|>assistant
Yes, we have 3 copies of Dune.<|im_end|>
<|im_start|>user
How much is it?<|im_end|>
<|im_start|>assistant
Dune is £9.99 in paperback.<|im_end|>
For conversations, use the messages format, with the whole chat in one list. Usually, the model should learn every assistant turn, but none of the user turns. In TRL, the assistant only loss setting does this, for chat templates that mark the assistant's turns.
Let's recap. Multi-turn examples teach follow-ups. Put the whole conversation in one messages list. And train only on the assistant's turns. Next, saving and sharing datasets.