[docs]classCouchbaseVectorStore(VectorStore):"""__ModuleName__ vector store integration. Setup: Install ``langchain-couchbase`` and head over to the Couchbase [website](https://cloud.couchbase.com) and create a new connection, with a bucket, collection, and search index. .. code-block:: bash pip install -U langchain-couchbase .. code-block:: python import getpass COUCHBASE_CONNECTION_STRING = getpass.getpass("Enter the connection string for the Couchbase cluster: ") DB_USERNAME = getpass.getpass("Enter the username for the Couchbase cluster: ") DB_PASSWORD = getpass.getpass("Enter the password for the Couchbase cluster: ") Key init args — indexing params: embedding: Embeddings Embedding function to use. Key init args — client params: cluster: Cluster Couchbase cluster object with active connection. bucket_name: str Name of the bucket to store documents in. scope_name: str Name of the scope in the bucket to store documents in. collection_name: str Name of the collection in the scope to store documents in. index_name: str Name of the Search index to use. Instantiate: .. code-block:: python from datetime import timedelta from langchain_openai import OpenAIEmbeddings from couchbase.auth import PasswordAuthenticator from couchbase.cluster import Cluster from couchbase.options import ClusterOptions auth = PasswordAuthenticator(DB_USERNAME, DB_PASSWORD) options = ClusterOptions(auth) cluster = Cluster(COUCHBASE_CONNECTION_STRING, options) # Wait until the cluster is ready for use. cluster.wait_until_ready(timedelta(seconds=5)) BUCKET_NAME = "langchain_bucket" SCOPE_NAME = "_default" COLLECTION_NAME = "default" SEARCH_INDEX_NAME = "langchain-test-index" vector_store = CouchbaseVectorStore( cluster=cluster, bucket_name=BUCKET_NAME, scope_name=SCOPE_NAME, collection_name=COLLECTION_NAME, embedding=embeddings, index_name=SEARCH_INDEX_NAME, ) Add Documents: .. code-block:: python from langchain_core.documents import Document document_1 = Document(page_content="foo", metadata={"baz": "bar"}) document_2 = Document(page_content="thud", metadata={"bar": "baz"}) document_3 = Document(page_content="i will be deleted :(") documents = [document_1, document_2, document_3] ids = ["1", "2", "3"] vector_store.add_documents(documents=documents, ids=ids) Delete Documents: .. code-block:: python vector_store.delete(ids=["3"]) # TODO: Fill out with example output. Search: .. code-block:: python results = vector_store.similarity_search(query="thud",k=1) for doc in results: print(f"* {doc.page_content} [{doc.metadata}]") .. code-block:: python # TODO: Example output # TODO: Fill out with relevant variables and example output. Search with filter: .. code-block:: python # TODO: Update filter to correct format results = vector_store.similarity_search(query="thud",k=1,filter={"bar": "baz"}) for doc in results: print(f"* {doc.page_content} [{doc.metadata}]") .. code-block:: python # TODO: Example output # TODO: Fill out with example output. Search with score: .. code-block:: python results = vector_store.similarity_search_with_score(query="qux",k=1) for doc, score in results: print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]") .. code-block:: python # TODO: Example output # TODO: Fill out with example output. Async: .. code-block:: python # add documents # await vector_store.aadd_documents(documents=documents, ids=ids) # delete documents # await vector_store.adelete(ids=["3"]) # search # results = vector_store.asimilarity_search(query="thud",k=1) # search with score results = await vector_store.asimilarity_search_with_score(query="qux",k=1) for doc,score in results: print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]") .. code-block:: python # TODO: Example output # TODO: Fill out with example output. Use as Retriever: .. code-block:: python retriever = vector_store.as_retriever( search_type="mmr", search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5}, ) retriever.invoke("thud") .. code-block:: python # TODO: Example output """# noqa: E501# Default batch sizeDEFAULT_BATCH_SIZE=100_metadata_key="metadata"_default_text_key="text"_default_embedding_key="embedding"def_check_bucket_exists(self)->bool:"""Check if the bucket exists in the linked Couchbase cluster"""bucket_manager=self._cluster.buckets()try:bucket_manager.get_bucket(self._bucket_name)returnTrueexceptException:returnFalsedef_check_scope_and_collection_exists(self)->bool:"""Check if the scope and collection exists in the linked Couchbase bucket Raises a ValueError if either is not found"""scope_collection_map:Dict[str,Any]={}# Get a list of all scopes in the bucketforscopeinself._bucket.collections().get_all_scopes():scope_collection_map[scope.name]=[]# Get a list of all the collections in the scopeforcollectioninscope.collections:scope_collection_map[scope.name].append(collection.name)# Check if the scope existsifself._scope_namenotinscope_collection_map.keys():raiseValueError(f"Scope {self._scope_name} not found in Couchbase "f"bucket {self._bucket_name}")# Check if the collection exists in the scopeifself._collection_namenotinscope_collection_map[self._scope_name]:raiseValueError(f"Collection {self._collection_name} not found in scope "f"{self._scope_name} in Couchbase bucket {self._bucket_name}")returnTruedef_check_index_exists(self)->bool:"""Check if the Search index exists in the linked Couchbase cluster Raises a ValueError if the index does not exist"""ifself._scoped_index:all_indexes=[index.nameforindexinself._scope.search_indexes().get_all_indexes()]ifself._index_namenotinall_indexes:raiseValueError(f"Index {self._index_name} does not exist. "" Please create the index before searching.")else:all_indexes=[index.nameforindexinself._cluster.search_indexes().get_all_indexes()]ifself._index_namenotinall_indexes:raiseValueError(f"Index {self._index_name} does not exist. "" Please create the index before searching.")returnTrue
[docs]def__init__(self,cluster:Cluster,bucket_name:str,scope_name:str,collection_name:str,embedding:Embeddings,index_name:str,*,text_key:Optional[str]=_default_text_key,embedding_key:Optional[str]=_default_embedding_key,scoped_index:bool=True,)->None:""" Initialize the Couchbase Vector Store. Args: cluster (Cluster): couchbase cluster object with active connection. bucket_name (str): name of bucket to store documents in. scope_name (str): name of scope in the bucket to store documents in. collection_name (str): name of collection in the scope to store documents in embedding (Embeddings): embedding function to use. index_name (str): name of the Search index to use. text_key (optional[str]): key in document to use as text. Set to text by default. embedding_key (optional[str]): key in document to use for the embeddings. Set to embedding by default. scoped_index (optional[bool]): specify whether the index is a scoped index. Set to True by default. """ifnotisinstance(cluster,Cluster):raiseValueError(f"cluster should be an instance of couchbase.Cluster, "f"got {type(cluster)}")self._cluster=clusterifnotembedding:raiseValueError("Embeddings instance must be provided.")ifnotbucket_name:raiseValueError("bucket_name must be provided.")ifnotscope_name:raiseValueError("scope_name must be provided.")ifnotcollection_name:raiseValueError("collection_name must be provided.")ifnotindex_name:raiseValueError("index_name must be provided.")self._bucket_name=bucket_nameself._scope_name=scope_nameself._collection_name=collection_nameself._embedding_function=embeddingself._text_key=text_keyself._embedding_key=embedding_keyself._index_name=index_nameself._scoped_index=scoped_index# Check if the bucket existsifnotself._check_bucket_exists():raiseValueError(f"Bucket {self._bucket_name} does not exist. "" Please create the bucket before searching.")try:self._bucket=self._cluster.bucket(self._bucket_name)self._scope=self._bucket.scope(self._scope_name)self._collection=self._scope.collection(self._collection_name)exceptExceptionase:raiseValueError("Error connecting to couchbase. ""Please check the connection and credentials.")frome# Check if the scope and collection exists. Throws ValueError if they don'ttry:self._check_scope_and_collection_exists()exceptExceptionase:raisee# Check if the index exists. Throws ValueError if it doesn'ttry:self._check_index_exists()exceptExceptionase:raisee
[docs]defadd_texts(self,texts:Iterable[str],metadatas:Optional[List[dict]]=None,ids:Optional[List[str]]=None,batch_size:Optional[int]=None,**kwargs:Any,)->List[str]:"""Run texts through the embeddings and persist in vectorstore. If the document IDs are passed, the existing documents (if any) will be overwritten with the new ones. Args: texts (Iterable[str]): Iterable of strings to add to the vectorstore. metadatas (Optional[List[Dict]]): Optional list of metadatas associated with the texts. ids (Optional[List[str]]): Optional list of ids associated with the texts. IDs have to be unique strings across the collection. If it is not specified uuids are generated and used as ids. batch_size (Optional[int]): Optional batch size for bulk insertions. Default is 100. Returns: List[str]:List of ids from adding the texts into the vectorstore. """ifnotbatch_size:batch_size=self.DEFAULT_BATCH_SIZEdoc_ids:List[str]=[]ifidsisNone:ids=[uuid.uuid4().hexfor_intexts]ifmetadatasisNone:metadatas=[{}for_intexts]embedded_texts=self._embedding_function.embed_documents(list(texts))documents_to_insert=[{id:{self._text_key:text,self._embedding_key:vector,self._metadata_key:metadata,}forid,text,vector,metadatainzip(ids,texts,embedded_texts,metadatas)}]# Insert in batchesforiinrange(0,len(documents_to_insert),batch_size):batch=documents_to_insert[i:i+batch_size]try:result=self._collection.upsert_multi(batch[0])ifresult.all_ok:doc_ids.extend(batch[0].keys())exceptDocumentExistsExceptionase:raiseValueError(f"Document already exists: {e}")returndoc_ids
[docs]defdelete(self,ids:Optional[List[str]]=None,**kwargs:Any)->Optional[bool]:"""Delete documents from the vector store by ids. Args: ids (List[str]): List of IDs of the documents to delete. batch_size (Optional[int]): Optional batch size for bulk deletions. Returns: bool: True if all the documents were deleted successfully, False otherwise. """ifidsisNone:raiseValueError("No document ids provided to delete.")batch_size=kwargs.get("batch_size",self.DEFAULT_BATCH_SIZE)deletion_status=True# Delete in batchesforiinrange(0,len(ids),batch_size):batch=ids[i:i+batch_size]try:result=self._collection.remove_multi(batch)exceptDocumentNotFoundExceptionase:deletion_status=FalseraiseValueError(f"Document not found: {e}")deletion_status&=result.all_okreturndeletion_status
@propertydefembeddings(self)->Embeddings:"""Return the query embedding object."""returnself._embedding_functiondef_format_metadata(self,row_fields:Dict[str,Any])->Dict[str,Any]:"""Helper method to format the metadata from the Couchbase Search API. Args: row_fields (Dict[str, Any]): The fields to format. Returns: Dict[str, Any]: The formatted metadata. """metadata={}forkey,valueinrow_fields.items():# Couchbase Search returns the metadata key with a prefix# `metadata.` We remove it to get the original metadata keyifkey.startswith(self._metadata_key):new_key=key.split(self._metadata_key+".")[-1]metadata[new_key]=valueelse:metadata[key]=valuereturnmetadata
[docs]defsimilarity_search(self,query:str,k:int=4,search_options:Optional[Dict[str,Any]]={},**kwargs:Any,)->List[Document]:"""Return documents most similar to embedding vector with their scores. Args: query (str): Query to look up for similar documents k (int): Number of Documents to return. Defaults to 4. search_options (Optional[Dict[str, Any]]): Optional search options that are passed to Couchbase search. Defaults to empty dictionary fields (Optional[List[str]]): Optional list of fields to include in the metadata of results. Note that these need to be stored in the index. If nothing is specified, defaults to all the fields stored in the index. Returns: List of Documents most similar to the query. """query_embedding=self.embeddings.embed_query(query)docs_with_scores=self.similarity_search_with_score_by_vector(query_embedding,k,search_options,**kwargs)return[docfordoc,_indocs_with_scores]
[docs]defsimilarity_search_with_score_by_vector(self,embedding:List[float],k:int=4,search_options:Optional[Dict[str,Any]]={},**kwargs:Any,)->List[Tuple[Document,float]]:"""Return docs most similar to embedding vector with their scores. Args: embedding (List[float]): Embedding vector to look up documents similar to. k (int): Number of Documents to return. Defaults to 4. search_options (Optional[Dict[str, Any]]): Optional search options that are passed to Couchbase search. Defaults to empty dictionary. fields (Optional[List[str]]): Optional list of fields to include in the metadata of results. Note that these need to be stored in the index. If nothing is specified, defaults to all the fields stored in the index. Returns: List of (Document, score) that are the most similar to the query vector. """fields=kwargs.get("fields",["*"])# Document text field needs to be returned from the searchiffields!=["*"]andself._text_keynotinfields:fields.append(self._text_key)search_req=search.SearchRequest.create(VectorSearch.from_vector_query(VectorQuery(self._embedding_key,embedding,k,)))try:ifself._scoped_index:search_iter=self._scope.search(self._index_name,search_req,SearchOptions(limit=k,fields=fields,raw=search_options,),)else:search_iter=self._cluster.search(self._index_name,search_req,SearchOptions(limit=k,fields=fields,raw=search_options),)docs_with_score=[]# Parse the resultsforrowinsearch_iter.rows():text=row.fields.pop(self._text_key,"")# Format the metadata from Couchbasemetadata=self._format_metadata(row.fields)score=row.scoredoc=Document(page_content=text,metadata=metadata)docs_with_score.append((doc,score))exceptExceptionase:raiseValueError(f"Search failed with error: {e}")returndocs_with_score
[docs]defsimilarity_search_with_score(self,query:str,k:int=4,search_options:Optional[Dict[str,Any]]={},**kwargs:Any,)->List[Tuple[Document,float]]:"""Return documents that are most similar to the query with their scores. Args: query (str): Query to look up for similar documents k (int): Number of Documents to return. Defaults to 4. search_options (Optional[Dict[str, Any]]): Optional search options that are passed to Couchbase search. Defaults to empty dictionary. fields (Optional[List[str]]): Optional list of fields to include in the metadata of results. Note that these need to be stored in the index. If nothing is specified, defaults to text and metadata fields. Returns: List of (Document, score) that are most similar to the query. """query_embedding=self.embeddings.embed_query(query)docs_with_score=self.similarity_search_with_score_by_vector(query_embedding,k,search_options,**kwargs)returndocs_with_score
[docs]defsimilarity_search_by_vector(self,embedding:List[float],k:int=4,search_options:Optional[Dict[str,Any]]={},**kwargs:Any,)->List[Document]:"""Return documents that are most similar to the vector embedding. Args: embedding (List[float]): Embedding to look up documents similar to. k (int): Number of Documents to return. Defaults to 4. search_options (Optional[Dict[str, Any]]): Optional search options that are passed to Couchbase search. Defaults to empty dictionary. fields (Optional[List[str]]): Optional list of fields to include in the metadata of results. Note that these need to be stored in the index. If nothing is specified, defaults to document text and metadata fields. Returns: List of Documents most similar to the query. """docs_with_score=self.similarity_search_with_score_by_vector(embedding,k,search_options,**kwargs)return[docfordoc,_indocs_with_score]
@classmethoddef_from_kwargs(cls:Type[CouchbaseVectorStore],embedding:Embeddings,**kwargs:Any,)->CouchbaseVectorStore:"""Initialize the Couchbase vector store from keyword arguments for the vector store. Args: embedding: Embedding object to use to embed text. **kwargs: Keyword arguments to initialize the vector store with. Accepted arguments are: - cluster - bucket_name - scope_name - collection_name - index_name - text_key - embedding_key - scoped_index """cluster=kwargs.get("cluster",None)bucket_name=kwargs.get("bucket_name",None)scope_name=kwargs.get("scope_name",None)collection_name=kwargs.get("collection_name",None)index_name=kwargs.get("index_name",None)text_key=kwargs.get("text_key",cls._default_text_key)embedding_key=kwargs.get("embedding_key",cls._default_embedding_key)scoped_index=kwargs.get("scoped_index",True)returncls(embedding=embedding,cluster=cluster,bucket_name=bucket_name,scope_name=scope_name,collection_name=collection_name,index_name=index_name,text_key=text_key,embedding_key=embedding_key,scoped_index=scoped_index,)
[docs]@classmethoddeffrom_texts(cls:Type[CouchbaseVectorStore],texts:List[str],embedding:Embeddings,metadatas:Optional[List[dict]]=None,**kwargs:Any,)->CouchbaseVectorStore:"""Construct a Couchbase vector store from a list of texts. Example: .. code-block:: python from langchain_couchbase import CouchbaseVectorStore from langchain_openai import OpenAIEmbeddings from couchbase.cluster import Cluster from couchbase.auth import PasswordAuthenticator from couchbase.options import ClusterOptions from datetime import timedelta auth = PasswordAuthenticator(username, password) options = ClusterOptions(auth) connect_string = "couchbases://localhost" cluster = Cluster(connect_string, options) # Wait until the cluster is ready for use. cluster.wait_until_ready(timedelta(seconds=5)) embeddings = OpenAIEmbeddings() texts = ["hello", "world"] vectorstore = CouchbaseVectorStore.from_texts( texts, embedding=embeddings, cluster=cluster, bucket_name="", scope_name="", collection_name="", index_name="vector-index", ) Args: texts (List[str]): list of texts to add to the vector store. embedding (Embeddings): embedding function to use. metadatas (optional[List[Dict]): list of metadatas to add to documents. **kwargs: Keyword arguments used to initialize the vector store with and/or passed to `add_texts` method. Check the constructor and/or `add_texts` for the list of accepted arguments. Returns: A Couchbase vector store. """vector_store=cls._from_kwargs(embedding,**kwargs)batch_size=kwargs.get("batch_size",vector_store.DEFAULT_BATCH_SIZE)ids=kwargs.get("ids",None)vector_store.add_texts(texts,metadatas=metadatas,ids=ids,batch_size=batch_size)returnvector_store