Papa Programmer
Lesson 13 of 20

From text predictor to assistant

9 min read

Optional

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 modelInstruction-tuned model
Trained onRaw textRaw text, then requests with good answers
Given a questionOften continues with more text like itAnswers the question
Good forCompleting text and further trainingChat and following instructions
What instruction tuning changes

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.

python
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:

text
<|im_start|>system
You answer in one sentence.<|im_end|>
<|im_start|>user
What is a token?<|im_end|>
<|im_start|>assistant

The 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.

Base models continue textThey write what most likely comes next, not an answer.
Train on good answersInstruction tuning uses the same next-token training on requests paired with answers.
Templates mark the turnsSpecial tokens show where each message starts and who wrote it.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools