Dynamic Few-Shot Selection
Retrieving examples per query.
From Static to Dynamic Demos
Static few-shot uses the same examples for every query. Dynamic few-shot retrieves the most relevant demonstrations per query from a pool, conditioning the model on examples that resemble the current input.
This is retrieval-augmented in-context learning: it raises the relevance of demonstrations, which is one of the strongest levers for ICL quality, especially on heterogeneous traffic.
class DynamicSelector:
def __init__(self, pool, embedder, index):
self.pool = pool # candidate demonstrations
self.embed = embedder
self.index = index # ANN index over pool embeddings
def select(self, query, k):
q = self.embed(query)
ids = self.index.search(q, k)
return [self.pool[i] for i in ids]Embedding the Demonstration Pool
Precompute embeddings for every candidate demonstration and store them in an approximate nearest neighbor index (FAISS, HNSW, or a vector DB). At query time, embed the input once and retrieve the top matches.
Choose an embedding model aligned with your task semantics; a generic embedder may cluster on surface features rather than the dimension that predicts the correct label.
import numpy as np
def build_index(pool, embed):
vecs = np.stack([embed(d.input) for d in pool]).astype('float32')
vecs /= np.linalg.norm(vecs, axis=1, keepdims=True) # cosine via dot
index = HNSW(dim=vecs.shape[1])
index.add(vecs)
return indexAll lessons in this course
- Zero, One, and Few-Shot
- Designing Effective Examples
- Example Ordering and Recency
- Dynamic Few-Shot Selection