AI Hints
Home›Fine-Tuning LLMs Tutorial›Data: Prompts, Conversations and Sharing
Fine-Tuning LLMs · Lesson 77 of 120

Multi-turn Data

Conversations, not single questions.

⏱ 2 min read▶ Video at 1:57:41🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

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.

  • Real chats go back and forth
  • Follow-up questions
  • Examples with several turns

A two-turn conversation

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 AutoTokenizer

tok = 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|>

Training on conversations

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.

keyvalue
Formatmessages: the whole chat
Learnevery assistant turn
Don't learnuser turns
TRLassistant_only_loss=True

Summary

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.

Key takeaways
  • Several turns
  • messages format
  • Learn assistant turns only
Next up: Sharing Datasets — files and the Hub