Lesson 5 of 15 · Core Techniques
Zero-Shot and Few-Shot Prompting
Teach a model by example: zero-shot, one-shot and few-shot prompting, how in-context learning works, choosing good examples and a few-shot classifier in Python.
- 6 min read
- Beginner
- Reviewed
- What zero-shot and few-shot prompting are
- How a model learns a pattern from examples without being retrained
- When examples help, and how to choose good ones
- The ways examples can mislead a model
Telling versus showing
There are two ways to explain what you want: tell the model (instructions) or show it (examples). Most good prompts do some of both.
Modern models are good at zero-shot work, so start there. Add examples when the output must follow a specific format, set of labels, style or judgement that is easier to show than to describe.
See the difference
Prompt
What is the sentiment of this review? "The room was tiny, but the staff were lovely and breakfast was great."
Example response
The sentiment of this review is mixed but leans positive. The guest is unhappy with the size of the room, but praises the staff and the breakfast, which suggests an overall pleasant stay.
A reasonable answer, but a long sentence is no use if a program needs to store one label per review.
Building a few-shot prompt
The program below builds a classification prompt from a list of labelled examples, ending with the new message and an empty label for the model to fill in.
examples = [
("I was charged twice this month", "billing"),
("The app crashes when I open settings", "bug"),
("Could you add a dark mode?", "feature request"),
("Why did my price go up?", "billing"),
]
new_message = "My invoice shows the wrong company name"
lines = ["Classify each customer message as billing, bug or feature request.", ""]
for text, label in examples:
lines.append(f"Message: {text}")
lines.append(f"Label: {label}")
lines.append("")
lines.append(f"Message: {new_message}")
lines.append("Label:")
prompt = "\n".join(lines)
print(prompt)Every example has exactly the same shape: Message: then Label:. That consistency is what the model latches on to. Ending with an empty Label: makes the most likely continuation a single label.
Choosing good examples
- Representative: look like the real inputs the model will see.
- Diverse: cover different kinds of input, so the model learns the rule rather than one surface feature.
- Include edge cases: the tricky ones, such as a mixed review or a message that is both a bug and a billing problem, with the answer you want.
- Balanced: do not give four "billing" examples and one "bug"; the model may lean toward the common label.
- Consistent format: identical labels, punctuation and layout every time.
- Correct: the model copies mistakes as faithfully as it copies good answers.
Three to five well-chosen examples usually beat a dozen careless ones. For clarity, many people wrap each example in tags, such as <example> and </example>, so the model can tell examples from instructions.
When examples mislead
Examples are powerful, so they can also push the model the wrong way.
- Copying too closely. If every example is about hotels, the model may drag hotels into an answer about restaurants. If every example answer is 20 words, it will write 20 words even when 50 are needed.
- Picking up the wrong rule. If all your positive examples are short and all negative ones are long, the model might learn "short means positive".
- Recency. The last example can have extra influence. Shuffle examples, or make sure the last one is not unusual.
Common mistakes
Build a few-shot prompt that teaches the pattern with two examples, then leaves the last answer for the model.
pairs = [("cat", "gato"), ("house", "casa")]
new_word = "dog"
# Print a few-shot prompt in exactly this shape:
# English: cat
# Spanish: gato
# English: house
# Spanish: casa
# English: dog
# Spanish:
You need every answer to be one of exactly three labels, in a fixed format. What is the most reliable approach?
Summary
- Zero-shot: instructions only. Few-shot: instructions plus a few input-output examples.
- In-context learning lets a model pick up a pattern from the prompt without retraining.
- Use examples for formats, labels, style and judgement calls.
- Choose examples that are representative, diverse, balanced, consistent and correct, including edge cases.
- Watch for copying, wrong rules and recency effects.
Next: getting better answers on hard problems with step-by-step reasoning.
Finished reading? Tick it off to track your progress.