Skip to main content
Couchbase 是一款屡获殊荣的分布式 NoSQL 云数据库, 为您的所有云、移动、AI 和边缘计算应用提供无与伦比的通用性、性能、可扩展性和性价比。
如需查看详细使用示例,请参阅 Couchbase 向量存储

安装与设置

安装 langchain-couchbase 包及嵌入依赖项:
pip install langchain-couchbase langchain-openai

向量存储

Couchbase 为 LangChain 提供了两种不同的向量存储实现:
向量存储索引类型最低版本最适合
CouchbaseSearchVectorStoreSearch 向量索引Couchbase Server 7.6+结合向量相似性与全文搜索(FTS)和地理空间搜索的混合搜索
CouchbaseQueryVectorStore超大规模向量索引复合向量索引Couchbase Server 8.0+大规模纯向量搜索或结合向量相似性与标量过滤器的搜索

CouchbaseSearchVectorStore

from langchain_couchbase import CouchbaseSearchVectorStore
from langchain_openai import OpenAIEmbeddings

import getpass
import os

# 获取凭据
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

# 创建 Couchbase 连接对象
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)

# 等待集群就绪
cluster.wait_until_ready(timedelta(seconds=5))

# 设置嵌入
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")

# 创建向量存储
vector_store = CouchbaseSearchVectorStore(
    cluster=cluster,
    bucket_name="my_bucket",
    scope_name="_default",
    collection_name="_default",
    embedding=embeddings,
    index_name="my_search_index",
)

# 添加文档
texts = ["Couchbase is a NoSQL database", "LangChain is a framework for LLM applications"]
vector_store.add_texts(texts)

# 搜索
query = "What is Couchbase?"
docs = vector_store.similarity_search(query)
API 参考:CouchbaseSearchVectorStore

CouchbaseQueryVectorStore

from langchain_couchbase import CouchbaseQueryVectorStore
from langchain_openai import OpenAIEmbeddings

# (按上述方式设置集群连接后)

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",
)

# 创建索引(如需要)
vector_store.create_index(
    index_type=IndexType.HYPERSCALE,
    index_description="IVF,SQ8",
    index_name="my_vector_index",
)

# 添加文档并搜索
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"  # 有效的 Couchbase 连接字符串
db_username = (
    "Administrator"  # 具有被查询 bucket 读权限的有效数据库用户
)
db_password = "Password"  # 数据库用户的密码

# query 是一个有效的 SQL++ 查询
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 需要定义一个 Search 索引才能工作。请参阅使用示例了解如何设置索引。 导入此缓存:
from langchain_couchbase.cache import CouchbaseSemanticCache
在 LLM 中使用此缓存:
from langchain_core.globals import set_llm_cache

# 使用任意嵌入提供商...
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