In statistics, the k-nearest neighbors algorithm (k-NN) is a non-parametric supervised learning method first developed by Evelyn Fix and Joseph Hodges in 1951, and later expanded by Thomas Cover. It is used for classification and regression.

This notebook goes over how to use a retriever that under the hood uses an kNN.

Largely based on

from langchain.retrievers import KNNRetriever
from langchain.embeddings import 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.get_relevant_documents("foo")
[Document(page_content='foo', metadata={}),
 Document(page_content='foo bar', metadata={}),
 Document(page_content='hello', metadata={}),
 Document(page_content='bar', metadata={})]