For a flight-query intent classifier, examples similar to the current utterance may be more useful than a fixed set of demonstrations. K-nearest neighbors selects the k most similar training utterances for each language-model call, so the prompt can change with the input. section 5.5
A DSPy KNN optimizer stores and encodes the training set during compilation without calling the language model. It chooses demonstrations when an input arrives. Because each set only needs to suit that input, fewer examples may be enough. section 5.5
Listing 5.12 KNNFewShot optimizer
import dspy
from dspy import KNNFewShot
from sentence_transformers import SentenceTransformer
encoder_func = SentenceTransformer(
"sentence-transformers/all-MiniLM-L12-v2",
token=False).encode
knn_optimizer = KNNFewShot(
k=3,
trainset=train_set,
vectorizer=dspy.Embedder(encoder_func)
)
optimized_module = knn_optimizer.compile(dspy.Predict(ClosedIntentSignature))
prediction = optimized_module(**dev_set[0].with_inputs())
print(lm.inspect_history(n=1))
The Python listing configures a sentence-transformer encoder, sets k to three, and compiles a DSPy predictor with KNNFewShot. section 5.5


