kNN
In statistics, the k-nearest neighbours algorithm (k-NN) is a non-parametric supervised learning method first developed by
Evelyn Fix
andJoseph Hodges
in 1951, and later expanded byThomas Cover
. It is used for classification and regression.
This notebook goes over how to use a retriever that under the hood uses a kNN.
Largely based on the code of Andrej Karpathy.
from langchain_community.retrievers import KNNRetriever
from langchain_openai import OpenAIEmbeddings
API Reference:KNNRetriever | OpenAIEmbeddings
Create New Retriever with Textsโ
retriever = KNNRetriever.from_texts(
["foo", "bar", "world", "hello", "foo bar"], OpenAIEmbeddings()
)
Use Retrieverโ
We can now use the retriever!
result = retriever.invoke("foo")
result
[Document(page_content='foo', metadata={}),
Document(page_content='foo bar', metadata={}),
Document(page_content='hello', metadata={}),
Document(page_content='bar', metadata={})]
Relatedโ
- Retriever conceptual guide
- Retriever how-to guides