Skip to main content

Few-Shot Learning — Teaching by Example

83/221
Chapter 9 Advanced Text Model Techniques — Coding

Few-Shot Learning — Teaching by Example

7 min read Lesson 83 / 221 Preview

Few-Shot Learning — Teaching by Example

Few-shot learning provides the model with example input-output pairs directly in the prompt, enabling it to learn a pattern and apply it to new inputs without any fine-tuning.

Zero-Shot vs. Few-Shot

# Zero-shot: No examples
zero_shot_prompt = "Classify the sentiment: 'This product is amazing!' → "

# Few-shot: With examples
few_shot_prompt = """Classify the sentiment of each review.

Review: "The battery life is incredible, best phone I've owned."
Sentiment: positive

Review: "Broke after two days, complete waste of money."
Sentiment: negative

Review: "It works fine, nothing special."
Sentiment: neutral

Review: "This product is amazing!"
Sentiment: """

Structured Few-Shot Implementation

import openai
from typing import List, Tuple

def few_shot_classify(
    examples: List[Tuple[str, str]],
    query: str,
    task_description: str
) -> str:
    messages = [
        {"role": "system", "content": task_description}
    ]

    # Add examples as user/assistant pairs
    for user_input, expected_output in examples:
        messages.append({"role": "user", "content": user_input})
        messages.append({"role": "assistant", "content": expected_output})

    # Add the actual query
    messages.append({"role": "user", "content": query})

    response = openai.chat.completions.create(
        model="gpt-4o",
        messages=messages,
        temperature=0.0  # Deterministic for classification
    )
    return response.choices[0].message.content

# Usage
examples = [
    ("The food was delicious and the service was fast", "positive"),
    ("Waited 45 minutes and the order was wrong", "negative"),
    ("Average experience, nothing to complain about", "neutral"),
]

result = few_shot_classify(
    examples=examples,
    query="Best restaurant in town, will definitely come back!",
    task_description="You classify restaurant reviews as positive, negative, or neutral."
)
print(result)  # "positive"

Best Practices for Example Selection

  1. Diversity — cover all possible output categories
  2. Balance — equal number of examples per category
  3. Edge cases — include ambiguous examples to teach boundary decisions
  4. Recency — place the most relevant examples closest to the query

Dynamic Example Selection with Embeddings

from openai import OpenAI
import numpy as np

client = OpenAI()

def get_embedding(text: str) -> list:
    response = client.embeddings.create(model="text-embedding-3-small", input=text)
    return response.data[0].embedding

def select_relevant_examples(query: str, example_pool: list, k: int = 3):
    query_emb = np.array(get_embedding(query))
    scored = []
    for ex in example_pool:
        ex_emb = np.array(get_embedding(ex["input"]))
        similarity = np.dot(query_emb, ex_emb)
        scored.append((similarity, ex))
    scored.sort(reverse=True, key=lambda x: x[0])
    return [ex for _, ex in scored[:k]]

Key Takeaways

  • Few-shot examples in the chat message format (user/assistant pairs) are more effective than concatenated text
  • Use dynamic example selection for large example pools
  • Temperature 0.0 is essential for classification tasks requiring consistency