Chat Templates
Learn the basics and customization options for chat templates including the Alpaca format.
Last updated
Was this helpful?
Learn the basics and customization options for chat templates including the Alpaca format.
Last updated
Was this helpful?
Text Classification by Timotheeee
Multiple Datasets by Flail
A bit issue if you didn't notice is the Alpaca dataset is single turn, whilst remember using ChatGPT was interactive and you can talk to it in multiple turns. For example, the left is what we want, but the right which is the Alpaca dataset only provides singular conversations. We want the finetuned language model to somehow learn how to do multi turn conversations just like ChatGPT.
So we introduced the conversation_extension
parameter, which essentially selects some random rows in your single turn dataset, and merges them into 1 conversation! For example, if you set it to 3, we randomly select 3 rows and merge them into 1! Setting them too long can make training slower, but could make your chatbot and final finetune much better!
Then set output_column_name
to the prediction / output column. For the Alpaca dataset dataset, it would be the output column.
We then use the standardize_sharegpt
function to just make the dataset in a correct format for finetuning! Always call this!
We can now specify the chat template for finetuning itself. The very famous Alpaca format is below:
But remember we said this was a bad idea because ChatGPT style finetunes require only 1 prompt? Since we successfully merged all dataset columns into 1 using Unsloth, we essentially can create the below style chat template with 1 input column (instruction) and 1 output:
We just require you must put a {INPUT}
field for the instruction and an {OUTPUT}
field for the model's output field. We in fact allow an optional {SYSTEM}
field as well which is useful to customize a system prompt just like in ChatGPT. For example, below are some cool examples which you can customize the chat template to be:
For the ChatML format used in OpenAI models:
Or you can use the Llama-3 template itself (which only functions by using the instruct version of Llama-3): We in fact allow an optional {SYSTEM}
field as well which is useful to customize a system prompt just like in ChatGPT.
Or in the Titanic prediction task where you had to predict if a passenger died or survived in this Colab notebook which includes CSV and Excel uploading: https://colab.research.google.com/drive/1VYkncZMfGFkeCEgN2IzbZIKEDkyQuJAS?usp=sharing
Assuming your dataset is a list of list of dictionaries like the below:
You can use our get_chat_template
to format it. Select chat_template
to be any of zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
, and use mapping
to map the dictionary values from
, value
etc. map_eos_token
allows you to map <|im_end|>
to EOS without any training.
You can also make your own custom chat templates! For example our internal chat template we use is below. You must pass in a tuple
of (custom_template, eos_token)
where the eos_token
must be used inside the template.