Faithful port of DSPy 3.2.1 KNNFewShot (dspy/teleprompt/knn_fewshot.py).
Construction embeds the trainset once through the required :vectorizer
(via Imp.Predict.KNN, the DSPy KNN port). The compiled program then, on
EVERY forward call, retrieves the k nearest trainset examples for that
call's inputs and runs a full Imp.Optimizer.BootstrapFewShot compilation
of the student over those neighbors — metric/teacher-driven bootstrapped
demonstrations, not raw attached neighbors — before executing the compiled
student on the inputs (upstream KNNFewShot.compile's patched
forward_pass).
few_shot_bootstrap_args maps DSPy's **few_shot_bootstrap_args onto
Imp.Optimizer.BootstrapFewShot.new/2: pass :metric plus any
BootstrapFewShot options (:max_bootstrapped_demos, :max_labeled_demos,
:max_rounds, ...). A teacher passes to compile/3 as teacher:, mirroring
upstream's compile(student, teacher=...).
Successful predictions are annotated with the retrieved neighbors under
prediction.metadata.knn_few_shot (an Imp observability extension; the
prompt-visible behavior is upstream's).
Example
knn_few_shot =
Imp.Optimizer.KNNFewShot.new(3, trainset,
vectorizer: Imp.Embeddings.BagOfWords,
few_shot_bootstrap_args: [metric: metric]
)
program = Imp.Optimizer.KNNFewShot.compile(knn_few_shot, student)
Summary
Functions
Returns the compiled program (DSPy KNNFewShot.compile(student, teacher=...)):
per-call neighbor retrieval + BootstrapFewShot compilation of the student.
Builds the optimizer (DSPy KNNFewShot.__init__): a KNN retriever over
the trainset plus stored BootstrapFewShot arguments.
Functions
Returns the compiled program (DSPy KNNFewShot.compile(student, teacher=...)):
per-call neighbor retrieval + BootstrapFewShot compilation of the student.
Builds the optimizer (DSPy KNNFewShot.__init__): a KNN retriever over
the trainset plus stored BootstrapFewShot arguments.