🎯 Few-Shot Prompting
🌟 Start With a Real-World Scenario
Imagine teaching a friend to classify movie reviews:
Bad way (Zero-shot - No examples):
You: "Classify this review as positive or negative: 'This movie was okay'"
Friend: "Uh... positive? Or maybe neutral? I'm not sure..."
❌ Confused, inconsistent
Good way (Few-shot - With examples):
You: "Here are some examples:
- 'Amazing film!' → Positive
- 'Waste of time' → Negative
- 'Pretty good' → Positive
- 'Terrible acting' → Negative
Now classify: 'This movie was okay'"
Friend: "Hmm, based on the examples, I'd say Positive (like 'Pretty good')"
✅ Clear, consistent!
This is Few-Shot Prompting! 🎯
Real Example with ChatGPT:
Without Examples (Zero-Shot):
Prompt: "Extract the company name from this text:
'Apple announced new iPhone features yesterday'"
ChatGPT: "The company name is Apple Inc."
✅ Works, but...
Prompt: "Extract the company name from this text:
'I love eating apples'"
ChatGPT: "The company name is Apple."
❌ Wrong! It's a fruit, not a company!
With Examples (Few-Shot):
Prompt: "Extract company names from text. Examples:
Text: 'Microsoft released Windows 11'
Company: Microsoft
Text: 'I bought a Tesla yesterday'
Company: Tesla
Text: 'The orange was delicious'
Company: None
Now extract from: 'I love eating apples'
Company:"
ChatGPT: "None"
✅ Correct! It learned from examples!
The Magic of Few-Shot Learning:
GPT-3's Breakthrough (2020):
Before GPT-3, you needed to:
- Collect 10,000+ labeled examples
- Fine-tune a model (expensive!)
- Deploy specialized model
With GPT-3 few-shot:
- Show 3-5 examples in your prompt
- Done! ✅
Example: Translate English to French
Old way (Fine-tuning):
- Collect 100,000 translation pairs
- Train for days
- Cost: $10,000+
Few-shot way:
Prompt: "Translate English to French:
English: Hello
French: Bonjour
English: Thank you
French: Merci
English: Good morning
French: Bon matin
English: How are you?
French:"
GPT: "Comment allez-vous?"
✅ Works instantly! Cost: $0.001
Real-World Applications:
Customer Support Classification:
Examples:
"My order hasn't arrived" → Shipping Issue
"I want a refund" → Refund Request
"How do I use this?" → Product Question
"The product is broken" → ?
AI learns: "Product Issue" ✅
Code Generation:
Examples:
Input: "sort array ascending"
Output: arr.sort()
Input: "remove duplicates"
Output: [...new Set(arr)]
Input: "find maximum value"
Output: Math.max(...arr)
Input: "reverse array"
Output: ?
AI learns: "arr.reverse()" ✅
Email Response Style:
Examples:
Customer: "When will my order ship?"
Response: "Thanks for reaching out! Your order will ship within 24 hours. Tracking info will be sent to your email. 😊"
Customer: "I need help with setup"
Response: "Happy to help! Here's a quick guide: [link]. Let me know if you have questions! 😊"
Customer: "This is broken!"
Response: ?
AI learns your friendly, helpful tone! ✅
The Power Levels:
Zero-Shot (No examples):
"Translate to French: Hello"
- Accuracy: 60-70%
- Works for simple tasks
- Inconsistent
One-Shot (1 example):
"English: Hello → French: Bonjour
English: Thank you → French: ?"
- Accuracy: 75-85%
- Better, but still inconsistent
Few-Shot (3-5 examples):
"English: Hello → French: Bonjour
English: Thank you → French: Merci
English: Good morning → French: Bon matin
English: How are you? → French: ?"
- Accuracy: 85-95%
- Consistent, reliable!
Many-Shot (10+ examples):
[10 examples...]
- Accuracy: 90-98%
- Best quality, but uses more tokens (costs more)
When to Use Few-Shot:
✅ Perfect for:
- Classification tasks (sentiment, category, intent)
- Format conversion (JSON, CSV, structured data)
- Style matching (write like X, respond like Y)
- Pattern recognition (extract entities, dates, numbers)
- Quick prototyping (test ideas fast)
❌ Not ideal for:
- Tasks requiring deep domain knowledge (use fine-tuning)
- Very complex reasoning (use chain-of-thought)
- When you have 10,000+ examples (fine-tune instead)
- Real-time applications (too many tokens = slow + expensive)
By the end of this guide, you'll understand:
- How to craft effective few-shot examples
- The optimal number of examples (hint: 3-5 is the sweet spot!)
- How to choose diverse, representative examples
- When few-shot beats fine-tuning (and vice versa)
- How to structure prompts for maximum effectiveness
- Cost optimization (fewer examples = lower cost)
The secret: Few-shot prompting is the fastest way to customize AI behavior without any training! It's like having a super-smart assistant who learns from just a few examples! 🚀
Fun fact: GPT-3's few-shot ability was so surprising that researchers called it "emergent behavior" - nobody programmed it to learn from examples, it just... did! 🤯
Let's dive in! 🚀
🗺️ Concept Mind Map
FEW-SHOT PROMPTING
|
+-------------------+-------------------+
| | |
LEARNING TYPES COMPONENTS STRATEGIES
| | |
| | |
Zero-Shot Task Description Example Selection
- No examples - Clear goal - Diverse examples
- Just instruction - Output format - Representative
- Works for simple - Constraints samples
tasks | - Balanced classes
| | |
| | |
One-Shot Examples Example Ordering
- 1 example - Input-Output - Easy → Hard
- Shows format pairs - Similar first
- Quick setup - 2-10 examples - Random can work
- Good baseline - Quality matters |
| | |
| | |
Few-Shot Reasoning Dynamic Selection
- 2-10 examples - Chain of - Semantic search
- Best performance Thought - Retrieve similar
- Most common - Step-by-step - Context-aware
- Needs balance - Explanations - Adaptive
| | |
| | |
Many-Shot Output Format Prompt Templates
- 10+ examples - JSON - Reusable patterns
- Diminishing - Markdown - Parameterized
returns - Structured - Version controlled
- Context limits - Consistent
Learning Spectrum:
APPROACH EXAMPLES COST QUALITY WHEN TO USE
Zero-Shot 0 $ ⭐⭐ Simple, general tasks
One-Shot 1 $ ⭐⭐⭐ Show format
Few-Shot 2-10 $$ ⭐⭐⭐⭐ Most tasks
Many-Shot 10+ $$$ ⭐⭐⭐⭐ Complex patterns
Fine-Tuning 1000+ $$$$ ⭐⭐⭐⭐⭐ Production use
Prompt Structure:
[Task Description]
"Classify the sentiment of movie reviews."
[Examples]
Input: "This movie was amazing!"
Output: Positive
Input: "Terrible waste of time."
Output: Negative
Input: "It was okay, nothing special."
Output: Neutral
[New Input]
Input: "Best film I've seen this year!"
Output: ?
Key Principles:
- Quality > Quantity: 3 good examples beat 10 mediocre ones
- Diversity: Cover different cases and edge cases
- Consistency: Same format for all examples
- Clarity: Clear input-output boundaries
zations](#🎮-interactive-tools--visualizations) 3. Fun Facts & Hacks 4. What You'll Learn 5. Core Concept 6. Code Examples 7. Hands-On Exercises 8. Assignments & Practice Problems 9. Common Pitfalls & Debugging 10. Related Concepts 11. Resources 12. Key Takeaways 13. Next Steps
🛠️ Local Setup (5 Minutes)
Set up for experimenting with few-shot prompting:
Step 1: Install Dependencies
pip install openai==1.3.0 \
anthropic==0.7.0 \
python-dotenv==1.0.0
Step 2: Set Up API Keys
# Create .env file
echo "OPENAI_API_KEY=your-key-here" > .env
echo "ANTHROPIC_API_KEY=your-key-here" >> .env
Step 3: Quick Test
from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
# Test few-shot prompting
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{
"role": "user",
"content": """
Translate to French:
Hello → Bonjour
Goodbye → Au revoir
Thank you → Merci
Good morning →
"""
}]
)
print(response.choices[0].message.content)
print("✅ Few-shot prompting working!")
🧠 Core Concept
The Learning Spectrum
Zero-Shot: No examples, just instructions
prompt = "Classify sentiment: I love this product!"
# Model must figure it out from instructions alone
One-Shot: Single example
prompt = """
Example: "Great service!" → Positive
Classify: "I love this product!" →
"""
# Model learns pattern from one example
Few-Shot: Multiple examples (3-5)
prompt = """
"Great service!" → Positive
"Terrible experience" → Negative
"It's okay" → Neutral
Classify: "I love this product!" →
"""
# Model learns robust pattern from multiple examples
Why Few-Shot Works
- Pattern Recognition: Models are trained to recognize patterns
- In-Context Learning: Can adapt behavior based on context
- No Weight Updates: Learns without changing model parameters
- Emergent Ability: Only works at sufficient scale
The Anatomy of a Good Example
# Good Example Structure:
# 1. Clear input
# 2. Clear output
# 3. Consistent format
# 4. Representative of task
example = """
Input: The movie was amazing!
Sentiment: positive
Confidence: high
"""
💻 Code Examples
Example 1: Sentiment Classification (Basic)
from openai import OpenAI
client = OpenAI()
def few_shot_sentiment(text):
"""
Classify sentiment using few-shot prompting.
"""
prompt = f"""
Classify the sentiment of the following texts:
Text: "I absolutely loved this product!"
Sentiment: positive
Text: "Worst purchase ever, very disappointed"
Sentiment: negative
Text: "It's okay, nothing special"
Sentiment: neutral
Text: "{text}"
Sentiment:"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0, # Deterministic
max_tokens=10
)
return response.choices[0].message.content.strip()
# Test
texts = [
"This is amazing!",
"I hate this",
"It's fine"
]
for text in texts:
sentiment = few_shot_sentiment(text)
print(f"'{text}' → {sentiment}")
Output:
'This is amazing!' → positive
'I hate this' → negative
'It's fine' → neutral
Example 2: Data Extraction (Structured Output)
def extract_contact_info(text):
"""
Extract structured contact information using few-shot.
"""
prompt = f"""
Extract contact information in JSON format:
Text: "Contact John Doe at john@email.com or call 555-1234"
Output: {{"name": "John Doe", "email": "john@email.com", "phone": "555-1234"}}
Text: "Reach out to Jane Smith (jane.smith@company.com)"
Output: {{"name": "Jane Smith", "email": "jane.smith@company.com", "phone": null}}
Text: "Call Mike at 555-9876"
Output: {{"name": "Mike", "email": null, "phone": "555-9876"}}
Text: "{text}"
Output:"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
import json
return json.loads(response.choices[0].message.content)
# Test
text = "Email Sarah Johnson at sarah.j@example.com or phone 555-4321"
result = extract_contact_info(text)
print(json.dumps(result, indent=2))
Output:
{
"name": "Sarah Johnson",
"email": "sarah.j@example.com",
"phone": "555-4321"
}
Example 3: Code Generation (Programming Tasks)
def generate_function(description):
"""
Generate Python functions using few-shot examples.
"""
prompt = f"""
Generate Python functions based on descriptions:
Description: Calculate factorial of a number
Code:
```python
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
Description: Check if string is palindrome Code:
def is_palindrome(s):
return s == s[::-1]
Description: Find maximum in list Code:
def find_max(lst):
return max(lst) if lst else None
Description: {description} Code:"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0.3
)
return response.choices[0].message.content
Test
description = "Calculate sum of even numbers in a list" code = generate_function(description) print(code)
---
### Example 4: Translation with Context
```python
def contextual_translate(text, source_lang, target_lang):
"""
Translate with context-aware few-shot examples.
"""
prompt = f"""
Translate from {source_lang} to {target_lang}, preserving tone and context:
{source_lang}: "The bank is closed"
Context: Financial institution
{target_lang}: "La banque est fermée"
{source_lang}: "I sat on the bank"
Context: River side
{target_lang}: "Je me suis assis sur la rive"
{source_lang}: "The mouse is broken"
Context: Computer device
{target_lang}: "La souris est cassée"
{source_lang}: "{text}"
{target_lang}:"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0.3
)
return response.choices[0].message.content.strip()
# Test
text = "The mouse ran across the floor"
translation = contextual_translate(text, "English", "French")
print(f"'{text}' → '{translation}'")
Example 5: Dynamic Few-Shot Selection
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer
import numpy as np
class DynamicFewShotPrompt:
"""
Dynamically select most relevant examples for each query.
"""
def __init__(self, examples):
self.examples = examples
self.model = SentenceTransformer('all-MiniLM-L6-v2')
# Pre-compute embeddings for all examples
self.example_texts = [ex['input'] for ex in examples]
self.embeddings = self.model.encode(self.example_texts)
def select_examples(self, query, k=3):
"""
Select k most similar examples to query.
"""
query_embedding = self.model.encode([query])
similarities = cosine_similarity(query_embedding, self.embeddings)[0]
# Get top k indices
top_k_indices = np.argsort(similarities)[-k:][::-1]
return [self.examples[i] for i in top_k_indices]
def generate_prompt(self, query, k=3):
"""
Generate few-shot prompt with most relevant examples.
"""
selected = self.select_examples(query, k)
prompt = "Classify sentiment:\n\n"
for ex in selected:
prompt += f"Text: \"{ex['input']}\"\n"
prompt += f"Sentiment: {ex['output']}\n\n"
prompt += f"Text: \"{query}\"\nSentiment:"
return prompt
# Example usage
examples = [
{"input": "I love this product!", "output": "positive"},
{"input": "Terrible service", "output": "negative"},
{"input": "It's okay", "output": "neutral"},
{"input": "Best purchase ever!", "output": "positive"},
{"input": "Waste of money", "output": "negative"},
{"input": "Not bad", "output": "neutral"},
]
prompter = DynamicFewShotPrompt(examples)
# Test with new query
query = "This is amazing!"
prompt = prompter.generate_prompt(query, k=3)
print(prompt)
# Use with LLM
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
print(f"\nResult: {response.choices[0].message.content}")
📝 Assignments & Practice Problems
Assignment 1: Email Classifier ⭐
Difficulty: Easy | Time: 30 minutes
Build an email classifier (spam/not spam) using few-shot prompting.
Requirements:
- Create 5 example emails (mix of spam and legitimate)
- Test with 20 new emails
- Achieve >80% accuracy
- Compare with zero-shot baseline
Assignment 2: Code Comment Generator ⭐⭐
Difficulty: Medium | Time: 60 minutes
Generate docstrings for Python functions using few-shot examples.
Requirements:
- Provide 3 example functions with good docstrings
- Generate docstrings for 10 new functions
- Include parameters, return values, and examples
- Validate generated docstrings
Assignment 3: Multi-Label Classifier ⭐⭐⭐
Difficulty: Hard | Time: 90 minutes
Build a classifier that assigns multiple labels to text.
Requirements:
- Create examples with multiple labels
- Handle varying number of labels per input
- Implement confidence scores
- Test with complex cases
❌ Bad: Inconsistent format
examples = """ Input: text1 -> output1 text2: output2 Input text3 = output3 """
✅ Good: Consistent format
examples = """ Input: text1 Output: output1
Input: text2 Output: output2
Input: text3 Output: output3 """
### Pitfall 2: Too Many Examples
```python
# ❌ Bad: 20 examples (expensive, slow)
# ✅ Good: 3-5 examples (optimal)
# Diminishing returns after 5 examples
# More examples = more tokens = higher cost
Pitfall 3: Biased Examples
# ❌ Bad: All positive examples
examples = [
"Great! → positive",
"Excellent! → positive",
"Amazing! → positive"
]
# ✅ Good: Balanced examples
examples = [
"Great! → positive",
"Terrible → negative",
"Okay → neutral"
]
🔗 Related Concepts
- Previous: Large Language Models - Understanding LLMs
- Next: Context Engineering - Advanced prompting
- Related: Chain of Thought - Reasoning with examples
- Advanced: Agents - Using few-shot in agents
📚 Resources
Papers
Guides
Tools
pip install langchain # Few-shot prompt templates
pip install guidance # Structured prompting
