Source code for langchain_community.vectorstores.vlite
from__future__importannotations# Standard library importsfromtypingimportAny,Dict,Iterable,List,Optional,Tuplefromuuidimportuuid4# LangChain importsfromlangchain_core.documentsimportDocumentfromlangchain_core.embeddingsimportEmbeddingsfromlangchain_core.vectorstoresimportVectorStore
[docs]classVLite(VectorStore):"""VLite is a simple and fast vector database for semantic search."""
[docs]def__init__(self,embedding_function:Embeddings,collection:Optional[str]=None,**kwargs:Any,):super().__init__()self.embedding_function=embedding_functionself.collection=collectionorf"vlite_{uuid4().hex}"# Third-party importstry:fromvliteimportVLiteexceptImportError:raiseImportError("Could not import vlite python package. ""Please install it with `pip install vlite`.")self.vlite=VLite(collection=self.collection,**kwargs)
[docs]defadd_texts(self,texts:Iterable[str],metadatas:Optional[List[dict]]=None,**kwargs:Any,)->List[str]:"""Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. metadatas: Optional list of metadatas associated with the texts. kwargs: vectorstore specific parameters Returns: List of ids from adding the texts into the vectorstore. """texts=list(texts)ids=kwargs.pop("ids",[str(uuid4())for_intexts])embeddings=self.embedding_function.embed_documents(texts)ifnotmetadatas:metadatas=[{}for_intexts]data_points=[{"text":text,"metadata":metadata,"id":id,"embedding":embedding}fortext,metadata,id,embeddinginzip(texts,metadatas,ids,embeddings)]results=self.vlite.add(data_points)return[result[0]forresultinresults]
[docs]defadd_documents(self,documents:List[Document],**kwargs:Any,)->List[str]:"""Add a list of documents to the vectorstore. Args: documents: List of documents to add to the vectorstore. kwargs: vectorstore specific parameters such as "file_path" for processing directly with vlite. Returns: List of ids from adding the documents into the vectorstore. """ids=kwargs.pop("ids",[str(uuid4())for_indocuments])texts=[]metadatas=[]fordoc,idinzip(documents,ids):if"file_path"inkwargs:# Third-party importstry:fromvlite.utilsimportprocess_fileexceptImportError:raiseImportError("Could not import vlite python package. ""Please install it with `pip install vlite`.")processed_data=process_file(kwargs["file_path"])texts.extend(processed_data)metadatas.extend([doc.metadata]*len(processed_data))ids.extend([f"{id}_{i}"foriinrange(len(processed_data))])else:texts.append(doc.page_content)metadatas.append(doc.metadata)returnself.add_texts(texts,metadatas,ids=ids)
[docs]defsimilarity_search(self,query:str,k:int=4,**kwargs:Any,)->List[Document]:"""Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. Returns: List of Documents most similar to the query. """docs_and_scores=self.similarity_search_with_score(query,k=k)return[docfordoc,_indocs_and_scores]
[docs]defsimilarity_search_with_score(self,query:str,k:int=4,filter:Optional[Dict[str,str]]=None,**kwargs:Any,)->List[Tuple[Document,float]]:"""Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: Filter by metadata. Defaults to None. Returns: List of Tuples of (doc, score), where score is the similarity score. """metadata=filteror{}embedding=self.embedding_function.embed_query(query)results=self.vlite.retrieve(text=query,top_k=k,metadata=metadata,return_scores=True,embedding=embedding,)documents_with_scores=[(Document(page_content=text,metadata=metadata),score)fortext,score,metadatainresults]returndocuments_with_scores
[docs]defupdate_document(self,document_id:str,document:Document)->None:"""Update an existing document in the vectorstore."""self.vlite.update(document_id,text=document.page_content,metadata=document.metadata)
[docs]defget(self,ids:List[str])->List[Document]:"""Get documents by their IDs."""results=self.vlite.get(ids)documents=[Document(page_content=text,metadata=metadata)fortext,metadatainresults]returndocuments
[docs]defdelete(self,ids:Optional[List[str]]=None,**kwargs:Any)->Optional[bool]:"""Delete by ids."""ifidsisnotNone:self.vlite.delete(ids,**kwargs)returnTruereturnNone
[docs]@classmethoddeffrom_existing_index(cls,embedding:Embeddings,collection:str,**kwargs:Any,)->VLite:"""Load an existing VLite index. Args: embedding: Embedding function collection: Name of the collection to load. Returns: VLite vector store. """vlite=cls(embedding_function=embedding,collection=collection,**kwargs)returnvlite
[docs]@classmethoddeffrom_texts(cls,texts:List[str],embedding:Embeddings,metadatas:Optional[List[dict]]=None,collection:Optional[str]=None,**kwargs:Any,)->VLite:"""Construct VLite wrapper from raw documents. This is a user-friendly interface that: 1. Embeds documents. 2. Adds the documents to the vectorstore. This is intended to be a quick way to get started. Example: .. code-block:: python from langchain import VLite from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings() vlite = VLite.from_texts(texts, embeddings) """vlite=cls(embedding_function=embedding,collection=collection,**kwargs)vlite.add_texts(texts,metadatas,**kwargs)returnvlite
[docs]@classmethoddeffrom_documents(cls,documents:List[Document],embedding:Embeddings,collection:Optional[str]=None,**kwargs:Any,)->VLite:"""Construct VLite wrapper from a list of documents. This is a user-friendly interface that: 1. Embeds documents. 2. Adds the documents to the vectorstore. This is intended to be a quick way to get started. Example: .. code-block:: python from langchain import VLite from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings() vlite = VLite.from_documents(documents, embeddings) """vlite=cls(embedding_function=embedding,collection=collection,**kwargs)vlite.add_documents(documents,**kwargs)returnvlite