AI Engineering Tutorial · Prompting · Lesson 9 of 25
Examples in Prompts (Few-Shot Prompting)
Show the model two or three examples of input and ideal output; it copies the pattern more reliably than it follows a description.
Show, don't only tell
Describing a format in words is often ambiguous. Adding a few examples of input and the exact output you want ("few-shot prompting") is one of the most effective ways to get consistent results.
Good examples are:
- Representative of real inputs, including a tricky one.
- Varied, so the model learns the rule, not one example's wording.
- Consistent in format, exactly as you want the output.
examples = [("Refund hasn't arrived after 10 days", "billing"),
("App crashes when I open settings", "technical"),
("Charged twice and now the app won't load", "billing")] # tricky: billing wins
def few_shot_prompt(message):
shots = "\n".join(f"<example>\nMessage: {m}\nCategory: {c}\n</example>" for m, c in examples)
return (f"Classify each message as billing, technical or account.\n{shots}\n"
f"Message: {message}\nCategory:")
print(few_shot_prompt("I can't log in after changing my email"))
Output
Classify each message as billing, technical or account. <example> Message: Refund hasn't arrived after 10 days Category: billing </example> <example> Message: App crashes when I open settings Category: technical </example> <example> Message: Charged twice and now the app won't load Category: billing </example> Message: I can't log in after changing my email Category:
Wrap examples in tags (<example>) so they are clearly separate from the real input. See
Use examples.
Test yourself
What makes few-shot examples most useful?
They are all identicalThey are representative, varied and formatted exactly like the desired outputThere are at least 100 of themThey contain no edge cases
Show answer
Answer: They are representative, varied and formatted exactly like the desired output
Varied, realistic examples in the exact output format teach the pattern.