Dynamic Few-Shot: Retrieval-Based Examples
What you'll learn: How to automatically select the most relevant few-shot examples for each query instead of using the same fixed examples for all inputs.
The Problem with Static Examples
In traditional few-shot prompting, you pick a handful of example input-output pairs and use them for every query. But what if your query is about medical terminology and your fixed examples are about cooking? The mismatch reduces effectiveness.
Core Idea: Query-Driven Example Selection
Dynamic few-shot treats your collection of examples as a database. For each new query, you:
- Measure similarity between the query and all available examples
- Retrieve the top-k most similar examples
- Build the prompt using only those relevant examples
Think of it like a librarian who doesn't hand you the same three books every time—instead, they look at your question and fetch the most relevant books from the entire library.
Why This Works Better
When examples semantically match your query, the model gets more useful context. A question about "canceling subscriptions" benefits from examples about "refund policies" and "account management," not random examples about weather forecasts.
The retrieval step typically uses:
- Embedding similarity (cosine similarity between query and example embeddings)
- BM25 or other keyword matching
- Hybrid approaches combining both
Practical Advantage
Dynamic few-shot scales better than static prompting. You can maintain a large pool of diverse examples and let the retrieval system automatically find the best 3-5 for each specific input, adapting to varied use cases without manual prompt engineering per query type.
Key Takeaway: Dynamic few-shot uses similarity-based retrieval to automatically select the most relevant examples for each query, making prompts more contextually appropriate than static few-shot approaches.