Beginner7 min readPrompting Techniques

October 10, 2024

Provide examples to guide AI responses. Perfect for consistent formatting and specific outputs.

Few-Shot Prompting

Few-shot prompting is a technique where you provide the AI with a few examples of the task you want it to perform before asking it to complete a new instance. This helps the model understand the pattern, format, and style you're looking for.

What is Few-Shot Prompting?

Instead of just describing what you want, you show the AI a few examples. This is like teaching by demonstration—the model learns from your examples and applies the same pattern to new inputs.

Example:

Prompt:
"Classify these customer reviews as positive, negative, or neutral:

Review: 'The product exceeded my expectations!'
Sentiment: Positive

Review: 'Terrible quality, broke after one use.'
Sentiment: Negative

Review: 'It's okay, nothing special.'
Sentiment: Neutral

Review: 'Amazing customer service and fast shipping!'
Sentiment: "

Response: "Positive"

Why Use Few-Shot Prompting?

Consistency

Examples ensure the AI follows your exact format and style every time.

Clarity

Showing is often clearer than telling. Examples eliminate ambiguity.

Quality

The model better understands nuanced requirements when given examples.

Flexibility

Works for complex tasks that are hard to describe in words alone.

How It Works

The Pattern

  1. Provide examples (typically 2-5)
  2. Show input-output pairs
  3. Present the new input
  4. Let the AI complete the pattern

Example Structure

[Example 1 Input] → [Example 1 Output]
[Example 2 Input] → [Example 2 Output]
[Example 3 Input] → [Example 3 Output]
[New Input] → [AI generates output]

Practical Applications

Data Formatting

Prompt:
"Convert names to email format:

Name: John Smith
Email: john.smith@company.com

Name: Mary Johnson
Email: mary.johnson@company.com

Name: Robert Williams
Email: "

Response: "robert.williams@company.com"

Text Classification

Prompt:
"Categorize these support tickets:

Ticket: 'My password isn't working'
Category: Account Access

Ticket: 'The app keeps crashing'
Category: Technical Issue

Ticket: 'How do I cancel my subscription?'
Category: Billing

Ticket: 'I can't find the export button'
Category: "

Response: "Technical Issue"

Content Generation

Prompt:
"Write product descriptions in this style:

Product: Wireless Mouse
Description: Ergonomic wireless mouse with precision tracking. Features 6 programmable buttons and 30-hour battery life. Perfect for productivity and gaming.

Product: Mechanical Keyboard
Description: Premium mechanical keyboard with customizable RGB lighting. Cherry MX switches provide satisfying tactile feedback. Built to last with aircraft-grade aluminum frame.

Product: USB-C Hub
Description: "

Response: "Versatile USB-C hub with 7-in-1 connectivity. Supports 4K display output, fast charging, and high-speed data transfer. Compact design perfect for travel and home office."

Best Practices

1. Choose Quality Examples

  • Use diverse but representative examples
  • Show edge cases if relevant
  • Ensure examples are correct and well-formatted

2. Optimal Number of Examples

  • 2-3 examples: Usually sufficient for simple tasks
  • 4-5 examples: Better for complex or nuanced tasks
  • More than 5: Rarely needed, may hit token limits

3. Maintain Consistency

  • Keep formatting identical across examples
  • Use the same structure for all input-output pairs
  • Be consistent with terminology and style

4. Show, Don't Just Tell

Less Effective:

"Format dates as MM/DD/YYYY"

More Effective:

"2024-01-15 → 01/15/2024
2024-03-22 → 03/22/2024
2024-12-05 → "

Advanced Techniques

Chain-of-Thought Few-Shot

Combine few-shot with reasoning steps:

Problem: If Sarah has 5 apples and gives 2 to Tom, how many does she have?
Solution: Sarah starts with 5 apples. She gives away 2 apples. 5 - 2 = 3 apples remaining.
Answer: 3

Problem: A store sells shirts for $15 each. If you buy 3, how much do you pay?
Solution: Each shirt costs $15. Buying 3 shirts means 3 × $15 = $45 total.
Answer: $45

Problem: A recipe needs 2 cups of flour. If you want to make 4 batches, how much flour do you need?
Solution: "

Role-Based Few-Shot

As a professional editor, improve these sentences:

Original: "The thing was very big and looked nice."
Improved: "The sculpture was imposing and aesthetically striking."

Original: "We should do the meeting soon maybe."
Improved: "We should schedule the meeting at your earliest convenience."

Original: "The report has lots of good info in it."
Improved: "

Common Pitfalls

Inconsistent Examples

Bad:

Input: "hello" → Output: "HELLO"
Input: "world" → Output: "World"
Input: "test" → Output: ?

Good:

Input: "hello" → Output: "HELLO"
Input: "world" → Output: "WORLD"
Input: "test" → Output: ?

Too Many Examples

  • Wastes tokens
  • Increases cost
  • May confuse the model
  • Slower processing

Poor Example Selection

  • Examples too similar
  • Missing edge cases
  • Incorrect demonstrations

When to Use Few-Shot vs Zero-Shot

Use Few-Shot When:

  • You need consistent formatting
  • The task is unusual or specific
  • Quality and accuracy are critical
  • You have clear examples to provide

Use Zero-Shot When:

  • The task is simple and common
  • You need quick results
  • Examples are hard to create
  • The task is self-explanatory

Real-World Examples

Email Classification

Email: "URGENT: Your account will be suspended"
Type: Spam

Email: "Meeting notes from today's standup"
Type: Work

Email: "Your Amazon order has shipped"
Type: Notification

Email: "Congratulations! You've won a prize"
Type: "

Sentiment Analysis

Review: "Best purchase I've made all year!"
Score: 5/5

Review: "Decent product but overpriced"
Score: 3/5

Review: "Completely useless, waste of money"
Score: 1/5

Review: "Works as expected, no complaints"
Score: "

Code Generation

Task: "reverse a string"
Code: return string[::-1]

Task: "check if number is even"
Code: return num % 2 == 0

Task: "find maximum in list"
Code: "

Tips for Success

  1. Start with 2-3 examples and add more if needed
  2. Test your examples to ensure they're correct
  3. Use diverse examples that cover different scenarios
  4. Keep formatting consistent across all examples
  5. Update examples based on results

Conclusion

Few-shot prompting is one of the most powerful and practical techniques in your AI toolkit. By providing clear examples, you can achieve consistent, high-quality results across a wide range of tasks.

The key is choosing good examples that clearly demonstrate the pattern you want the AI to follow. Start simple, iterate based on results, and you'll quickly master this essential technique.