dspy.KNN
dspy.KNN(k: int, trainset: list, vectorizer=None)
A k-nearest neighbors retriever that finds similar examples from a training set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k
|
int
|
Number of nearest neighbors to retrieve |
required |
trainset
|
list
|
List of training examples to search through |
required |
vectorizer
|
Optional dspy.Embedder for computing embeddings. If None, uses sentence-transformers. |
None
|
Example
trainset = [dsp.Example(input="hello", output="world"), ...] knn = KNN(k=3, trainset=trainset) similar_examples = knn(input="hello")