Skip to content

< all problems14 · Level 01, LLM APIs

Format a Conversation With a Chat Template

medium · implement · LLM Fundamentals

Implement two functions using the provided tokenizer and its chat template:

  1. format_chat(messages): turn [{"role": ..., "content": ...}] into the exact prompt string the model expects, ready for the assistant to continue.
  2. format_for_training(messages): the same conversation including the final assistant turn, for fine-tuning.

If the tokenizer has no template, fall back to role: content lines.

The catch: the two differ by one flag. Get it wrong and the model writes the user's next message instead of answering.