Source code for langchain_community.retrievers.knn
"""KNN Retriever.Largely based onhttps://github.com/karpathy/randomfun/blob/master/knn_vs_svm.ipynb"""from__future__importannotationsimportconcurrent.futuresfromtypingimportAny,Iterable,List,Optionalimportnumpyasnpfromlangchain_core.callbacksimportCallbackManagerForRetrieverRunfromlangchain_core.documentsimportDocumentfromlangchain_core.embeddingsimportEmbeddingsfromlangchain_core.retrieversimportBaseRetriever
[docs]defcreate_index(contexts:List[str],embeddings:Embeddings)->np.ndarray:""" Create an index of embeddings for a list of contexts. Args: contexts: List of contexts to embed. embeddings: Embeddings model to use. Returns: Index of embeddings. """withconcurrent.futures.ThreadPoolExecutor()asexecutor:returnnp.array(list(executor.map(embeddings.embed_query,contexts)))
[docs]classKNNRetriever(BaseRetriever):"""`KNN` retriever."""embeddings:Embeddings"""Embeddings model to use."""index:Any"""Index of embeddings."""texts:List[str]"""List of texts to index."""metadatas:Optional[List[dict]]=None"""List of metadatas corresponding with each text."""k:int=4"""Number of results to return."""relevancy_threshold:Optional[float]=None"""Threshold for relevancy."""classConfig:arbitrary_types_allowed=True