Source code for langchain_community.vectorstores.docarray.in_memory
"""Wrapper around in-memory storage."""from__future__importannotationsfromtypingimportAny,Dict,List,Literal,Optionalfromlangchain_core.embeddingsimportEmbeddingsfromlangchain_community.vectorstores.docarray.baseimport(DocArrayIndex,_check_docarray_import,)
[docs]classDocArrayInMemorySearch(DocArrayIndex):"""In-memory `DocArray` storage for exact search. To use it, you should have the ``docarray`` package with version >=0.32.0 installed. You can install it with `pip install docarray`. """
[docs]@classmethoddeffrom_params(cls,embedding:Embeddings,metric:Literal["cosine_sim","euclidian_dist","sgeuclidean_dist"]="cosine_sim",**kwargs:Any,)->DocArrayInMemorySearch:"""Initialize DocArrayInMemorySearch store. Args: embedding (Embeddings): Embedding function. metric (str): metric for exact nearest-neighbor search. Can be one of: "cosine_sim", "euclidean_dist" and "sqeuclidean_dist". Defaults to "cosine_sim". **kwargs: Other keyword arguments to be passed to the get_doc_cls method. """_check_docarray_import()fromdocarray.indeximportInMemoryExactNNIndexdoc_cls=cls._get_doc_cls(space=metric,**kwargs)doc_index=InMemoryExactNNIndex[doc_cls]()# type: ignorereturncls(doc_index,embedding)
[docs]@classmethoddeffrom_texts(cls,texts:List[str],embedding:Embeddings,metadatas:Optional[List[Dict[Any,Any]]]=None,**kwargs:Any,)->DocArrayInMemorySearch:"""Create an DocArrayInMemorySearch store and insert data. Args: texts (List[str]): Text data. embedding (Embeddings): Embedding function. metadatas (Optional[List[Dict[Any, Any]]]): Metadata for each text if it exists. Defaults to None. **kwargs: Other keyword arguments to be passed to the from_params method. Returns: DocArrayInMemorySearch Vector Store """store=cls.from_params(embedding,**kwargs)store.add_texts(texts=texts,metadatas=metadatas)returnstore