Back to Lexicon

Few-Shot Learning

novice

Providing a few examples in the prompt to demonstrate the desired behavior. Models learn the pattern from examples without fine-tuning.

Category: techniques
promptinglearning

Overview

Few-shot learning is one of the most powerful techniques for getting LLMs to do exactly what you want. Instead of just describing the task, you show the model examples of correct input-output pairs. The model recognizes the pattern and applies it to new inputs. This works because LLMs are exceptional pattern matchers. When they see "Input: X, Output: Y" repeated a few times, they infer the underlying transformation and apply it consistently. It's like teaching by example rather than teaching by explanation. The "few" in few-shot typically means 2-5 examples. More examples generally improve consistency, but you're trading off against context window space. The key is choosing diverse, representative examples that cover edge cases.

Key Concepts

In-Context Learning

The model learns from examples provided in the prompt, without any weight updates. This is temporary learning that exists only for that conversation.

Example Selection

Choosing examples that are diverse, representative, and cover edge cases. Poor examples lead to poor generalization.

Format Consistency

Using a consistent format across all examples helps the model recognize the pattern more reliably.

Code Examples

Sentiment Classificationtext
Classify the sentiment as positive, negative, or neutral.

Text: The movie was absolutely fantastic!
Sentiment: positive

Text: I waited two hours and the food was cold.
Sentiment: negative

Text: The package arrived on Tuesday.
Sentiment: neutral

Text: This product exceeded all my expectations!
Sentiment:

Three examples establish the pattern. The model will likely respond "positive" for the final input.

Data Extractiontext
Extract the company name and role from job postings.

Posting: "Senior Engineer wanted at Google for our AI team"
Company: Google
Role: Senior Engineer

Posting: "Anthropic is hiring ML researchers"
Company: Anthropic
Role: ML Researcher

Posting: "Join OpenAI as a Product Manager"
Company: OpenAI
Role: Product Manager

Posting: "Staff Software Engineer position at Stripe"

Examples show the exact output format. The model will extract in the same structure.

Few-Shot with APIpython
from openai import OpenAI
client = OpenAI()

few_shot_prompt = """Convert natural language to SQL.

Question: How many users signed up last month?
SQL: SELECT COUNT(*) FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)

Question: What are the top 5 products by revenue?
SQL: SELECT product_name, SUM(revenue) as total FROM sales GROUP BY product_name ORDER BY total DESC LIMIT 5

Question: {user_question}
SQL:"""

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{
        "role": "user",
        "content": few_shot_prompt.format(
            user_question="Show me all orders over $100"
        )
    }]
)

Examples in the prompt teach SQL patterns. The model generates consistent SQL for new questions.

Real-World Use Cases

  • 1Text classification with custom categories
  • 2Data extraction and formatting
  • 3Code translation between languages
  • 4Custom text transformations
  • 5Domain-specific Q&A

Practical Tips

  • Start with 3 examples and add more if consistency is low
  • Include at least one edge case in your examples
  • Use clear delimiters between examples (---, ###, or blank lines)
  • Order examples from simple to complex
  • Test with inputs that are different from your examples

Common Mistakes to Avoid

  • Using too few examples for complex tasks
  • Examples that are too similar (missing edge cases)
  • Inconsistent formatting between examples
  • Examples that don't match the actual use case distribution
  • Not testing with adversarial inputs

Related Concepts