Sub topic 6
Teaching by example: zero-shot and few-shot prompts
This chapter assumes you have read the earlier chapters on prompt anatomy and clear instructions. You should be comfortable reading short Python snippets and know what a prompt's instruction, input data, and output specification are.
Zero-shot and few-shot
A zero-shot prompt gives the model an instruction and no examples. The model relies only on what it learned in training and on your wording.
A few-shot prompt adds a handful of worked examples before the real input. Each example shows an input and the output you want. The model reads the pattern and continues it. Nothing is retrained. The examples exist only inside this one prompt, so the model "learns" the task only for this request.
Examples help most when the format or label set is hard to describe in words, or when the model keeps drifting from the style you want.
A zero-shot baseline
Start with zero-shot. It is the cheapest prompt, and it gives you a baseline to compare against.
Classify the sentiment of this review as positive, negative, or neutral.
Review: Setup was confusing but support helped.
Sentiment:A model may answer "Mixed, but mostly positive". That is a reasonable reading, but it is not one of your three labels. Few-shot examples are one way to fix this.
Building a few-shot prompt
The function below assembles a few-shot prompt from labeled examples. The model can infer both the task format and the label set from the pattern.
def build_few_shot_prompt(examples, new_input):
parts = ["Classify the sentiment of each review as positive, negative, or neutral.", ""]
for text, label in examples:
parts.append(f"Review: {text}\nSentiment: {label}\n")
parts.append(f"Review: {new_input}\nSentiment:")
return "\n".join(parts)
examples = [
("The battery lasts all week.", "positive"),
("It stopped working after two days.", "negative"),
("It arrived on Tuesday.", "neutral"),
]
print(build_few_shot_prompt(examples, "Setup was confusing but support helped."))Three things about this prompt matter:
- The instruction still comes first. Examples support the instruction. They do not replace it.
- Every example uses the same layout (
Review:thenSentiment:). The final line stops right afterSentiment:, so the most natural continuation is a single label. - Each label appears at least once, so the model sees the full set of allowed outputs.
Choosing good examples
The examples are part of your specification, so choose them with care.
- Cover every label. If no example is "neutral", the model may rarely produce it.
- Include a hard case. Examples that are all obvious teach little. Add one near a decision boundary.
- Keep the format identical. Small inconsistencies, such as "Positive" in one example and "positive" in another, are copied into the output.
- Balance the labels. If four of five examples are "positive", the model leans that way.
- Vary the content. Examples that are too similar make the model copy surface features, such as length or topic, instead of the underlying rule.
Order can matter too. Models often weight the last examples more, so avoid ending with a long run of the same label.
Common errors
| Problem | Symptom | Fix |
|---|---|---|
| Unbalanced labels | The model favors the majority label | Rebalance the examples |
| Inconsistent format | Output wanders from the layout | Make every example use the same template |
| Wrong examples | Output confidently repeats a mistake | Check each example label by hand |
| Too many examples | Higher cost, little gain | Remove examples one at a time and re-test |
| Examples leak into the answer | The model copies an example's content | Use varied examples and clear separators |
Zero-shot or few-shot?
Do not assume that more examples always help. Choose with a test.
- Use zero-shot when the task is common, the instruction is clear, and the output format is simple.
- Use few-shot when the output format is unusual, the labels are subtle, or zero-shot results are inconsistent.
- Keep in mind that examples consume space in the prompt, which adds cost and latency on every call.
The way to decide is to measure. The loop below scores a prompt against labeled test cases, so you can compare a zero-shot builder and a few-shot builder on the same cases. call_model is any function that sends a prompt to a model and returns its text.
def accuracy(call_model, build_prompt, cases):
correct = 0
for text, expected in cases:
reply = call_model(build_prompt(text)).strip().lower()
if reply == expected:
correct += 1
return correct / len(cases)Keep your test cases separate from the examples inside the prompt. If a test case also appears as an example, the score is inflated. Later chapters cover testing in more depth.
Exercise
Your few-shot sentiment prompt contains five examples: four labeled "positive" and one labeled "negative". It rarely returns "neutral" and labels most new reviews "positive". Name two changes to the examples that would help, and say how you would check that they worked.
Show answer
Add at least one "neutral" example, and rebalance the set so the labels appear roughly equally, for example two positive, two negative, and two neutral. Then run the old and new prompts through the same labeled test cases, using a function like accuracy, with test cases that are not in the prompt. The new prompt should score higher, and it should now return "neutral" for reviews that deserve it.