What is Few-shot Prompting?

Few-shot learning is a prompting technique where you provide the AI with one or several small number of examples (typically 1-10) to demonstrate the desired pattern, format, or behavior before asking it to perform a similar task. This technique leverages the AI’s ability to recognize patterns and generalize from limited examples.

Why Use Few-shot Prompting?

  • Improved Accuracy: Examples help the AI understand exactly what you want
  • Consistent Format: Ensures outputs follow a specific structure
  • Reduced Ambiguity: Clear examples eliminate guesswork
  • Better Context Understanding: Shows the AI how to handle edge cases
  • Domain Adaptation: Helps AI adapt to specific domains or styles

Zero-shot vs Few-shot

Zero-shot prompting involves asking the AI to perform a task without any examples, relying solely on its pre-existing knowledge. Few-shot prompting, on the other hand, provides a few examples to guide the AI’s response. Few-shot learning is generally more effective for complex tasks where context and specific patterns are crucial.

One-shot vs Few-shot

One-shot prompting provides a single example to guide the AI, the idea behind one-shot learning is to show the AI how to perform a task with just one example.

All variants of few-shot prompting (zero-shot, one-shot, and few-shot) can be implemented in Latitude. The choice depends on the complexity of the task and the amount of guidance needed.

Basic Implementation in Latitude

Here’s a simple few-shot learning example for email classification:

Email Classification
---
provider: OpenAI
model: gpt-4o
temperature: 0.1
---

# Email Classification Task

You are an expert email classifier. Based on the examples below, classify the given email
into one of these categories: URGENT, SPAM, BUSINESS, PERSONAL.

## Examples:

<user>
**Email**: "CONGRATULATIONS! You've won $1,000,000! Click here now!"
</user>
<assistant>
**Category**: SPAM
</assistant>

<user>
**Email**: "Hi John, can we reschedule tomorrow's meeting? I have a family emergency."
</user>
<assistant>
**Category**: PERSONAL
</assistant>

<user>
**Email**: "SYSTEM ALERT: Server down. Immediate attention required. Revenue impact critical."
</user>
<assistant>
**Category**: URGENT
</assistant>

<user>
**Email**: "Please find attached the Q4 financial report for your review."
</user>
<assistant>
**Category**: BUSINESS
</assistant>

## Email to Classify:
{{ email_content }}

## Category:

Advanced Implementation with Variables

Let’s create a more sophisticated example that uses Latitude’s parameters system:

---
provider: OpenAI
model: gpt-4o
temperature: 0.2
---

# {{ product_category }} Product Review Sentiment Analysis

Analyze the sentiment of {{ product_category }} reviews and extract {{ detail_level }} insights. Use the examples below as a guide.

## Examples:

{{ for example in examples }}
**Review**: "{{ example.text }}"
**Sentiment**: {{ example.sentiment }}
**Key Points**: {{ example.key_points }}
**Confidence**: {{ example.confidence }}
{{ endfor }}

## Review to Analyze:
{{ review_text }}

## Analysis:
**Sentiment**:
**Key Points**:
**Confidence**:

In this advanced example:

  1. Dynamic Content: We use templates ({{ variable }}) to insert parameters into the prompt.

  2. Templating Features: We demonstrate control structures like {{for item in items }} for arrays.

  3. Runtime Examples: The examples array parameter allows users to pass in any number of examples when calling the prompt.

This pattern makes your prompts more flexible and reusable across different use cases without creating separate prompts for each scenario. For more on parameters, see the Latitude Parameter Types documentation.

Multi-step Few-shot with Chains

Latitude’s chain feature allows you to create complex few-shot workflows:

---
provider: OpenAI
model: gpt-4o
temperature: 0.1
---

<step>
# Information Extraction

Extract structured information from the business proposal. Follow these examples:

## Examples:

**Input**: "We propose a mobile app for food delivery in NYC with $50K funding needed."
**Output**:
{
  "business_type": "mobile app",
  "industry": "food delivery",
  "location": "NYC",
  "funding_needed": "$50K"
}

**Input**: "Looking for $2M Series A for our AI-powered healthcare platform serving hospitals nationwide."
```json
{
  "business_type": "AI platform",
  "industry": "healthcare",
  "location": "nationwide",
  "funding_needed": "$2M"
}
```

## Proposal to Extract:
{{ proposal_text }}

## Extracted Information:
</step>

<step>
# Business Viability Analysis

Based on the extracted information, provide a viability score following these examples:

## Examples:

**Business**: AI healthcare platform, $2M Series A, nationwide
**Viability Score**: 8.5/10
**Reasoning**: Large addressable market, proven demand, appropriate funding level

**Business**: Food delivery app, $50K seed, NYC only
**Viability Score**: 6.0/10
**Reasoning**: Competitive market, limited geographic scope, low initial funding

## Analysis:
**Viability Score**:
**Reasoning**:
</step>

Dynamic Few-shot with Conditional Logic

Use Latitude’s conditional features to adapt examples based on context:

---
provider: OpenAI
model: gpt-4o
temperature: 0.3
---

# Content Generation with Context-Aware Examples

Generate {{ content_type }} following the appropriate examples below:

{{ if content_type === "social_media" }}
  <assistant>
  ## Social Media Examples:

  **Topic**: "New restaurant opening"
  **Content**: "🍕 EXCITING NEWS! Our new pizza place opens tomorrow at 123 Main St! First 50 customers get 50% off! #GrandOpening #Pizza #Foodie"

  **Topic**: "Product launch"
  **Content**: "🚀 Introducing our game-changing productivity app! Streamline your workflow like never before. Download now: [link] #ProductLaunch #Productivity #Innovation"
  </assistant>
{{ endif }}

{{ if content_type === "email" }}
  <assistant>
  ## Email Examples:

  **Topic**: "Welcome new customer"
  **Subject**: "Welcome to [Company] - Your journey starts here!"
  **Content**: "Dear [Name], Thank you for choosing [Company]. We're excited to help you achieve your goals. Here's what to expect next..."

  **Topic**: "Product announcement"
  **Subject**: "Introducing [Product] - Transform your [Industry]"
  **Content**: "Hi [Name], We're thrilled to announce [Product], designed specifically for professionals like you who want to [benefit]..."
  </assistant>
{{ endif }}

{{ if content_type === "blog" }}
  <assistant>
  ## Blog Examples:

  **Topic**: "Industry trends"
  **Content**: "# The Future of [Industry]: 5 Trends to Watch in 2024\n\nThe [industry] landscape is evolving rapidly. Here are the key trends shaping our future:\n\n## 1. [Trend Name]\n[Detailed explanation...]"

  **Topic**: "How-to guide"
  **Content**: "# Step-by-Step Guide: How to [Action]\n\nAre you struggling with [problem]? This comprehensive guide will walk you through everything you need to know.\n\n## Prerequisites\n- [Requirement 1]\n- [Requirement 2]"
  </assistant>
{{ endif }}

## Content Request:
**Topic**: {{ topic }}
**Target Audience**: {{ audience }}
**Key Message**: {{ key_message }}

## Generated {{ content_type }}:

Few-shot with Agent Collaboration

Combine few-shot learning with Latitude’s agent system for complex workflows:

---
provider: OpenAI
model: gpt-4o
temperature: 0.2
type: agent
agents:
  - agents/data_extractor
  - agents/validator
  - agents/formatter
---

# Document Processing Pipeline

Process the document using specialized agents, each with their own few-shot examples.

## Document Input:
{{ document_content }}

## Processing Steps:

1. **Data Extraction**: Use the data_extractor agent to pull key information
2. **Validation**: Use the validator agent to verify accuracy
3. **Formatting**: Use the formatter agent to structure output

Process the document and provide the final structured result.

Best Practices for Few-shot Prompting

Advanced Techniques

Self-Improving Few-shot

Create prompts that can improve their own examples:

---
provider: OpenAI
model: gpt-4o
temperature: 0.4
---

<step>
# Process the task with current examples

Current examples:
{{ current_examples }}

Task: {{ user_task }}

Result:
</step>

<step>
# Evaluate the result quality

Result: {{ initial_processing }}

Was this result satisfactory? If not, what examples would improve future performance?

Evaluation:
</step>

<step>
# Generate improved examples if needed

Based on the evaluation. If needs improvement. Generate 2-3 new examples that would help with tasks like: {{ user_task }}
New examples should address the identified weaknesses.

Improved Examples:
</step>

Cross-Domain Transfer

Use few-shot learning to transfer patterns across domains:

---
provider: OpenAI
model: gpt-4o
temperature: 0.3
---

# Cross-Domain Pattern Transfer

Learn the pattern from {{ source_domain }} examples and apply it to {{ target_domain }}.

## Source Domain Examples ({{ source_domain }}):

{{ for example in source_examples }}
**Input**: {{ example.input }}
**Pattern Applied**: {{ example.pattern }}
**Output**: {{ example.output }}
{{ endfor }}

## Pattern Recognition:
The consistent pattern across these examples is: {{ identified_pattern }}

## Target Domain Application ({{ target_domain }}):
Now apply the same pattern to this {{ target_domain }} scenario:

**Input**: {{ target_input }}
**Pattern to Apply**: {{ identified_pattern }}
**Output**:

Common Pitfalls and Solutions

Avoid These Common Mistakes:

  • Too Many Examples: More isn’t always better; 3-7 examples are usually optimal
  • Inconsistent Formatting: Make sure all examples follow the same structure
  • Biased Examples: Include diverse scenarios to avoid model bias
  • Unclear Boundaries: Clearly separate examples from the actual task

Pro Tips:

  • Start with 2-3 examples and add more if needed
  • Test your few-shot prompts with edge cases
  • Use Latitude’s version control to iterate on example sets
  • Combine with other techniques like Chain-of-Thought for complex reasoning

Next Steps

Now that you understand few-shot learning, explore these related techniques: