Couchbase is an award-winning distributed NoSQL cloud database
that delivers unmatched versatility, performance, scalability, and financial value
for all of your cloud, mobile, AI, and edge computing applications.
from langchain_couchbase import CouchbaseSearchVectorStoreimport getpass# Constants for the connectionCOUCHBASE_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: ")# Create Couchbase connection objectfrom datetime import timedeltafrom couchbase.auth import PasswordAuthenticatorfrom couchbase.cluster import Clusterfrom couchbase.options import ClusterOptionsauth = 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))vector_store = CouchbaseSearchVectorStore( cluster=cluster, bucket_name=BUCKET_NAME, scope_name=SCOPE_NAME, collection_name=COLLECTION_NAME, embedding=my_embeddings, index_name=SEARCH_INDEX_NAME,)# Add documentstexts = ["Couchbase is a NoSQL database", "LangChain is a framework for LLM applications"]vectorstore.add_texts(texts)# Searchquery = "What is Couchbase?"docs = vectorstore.similarity_search(query)
from langchain_community.document_loaders.couchbase import CouchbaseLoaderconnection_string = "couchbase://localhost" # valid Couchbase connection stringdb_username = ( "Administrator" # valid database user with read access to the bucket being queried)db_password = "Password" # password for the database user# query is a valid SQL++ queryquery = """ SELECT h.* FROM `travel-sample`.inventory.hotel h WHERE h.country = 'United States' LIMIT 1 """loader = CouchbaseLoader( connection_string, db_username, db_password, query,)docs = loader.load()
Semantic caching allows users to retrieve cached prompts based on the semantic similarity between the user input and previously cached inputs. Under the hood it uses Couchbase as both a cache and a vectorstore.
The CouchbaseSemanticCache needs a Search Index defined to work. Please look at the usage example on how to set up the index.See a usage example.To import this cache:
Copy
Ask AI
from langchain_couchbase.cache import CouchbaseSemanticCache
To use this cache with your LLMs:
Copy
Ask AI
from langchain_core.globals import set_llm_cache# use any embedding provider...from langchain_openai.Embeddings import OpenAIEmbeddingsembeddings = OpenAIEmbeddings()cluster = couchbase_cluster_connection_objectset_llm_cache( CouchbaseSemanticCache( cluster=cluster, embedding = embeddings, bucket_name=BUCKET_NAME, scope_name=SCOPE_NAME, collection_name=COLLECTION_NAME, index_name=INDEX_NAME, ))