From text predictor to assistant
9 min read
A base model continues text
After Pre-trainingThe first and largest stage of training. The model reads a huge amount of general text and learns to predict what comes next., a model is good at one thing: writing the text that most likely comes next. Ask it "How do I boil an egg?" and it may answer. It may also continue with "How do I fry an egg?", because many web pages are lists of questions. The model is not being unhelpful. It is doing exactly what it was trained to do.
Train on requests and good answers
Instruction tuningTraining a pre-trained model further on examples of requests and good answers, so that it follows instructions and replies as an assistant instead of only continuing text. fixes this habit with more training. The data is a set of examples, and each example pairs a request with a good answer. The training method is the same as before: predict the next token and lower the loss. The difference is in the text. The model now sees thousands of conversations in which the reply answers the request.
OpenAI's 2022 InstructGPT paper, by Ouyang and others, showed how much this matters. It started with about 13,000 prompts with answers written by people. People who rated the outputs preferred the answers of the 1.3 billion parameter InstructGPT model to those of the 175 billion parameter GPT-3 base model. A model more than 100 times smaller won, because it had learned to follow instructions.
| Base model | Instruction-tuned model | |
|---|---|---|
| Trained on | Raw text | Raw text, then requests with good answers |
| Given a question | Often continues with more text like it | Answers the question |
| Good for | Completing text and further training | Chat and following instructions |
Chat templates mark who said what
A conversation has turns, but a model reads one long string of tokens. A chat template turns the list of messages into that string. It wraps each message in special tokens that the model learned during instruction tuning.
The program below uses ChatML, the format that Qwen models use. Other model families use other markers, but the idea is the same.
messages = [
{"role": "system", "content": "You answer in one sentence."},
{"role": "user", "content": "What is a token?"},
]
def to_chatml(messages):
text = ""
for message in messages:
text += f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>\n"
return text + "<|im_start|>assistant\n"
print(to_chatml(messages))It prints the string that the model actually reads:
<|im_start|>system
You answer in one sentence.<|im_end|>
<|im_start|>user
What is a token?<|im_end|>
<|im_start|>assistantThe string ends with the start of an assistant turn. The model's next tokens are therefore the assistant's reply. When the model writes its own end marker, the program stops the loop.
Each model expects its own template, and a wrong template makes the replies worse. Libraries load the right one for you. In the Hugging Face transformers library, tokenizer.apply_chat_template(messages) builds the string from the model's own settings. Lesson 16 uses it.
Three roles
- System. Standing instructions for the whole conversation, such as tone, rules or a persona. This is the System promptThe instructions at the start of a prompt that set the model's role, its rules and the format of its answer. It stays the same on every call..
- User. What the person asks.
- Assistant. What the model replies. Earlier assistant turns are sent back to the model with each new request, so that it can see the whole conversation.
Instruction tuning has limits
Instruction tuning teaches the format of a good answer more than new facts. The knowledge still comes from pre-training. It also struggles to teach qualities that are hard to write down as one right answer, such as which of two good replies is more helpful. Lesson 14 covers the next stage, which trains on people's preferences between answers.
What does instruction tuning change?
A team trains a base model on 13,000 requests paired with good answers.
Instruction tuning uses the same next-token training on conversations, so the model learns the shape of a good reply.
Why do the replies get worse?
You send messages to a model in another model family's chat template.
Each model learns its own template during instruction tuning. Use the tokenizer's apply_chat_template to build the right one.
Where should this instruction go?
Every reply in the conversation must be in formal English.
The system message holds standing instructions for the whole conversation, such as tone and rules.
Your reading position is saved
0 of 20 complete
Lesson complete
From text predictor to assistant
Up next