Few-shot learning allows you to teach AI models new tasks or patterns by providing a small number of examples directly in the prompt.
Zero-Shot vs Few-Shot vs Many-Shot
| Type | Examples | Best For |
|---|---|---|
| Zero-shot | 0 | Simple, well-understood tasks |
| One-shot | 1 | Tasks with clear patterns |
| Few-shot | 2-5 | Complex or domain-specific tasks |
| Many-shot | 5+ | Nuanced or unusual formats |
Basic Few-Shot Pattern
Convert informal text to formal business English.
Example 1:
Informal: "gonna need those reports asap"
Formal: "I require those reports at your earliest convenience."
Example 2:
Informal: "cant make the meeting, something came up"
Formal: "I regret to inform you that I am unable to attend the meeting due to an unforeseen circumstance."
Example 3:
Informal: "the project is kinda behind schedule ngl"
Formal: [Model completes this]Effective Example Selection
Diversity Matters
Include examples that cover different scenarios:
Classify the sentiment of product reviews.
Example 1 (Positive):
Review: "Absolutely love this product! Best purchase ever."
Sentiment: Positive
Example 2 (Negative):
Review: "Complete waste of money. Broke after one day."
Sentiment: Negative
Example 3 (Mixed):
Review: "Great features but terrible battery life."
Sentiment: Mixed
Example 4 (Neutral):
Review: "It does what it says. Nothing special."
Sentiment: Neutral
Now classify:
Review: "The design is beautiful but it's overpriced."
Sentiment:Edge Cases
Include tricky examples:
Extract company names from text.
Example 1:
Text: "I work at Microsoft."
Companies: ["Microsoft"]
Example 2:
Text: "The Apple product works with Google services."
Companies: ["Apple", "Google"]
Example 3:
Text: "She ate an apple while working on her resume."
Companies: [] # Note: "apple" here is the fruit, not the company
Example 4:
Text: "Amazon rainforest is disappearing, but Amazon.com is growing."
Companies: ["Amazon.com"] # Only the company referenceFormat Preservation
Models follow the exact format of your examples:
JSON Output
Parse event details into JSON.
Input: "Team standup tomorrow at 9am in the main conference room"
Output: {
"event": "Team standup",
"date": "tomorrow",
"time": "9am",
"location": "main conference room"
}
Input: "Lunch with Sarah next Tuesday at noon"
Output: {
"event": "Lunch with Sarah",
"date": "next Tuesday",
"time": "noon",
"location": null
}
Input: "Board meeting on March 15th, 2pm, Building A Room 401"
Output:Markdown Output
Convert bug reports to markdown tickets.
Input: "Login button doesn't work on mobile Safari. Users can't access their accounts."
Output:
## Bug: Login Button Non-Functional on Mobile Safari
**Severity:** High
**Platform:** iOS Safari
### Description
Users are unable to access their accounts due to the login button being non-functional.
### Steps to Reproduce
1. Open site on iOS Safari
2. Navigate to login page
3. Tap login button
4. Observe: button does not respond
### Expected Behavior
Login button should authenticate user and redirect to dashboard.
---
Input: "Dark mode has wrong colors in settings panel"
Output:Few-Shot for Code Tasks
Code Translation
Convert Python to TypeScript.
Python:
```python
def greet(name: str) -> str:
return f"Hello, {name}!"TypeScript:
function greet(name: string): string {
return \`Hello, \${name}!\`;
}Python:
from typing import List
def sum_evens(numbers: List[int]) -> int:
return sum(n for n in numbers if n % 2 == 0)TypeScript:
function sumEvens(numbers: number[]): number {
return numbers.filter(n => n % 2 === 0).reduce((a, b) => a + b, 0);
}Python:
async def fetch_data(url: str) -> dict:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()TypeScript:
### Error Message Generation
```text
Generate helpful error messages for validation failures.
Input: email field is empty
Output: "Please enter your email address. We'll use this to send you important account updates."
Input: password too short
Output: "Your password needs at least 8 characters. Try adding numbers or symbols to make it stronger."
Input: phone number invalid format
Output:Advanced Techniques
Contrastive Examples
Show what NOT to do:
Write clear variable names.
Bad: x = get_data()
Good: user_profile = get_user_profile_from_database()
Bad: temp = calc(a, b)
Good: total_price = calculate_subtotal(quantity, unit_price)
Bad: res = proc(inp)
Good:Difficulty Progression
Order examples from simple to complex:
Write SQL queries for these natural language requests.
Request: "Get all users"
SQL: SELECT * FROM users;
Request: "Get users who signed up this month"
SQL: SELECT * FROM users WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE);
Request: "Get the top 10 users by order count with their email addresses"
SQL: SELECT u.email, COUNT(o.id) as order_count
FROM users u
JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.email
ORDER BY order_count DESC
LIMIT 10;
Request: "Get monthly revenue trends for the past year, broken down by product category"
SQL:Optimal Number of Examples
| Task Complexity | Recommended Examples |
|---|---|
| Simple format conversion | 1-2 |
| Classification | 3-5 (one per class) |
| Complex reasoning | 3-4 with CoT |
| Domain-specific | 4-6 |
| Highly nuanced | 6-10 |
Common Mistakes
- Inconsistent formatting: Examples use different structures
- Too similar: All examples cover the same case
- Too many: Model gets confused or truncated
- Wrong order: Complex before simple
- No edge cases: Model fails on unusual inputs
- few-shot learning
- in-context learning
- prompt examples
- ai learning
- icl