>Couchbase 是一款屡获殊荣的分布式 NoSQL 云数据库 > 提供无与伦比的多功能性、性能、可扩展性和经济效益 > 适用于您的所有云、移动、AI 和边缘计算应用。
如果您想查看详细的使用示例,请参阅 Couchbase 向量存储.
安装和设置
安装 langchain-couchbase 包以及嵌入依赖项:
pip install langchain-couchbase langchain-openai
uv add langchain-couchbase langchain-openai
向量存储
Couchbase 为 LangChain 提供了两种不同的向量存储实现:
| 向量存储 | 索引类型 | 最低版本 | 适用场景 |
|---|---|---|---|
CouchbaseSearchVectorStore | 搜索向量索引 | Couchbase Server 7.6+ | 结合向量相似性与全文搜索(FTS)和地理空间搜索的混合搜索 |
CouchbaseQueryVectorStore | 超大规模向量索引 or 复合向量索引 | Couchbase Server 8.0+ | 大规模纯向量搜索或结合向量相似性与标量过滤器的搜索 |
CouchbaseSearchVectorStore
from langchain_couchbase import CouchbaseSearchVectorStore
from langchain_openai import OpenAIEmbeddings
# Get credentials
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: ")
OPENAI_API_KEY = getpass.getpass("Enter your OpenAI API key: ")
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
# Create Couchbase connection object
from datetime import timedelta
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import ClusterOptions
auth = PasswordAuthenticator(DB_USERNAME, DB_PASSWORD)
options = ClusterOptions(auth)
options.apply_profile("wan_development")
cluster = Cluster(COUCHBASE_CONNECTION_STRING, options)
# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))
# Set up embeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
# Create vector store
vector_store = CouchbaseSearchVectorStore(
cluster=cluster,
bucket_name="my_bucket",
scope_name="_default",
collection_name="_default",
embedding=embeddings,
index_name="my_search_index",
)
# Add documents
texts = ["Couchbase is a NoSQL database", "LangChain is a framework for LLM applications"]
vector_store.add_texts(texts)
# Search
query = "What is Couchbase?"
docs = vector_store.similarity_search(query)
API 参考: CouchbaseSearchVectorStore
CouchbaseQueryVectorStore
from langchain_couchbase import CouchbaseQueryVectorStore
from langchain_openai import OpenAIEmbeddings
# (After setting up cluster connection as shown above)
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
vector_store = CouchbaseQueryVectorStore(
cluster=cluster,
bucket_name="my_bucket",
scope_name="_default",
collection_name="_default",
embedding=embeddings,
index_name="my_vector_index",
)
# Create index (if needed)
vector_store.create_index(
index_type=IndexType.HYPERSCALE,
index_description="IVF,SQ8",
index_name="my_vector_index",
)
# Add documents and search
vector_store.add_documents([
Document(page_content="Couchbase is a NoSQL database", metadata={"source": "couchbase"}),
Document(page_content="LangChain is a framework for LLM applications", metadata={"source": "langchain"}),
])
docs = vector_store.similarity_search("What is Couchbase?")
API 参考: CouchbaseQueryVectorStore
文档加载器
请参阅。
from langchain_community.document_loaders.couchbase import CouchbaseLoader
connection_string = "couchbase://localhost" # valid Couchbase connection string
db_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++ query
query = """
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()
LLM 缓存
CouchbaseCache
使用 Couchbase 作为提示和响应的缓存。
导入此缓存:
from langchain_couchbase.cache import CouchbaseCache
将此缓存与您的 LLM 配合使用:
from langchain_core.globals import set_llm_cache
cluster = couchbase_cluster_connection_object
set_llm_cache(
CouchbaseCache(
cluster=cluster,
bucket_name=BUCKET_NAME,
scope_name=SCOPE_NAME,
collection_name=COLLECTION_NAME,
)
)
API 参考: CouchbaseCache
CouchbaseSemanticCache
语义缓存允许用户根据用户输入与先前缓存的输入之间的语义相似性来检索缓存的提示。它在内部使用 Couchbase 作为缓存和向量存储。 CouchbaseSemanticCache 需要定义一个搜索索引才能工作。请参阅 使用示例 了解如何设置索引。
导入此缓存:
from langchain_couchbase.cache import CouchbaseSemanticCache
将此缓存与您的 LLM 配合使用:
from langchain_core.globals import set_llm_cache
# use any embedding provider...
from langchain_openai.Embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
cluster = couchbase_cluster_connection_object
set_llm_cache(
CouchbaseSemanticCache(
cluster=cluster,
embedding = embeddings,
bucket_name="my_bucket",
scope_name="_default",
collection_name="_default",
index_name="my_search_index",
)
)
API 参考: CouchbaseSemanticCache