Lesson 6 / 29
Few-Shot Examples
Show the model the pattern with a handful of examples.
Show, do not just tell
Zero-shot prompting gives only instructions. Few-shot prompting adds a few input-output examples so the model copies the pattern: label set, tone, format, edge-case handling. Good examples are representative, diverse (include a tricky case), consistent with each other and correct, and ordered without an accidental bias (for example, not all "positive" first). Three to eight examples are common. Examples cost tokens on every call, so keep them short, and test whether each one really helps. Models can over-copy examples, so avoid unusual details you do not want repeated.
Building a few-shot prompt, run
I ran this plain-Python (standard library only) example. The function assembles an instruction, three labelled examples and the new text, ending with Label: so the model completes the label.
examples = [
("The delivery was fast and the box was perfect.", "positive"),
("It stopped working after two days.", "negative"),
("It arrived on Tuesday.", "neutral"),
]
def few_shot(text):
lines = ["Classify the sentiment as positive, negative or neutral.", ""]
for t, label in examples:
lines += [f"Text: {t}", f"Label: {label}", ""]
lines += [f"Text: {text}", "Label:"]
return "\n".join(lines)
print(few_shot("The screen cracked on day one."))
Output:
Classify the sentiment as positive, negative or neutral. Text: The delivery was fast and the box was perfect. Label: positive Text: It stopped working after two days. Label: negative Text: It arrived on Tuesday. Label: neutral Text: The screen cracked on day one. Label:
Include a hard example
If "It arrived on Tuesday" could be confused with positive, an example labelled neutral teaches the boundary better than ten easy cases.
Quick check: What does few-shot prompting add?
- Automatic fine-tuning
- More model parameters
- A faster GPU
- Input-output examples that show the desired pattern
Answer
Input-output examples that show the desired pattern — Examples let the model infer format and labels without retraining.