LLM Kitchen
Explanations

Part VI ยท Prompt engineering

Ch 19. Learning from examples in the prompt

Show, donโ€™t tell

Write โ€œhot -> coldโ€ and โ€œbig -> smallโ€ on two lines, then โ€œhappy ->โ€, and a model will usually answer sad. Nobody told it the task was opposites; it worked that out from the examples, without any more training. This is , and giving a few examples like this is called few-shot prompting.[1]

Write โ€œhot -> coldโ€ and โ€œbig -> smallโ€ on two lines, then โ€œhappy ->โ€, and a model will usually answer sad. Nobody told it the task was opposites. It worked it out from the examples, during a single forward pass, without changing a single weight. This is , and giving a few examples like this is called few-shot prompting.[1][1]

Here it is for two tasks: opposites, and a rule nobody could guess, where fruits are โ€œblipโ€ and animals are โ€œzorkโ€. Change how many examples the prompt has:

Try it

Learning from examples

Recorded ยท Qwen2.5-1.5B-Instruct and GPT-2 small
Task
Examples in the prompt
Model

The prompt, for โ€œhappyโ€

hot -> cold
big -> small
up -> down
fast -> slow
happy ->
Chance that Qwen2.5-1.5B-Instruct answers right, by number of examples
WordRight answer0 examples1 example2 examples4 examples
happysad0.9%66%83%96%
lightdark0.5%48%30%13%
openclosed1.9%62%73%78%
oldyoung<0.1%47%78%75%

No weights change between the columns: the only difference is how many examples sit in the prompt. With none, the model has no way to know what โ€œ->โ€ is asking for.

Add your own word to the table, and Qwen2.5-0.5B-Instruct and GPT-2 score it live. Choose a model tier to run a real model in your browser.

With no examples, Qwen2.5-1.5B-Instruct gives sad about a 1% chance; with four examples, 96%. The made-up rule works too, though less reliably.

With no examples, Qwen2.5-1.5B-Instruct gives sad about 1% for โ€œhappy ->โ€; with four, 96%. For the made-up rule, the right label goes from almost nothing to between 22% and 78%. Not every word gets easier: โ€œlight -> darkโ€ became less likely as examples were added.

The much smaller GPT-2 mostly fails: it repeats the word it was given, or copies the last answer it saw. Bigger models are much better at this.[1]

GPT-2 small mostly fails. For opposites it usually just repeats the word it was given, and for the made-up rule it answers โ€œzorkโ€ for everything, the label of the last example it saw. In-context learning gets much better as models get bigger.[1][1]

Answers longer than one token

GPT-2 splits the made-up labels in two: โ€œblipโ€ is โ€ blโ€ + โ€œipโ€, and โ€œzorkโ€ is โ€ zโ€ + โ€œorkโ€. The chance of a whole answer is the product of its tokensโ€™ chances, each given everything before it:

p(zork)=p(z|prompt)ร—p(ork|prompt,z)

For โ€œhorse ->โ€ after four examples, thatโ€™s 0.69 ร— over 0.99, about 0.69: once GPT-2 has written โ€ zโ€, the โ€œorkโ€ is close to certain. So the whole choice is made at the first token, โ€ zโ€ or โ€ blโ€, and after four examples GPT-2 picks โ€ zโ€ for the fruits too.

Do the labels have to be right?

You might expect wrong examples to teach a wrong rule. A study of 12 models found that mixing up the examplesโ€™ labels at random barely mattered: what the examples taught was which labels to use and what the answer should look like.[2] But bigger models do follow labels that are wrong in a consistent way.[3] Here, eight short reviews are labeled positive or negative before 24 new ones. Pick how the eight are labeled:

If examples teach a rule, wrong examples should teach a wrong one. A study of 12 models, GPT-3 among them, found otherwise: swapping the examplesโ€™ labels at random barely hurt. What mattered was that the examples showed which labels to use, what the inputs look like, and the layout of the answers.[2][2] A later study found that size changes this. Given examples with every label flipped, small models stuck to what they had learned in training, while the largest flipped their answers too, and mainly large models could learn labels with no meaning, like foo and bar for positive and negative.[3][3] Here, eight short reviews are labeled before 24 new ones. Pick how the eight are labeled:

Recorded runs

Do the labels have to be right?

Recorded ยท Qwen2.5-1.5B-Instruct and GPT-2 small
The eight examples
  1. I loved every minute of it. positive
  2. The food was cold and bland. negative
  3. What a wonderful surprise! positive
  4. It broke after one day. negative
  5. The staff were kind and helpful. positive
  6. I want my money back. negative
  7. Best concert I have ever been to. positive
  8. The room smelled awful. negative
How each model labeled 24 new reviews
ModelChance of answering โ€œpositiveโ€ or โ€œnegativeโ€Labels by the real sentimentFollows the examplesโ€™ labels
Qwen2.5-1.5B-Instruct100%24 of 24 24 of 24
GPT-2 small96%21 of 24 21 of 24

Without examples, Qwen2.5-1.5B-Instruct gives positive or negative only about a quarter of its chances; with any eight examples, nearly all of them. That is the format at work. With half the labels wrong, it still labels all 24 reviews correctly. But flip every label and it follows the flipped labels for 23 of 24 reviews. GPT-2 small gets 21 right with the right labels, and with flipped or made-up labels it gives every review the same one.

Without examples, Qwen2.5-1.5B-Instruct puts only 27% of its probability on โ€ positiveโ€ or โ€ negativeโ€, and GPT-2 almost none; with any eight examples, Qwen puts over 99% there and GPT-2 over 90%. That is the label set and the format at work. With half the labels wrong, Qwen still labels all 24 reviews by their real sentiment: labels that contradict each other teach it nothing, so it falls back on what it knows. Flip every label, though, and it follows the flipped labels for 23 of 24 reviews, and with foo and bar it uses the made-up labels correctly 23 times. In that study, it took far larger models to follow flipped labels; Qwen is a much newer model, on a much easier task. GPT-2 small gets 21 right with the right labels and 17 with half wrong, and with flipped or made-up labels it gives every review the same label.

How the model copies a pattern

How does the model use the examples? Part of the answer is a kind of attention head that finds earlier places where the current token appeared and looks at what came right after it. On โ€œa b โ€ฆ aโ€, it looks back at โ€œbโ€. These induction heads may do much of the work of learning from examples.[4] GPT-2 small has one doing exactly this on the prompts above:

What does the model do with the examples? Part of the answer is a kind of attention head that finds earlier places where the current token appeared and looks at whatever came right after it. On โ€œa b โ€ฆ aโ€, it looks back at โ€œbโ€. These induction heads appear during training at the same moment models suddenly get better at learning from their context, and they may be how most in-context learning works.[4][4] GPT-2 small has one doing exactly this on the prompts above:

Try it

Where the arrow looks

Recorded ยท GPT-2 small
Task

In GPT-2 small, attention head 5.0 (layer 5, head 0) sends 98% of the final arrow's attention to the tokens right after the earlier arrows: the answers it could copy the pattern from.

From the final arrow, it looks almost entirely at the earlier answers, the words that followed the earlier arrows. The rest of the network can then use what it found to write an answer of the same kind. Copying is also why examples can work too well: a model can pick up their exact words, length, or quirks along with the rule, so examples that differ from each other in everything but the rule teach the rule best.

Two heads make an induction head

An induction head canโ€™t do this alone, because a tokenโ€™s query and key say nothing about which token came before it. An earlier head supplies that. A previous-token head copies each tokenโ€™s predecessor into it, so the position of the answer โ€œcoldโ€ also carries a note that it came after an arrow. The induction headโ€™s query at the final arrow then matches the positions that came after arrows, which are the answers, and its values copy those answers forward.[4] That takes two layers of attention, one after the other, so a one-layer model canโ€™t form one. Head 5.0 in the widget sends 98% of the final arrowโ€™s attention to the four answers to the opposites, rising from 5% on the first to 35% on the last.

Show or tell?

You can also just say what you want, with no examples. For a familiar task, that is often enough. Examples help most when the answer has to look a certain way, or uses labels or a style the model canโ€™t guess. They make every prompt longer, though.

Instructions alone, called zero-shot prompting, are often enough for familiar tasks like summarizing or translating. Examples earn their place when the answer has to follow an exact format, or uses labels or a style the model canโ€™t guess from a description, as the reviews above showed. They also cost tokens on every call, though a fixed set at the start of the prompt can be cached (Ch 26). The next chapter is about the other half: telling the model what to do.

Recipe card

Recap

  • A few examples in the prompt can teach a model a new task, with no change to its weights.
  • Examples teach the label set and the format; with labels that contradict each other, a model falls back on what it knows, while consistently wrong labels can pull a capable model along.
  • Attention heads that look at โ€œwhat came after this token last timeโ€ help copy the pattern.
  • Bigger models learn from examples much better; small ones often just repeat the last answer.
  • A whole answerโ€™s chance is the product of its tokensโ€™ chances; an induction head relies on an earlier previous-token head.

Next question: chat models come with a system prompt. Does telling the model who to be change what it can do?

Sources

  1. Tom B. Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020.
  2. Sewon Min et al. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?. EMNLP 2022.
  3. Jerry Wei et al. (2023). Larger language models do in-context learning differently. arXiv.
  4. Catherine Olsson et al. (2022). In-context Learning and Induction Heads. Transformer Circuits Thread.

Sources

  1. Tom B. Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020.
  2. Sewon Min et al. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?. EMNLP 2022.
  3. Jerry Wei et al. (2023). Larger language models do in-context learning differently. arXiv.
  4. Catherine Olsson et al. (2022). In-context Learning and Induction Heads. Transformer Circuits Thread.

Sources

  1. Tom B. Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020.
  2. Sewon Min et al. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?. EMNLP 2022.
  3. Jerry Wei et al. (2023). Larger language models do in-context learning differently. arXiv.
  4. Catherine Olsson et al. (2022). In-context Learning and Induction Heads. Transformer Circuits Thread.