1Why Examples Beat Instructions
Showing is more powerful than telling.
**Zero-shot (no examples):** ``` Classify the sentiment of this review as positive, negative, or neutral. ```
**Few-shot (with examples):** ``` Classify the sentiment of customer reviews.
Review: "Absolutely love this product! Best purchase ever." Sentiment: positive
Review: "Terrible quality. Broke after one day." Sentiment: negative
Review: "It arrived on time. Works as expected." Sentiment: neutral
Review: "{{user_review}}" Sentiment: ```
**The difference:** - Zero-shot relies on the model's interpretation - Few-shot shows exactly what you want - Consistency improves dramatically
2How Many Examples?
More isn't always better.
**Guidelines:** - **1-2 examples**: Simple, clear-cut tasks - **3-5 examples**: Most tasks, good balance - **5-10 examples**: Complex or ambiguous tasks - **10+ examples**: Diminishing returns, consider fine-tuning
**Quality over quantity:** 3 excellent examples beat 10 mediocre ones.
**What makes a good example:** 1. Clearly correct output 2. Representative of real inputs 3. Shows the exact format you want 4. Diverse (covers different cases)
3Including Bad Examples
Showing what NOT to do is powerful.
**Pattern:** ``` Good example: Input: "The meeting is at 3pm tomorrow" Output: {"date": "2024-01-16", "time": "15:00"}
Bad example (don't do this): Input: "The meeting is at 3pm tomorrow" Output: "The meeting is scheduled for January 16th at 3 PM" Reason: Must return JSON, not prose
Now process: Input: "{{user_input}}" Output: ```
**When to use bad examples:** - Common mistakes you've seen - Ambiguous cases - Format violations
4Edge Cases and Diversity
Cover the edges to prevent failures.
**For a sentiment classifier:** ``` # Clear positive "Best product ever!" → positive
# Clear negative "Waste of money" → negative
# Neutral/factual "Arrived in 3 days" → neutral
# Mixed (edge case) "Great product but shipping was slow" → mixed
# Sarcasm (edge case) "Oh wonderful, it broke again" → negative
# Short/ambiguous (edge case) "ok" → neutral ```
**Checklist for example diversity:** - [ ] Positive cases - [ ] Negative cases - [ ] Edge cases - [ ] Minimal inputs - [ ] Maximum length inputs - [ ] Special characters
5Dynamic Few-Shot Selection
For production, select examples dynamically.
**Retrieval pattern:** 1. Store examples in a database 2. When user submits input, find similar examples 3. Include the most relevant 3-5 examples 4. Run the prompt
**Implementation:** ```typescript async function buildPrompt(userInput: string) { // Get examples similar to user input const examples = await findSimilarExamples(userInput, 3); let prompt = "Classify the following:\n\n"; for (const ex of examples) { prompt += `Input: ${ex.input}\nOutput: ${ex.output}\n\n`; } prompt += `Input: ${userInput}\nOutput:`; return prompt; } ```
This technique improves accuracy significantly for diverse inputs.