Few-shot Examples

Show, Don't Just Tell

Zero-shot = instructions only. Few-shot = instructions plus a few input → output examples. Examples are the most reliable way to fix format and tone. Use 2–5 varied examples, including a tricky case.

Building a Few-shot Prompt

Example

Examples are wrapped in tags so the model can tell examples from the real input:

examples = [
    ("Delivery boy was rude and late", "DELIVERY"),
    ("Charged twice for one order", "PAYMENT"),
    ("Got size M instead of L", "WRONG_ITEM"),
    ("Love the app, fast delivery!", "PRAISE"),
]

def few_shot_prompt(text):
    lines = ["Classify the customer message into one label: DELIVERY, PAYMENT, WRONG_ITEM, PRAISE, OTHER.",
             "Reply with the label only.", "", "<examples>"]
    for msg, label in examples:
        lines.append(f"<example><message>{msg}</message><label>{label}</label></example>")
    lines += ["</examples>", "", f"<message>{text}</message>"]
    return "\n".join(lines)

print(few_shot_prompt("UPI money deducted but order shows failed"))

Output

Classify the customer message into one label: DELIVERY, PAYMENT, WRONG_ITEM, PRAISE, OTHER.
Reply with the label only.

<examples>
<example><message>Delivery boy was rude and late</message><label>DELIVERY</label></example>
<example><message>Charged twice for one order</message><label>PAYMENT</label></example>
<example><message>Got size M instead of L</message><label>WRONG_ITEM</label></example>
<example><message>Love the app, fast delivery!</message><label>PRAISE</label></example>
</examples>

<message>UPI money deducted but order shows failed</message>

Tips

  • Make examples varied (different lengths, languages, edge cases). Five near-identical examples make the model copy them too literally.
  • Include a Hinglish example if your users write Hinglish ("order abhi tak nahi aaya").
  • Keep the labels and format in examples exactly the same as what your code expects.