The Kaitchup – AI on a Budget

The Kaitchup – AI on a Budget

Share this post

The Kaitchup – AI on a Budget
The Kaitchup – AI on a Budget
Fine-tuning LLMs with a Chat Template
Copy link
Facebook
Email
Notes
More

Fine-tuning LLMs with a Chat Template

Turning Llama 3 into a pirate

Benjamin Marie's avatar
Benjamin Marie
May 20, 2024
∙ Paid
6

Share this post

The Kaitchup – AI on a Budget
The Kaitchup – AI on a Budget
Fine-tuning LLMs with a Chat Template
Copy link
Facebook
Email
Notes
More
7
Share
Generated with DALL-E

Instruct large language models (LLM), a.k.a., chat models, are base LLMs fine-tuned on instruction datasets. In an instruction dataset, each training example is an instruction paired with a possible answer to this instruction. In other words, each example has two columns, an instruction, and an answer.

Fine-tune Tiny Chat Models with Apple OpenELM and ORPO

Fine-tune Tiny Chat Models with Apple OpenELM and ORPO

Benjamin Marie
·
May 9, 2024
Read full story

Nonetheless, chat models are causal LLMs. They expect a sequence of tokens as input to predict the next token of this input. It means that we have to format each training example so that the columns “instruction” and “answer” become a unique sequence of tokens.

The best way to do this is to create and apply a chat template. With a chat template, we can systematically apply the same format to all the training examples and make sure that the same format is used at inference time. Templates are also very handy to make sure that we don’t forget any of the special tokens (BOS, EOS, etc.), always include the same “system” instructions, and don’t introduce any unwanted space that would significantly reduce the quality of the output.

The Kaitchup – AI on a Budget is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.

In this article, I explain how to create and modify a chat template. We will see how Llama 3’s chat template works and apply a new one through fine-tuning. For demonstration, I fine-tune Llama 3 8B with a chat template and a dataset of pirate speak to turn it into a pirate chat model.

I also made a notebook showing this fine-tuning and how to create/modify chat templates for Llama 3. The same method can be applied to any other LLMs:

Get the notebook (#71)

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 The Kaitchup
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More