跳到主要内容

Couchbase

Deepseek 3.2 中英对照 Couchbase

本节将引导您设置 CouchbaseSearchVectorStore,以存储文档嵌入并使用 Couchbase 执行相似性搜索。

Couchbase 是一个分布式的 JSON 文档数据库,具备关系型数据库管理系统(DBMS)的所有理想功能。其众多特性之一,是允许用户基于向量存储与检索进行信息查询。

前置条件

一个正在运行的 Couchbase 实例。以下选项可用:Couchbase * Docker * Capella - Couchbase 即服务 * 本地安装 Couchbase * Couchbase Kubernetes Operator

自动配置

备注

Spring AI 的自动配置和 starter 模块的 artifact 名称发生了重大变化。更多信息请参阅 升级说明

Spring AI 为 Couchbase 向量存储提供了 Spring Boot 自动配置。要启用此功能,请将以下依赖项添加到您项目的 Maven pom.xml 文件中:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-couchbase</artifactId>
</dependency>

或将其添加到您的 Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store-spring-boot-starter'
}
备注

Couchbase 向量搜索功能仅从 Couchbase Server 7.6 版本和 Java SDK 3.6.0 版本开始提供。

:::提示
请参考依赖管理章节,将 Spring AI BOM 添加到您的构建文件中。
:::

:::提示
请参考 Artifact Repositories 部分,将 Milestone 和/或 Snapshot 仓库添加到您的构建文件中。
:::

向量存储实现可以使用默认选项为你初始化已配置的存储桶、作用域、集合和搜索索引,但你必须通过在相应构造函数中指定 initializeSchema 布尔参数来选择启用此功能。

备注

这是一个破坏性变更! 在早期版本的 Spring AI 中,默认会自动进行此模式初始化。

请查看向量存储的配置参数列表,了解默认值和配置选项。

此外,你还需要一个配置好的 EmbeddingModel bean。更多信息请参阅 EmbeddingModel 章节。

现在,你可以在应用程序中自动连接 CouchbaseSearchVectorStore 作为向量存储。

@Autowired VectorStore vectorStore;

// ...

List <Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

// Add the documents to Qdrant
vectorStore.add(documents);

// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

配置属性

要连接到 Couchbase 并使用 CouchbaseSearchVectorStore,你需要提供 Couchbase 实例的访问详细信息。配置可以通过 Spring Boot 的 application.properties 文件来提供:

spring.ai.openai.api-key=<key>
spring.couchbase.connection-string=<conn_string>
spring.couchbase.username=<username>
spring.couchbase.password=<password>

如果您希望使用环境变量来存储敏感信息(如密码或 API 密钥),有几种可选方案:

选项一:使用 Spring 表达式语言 (SpEL)

您可以使用自定义环境变量名称,并通过SpEL在应用程序配置中引用它们:

# In application.yml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
couchbase:
connection-string: ${COUCHBASE_CONN_STRING}
username: ${COUCHBASE_USER}
password: ${COUCHBASE_PASSWORD}
# In your environment or .env file
export OPENAI_API_KEY=<api-key>
export COUCHBASE_CONN_STRING=<couchbase connection string like couchbase://localhost>
export COUCHBASE_USER=<couchbase username>
export COUCHBASE_PASSWORD=<couchbase password>

选项二:通过编程方式访问环境变量

或者,你可以在 Java 代码中访问环境变量:

String apiKey = System.getenv("OPENAI_API_KEY");

这种方法让你在命名环境变量时拥有灵活性,同时能将敏感信息排除在应用程序配置文件之外。

备注

如果你选择创建一个 shell 脚本以便于后续工作,请务必在启动应用程序之前通过 "sourcing" 文件来运行它,例如 source <your_script_name>.sh

Spring Boot 为 Couchbase 集群提供的自动配置功能将创建一个 bean 实例,该实例将被 CouchbaseSearchVectorStore 使用。

spring.couchbase.* 开头的 Spring Boot 属性用于配置 Couchbase 集群实例:

属性描述默认值
spring.couchbase.connection-stringCouchbase 连接字符串couchbase://localhost
spring.couchbase.password用于 Couchbase 身份验证的密码。-
spring.couchbase.username用于 Couchbase 身份验证的用户名。-
spring.couchbase.env.io.minEndpoints每个节点的套接字最小数量。1
spring.couchbase.env.io.maxEndpoints每个节点的套接字最大数量。12
spring.couchbase.env.io.idleHttpConnectionTimeoutHTTP 连接在关闭并从连接池移除前可以保持空闲的时间长度。1s
spring.couchbase.env.ssl.enabled是否启用 SSL 支持。如果提供了 "bundle",除非另有指定,否则将自动启用。-
spring.couchbase.env.ssl.bundleSSL bundle 名称。-
spring.couchbase.env.timeouts.connectBucket 连接超时。10s
spring.couchbase.env.timeouts.disconnectBucket 断开连接超时。10s
spring.couchbase.env.timeouts.key-value对特定 key-value 的操作超时。2500ms
spring.couchbase.env.timeouts.key-value对具有持久性级别的特定 key-value 的操作超时。10s
spring.couchbase.env.timeouts.key-value-durable对具有持久性级别的特定 key-value 的操作超时。10s
spring.couchbase.env.timeouts.querySQL++ 查询操作超时。75s
spring.couchbase.env.timeouts.view常规和地理空间视图操作超时。75s
spring.couchbase.env.timeouts.search搜索服务超时。75s
spring.couchbase.env.timeouts.analytics分析服务超时。75s
spring.couchbase.env.timeouts.management管理操作超时。75s

spring.ai.vectorstore.couchbase.* 开头的属性用于配置 CouchbaseSearchVectorStore

属性描述默认值
spring.ai.vectorstore.couchbase.index-name存储向量的索引名称。spring-ai-document-index
spring.ai.vectorstore.couchbase.bucket-nameCouchbase Bucket 的名称,是 scope 的父级。default
spring.ai.vectorstore.couchbase.scope-nameCouchbase scope 的名称,是 collection 的父级。搜索查询将在 scope 的上下文中执行。default
spring.ai.vectorstore.couchbase.collection-name用于存储 Document 的 Couchbase collection 名称。default
spring.ai.vectorstore.couchbase.dimensions向量中的维度数量。1536
spring.ai.vectorstore.couchbase.similarity要使用的相似性函数。dot_product
spring.ai.vectorstore.couchbase.optimization要使用的相似性函数。recall
spring.ai.vectorstore.couchbase.initialize-schema是否初始化所需的 schemafalse

以下相似度函数可供使用:

  • l2_norm

  • dot_product

以下索引优化功能可供使用:

  • 召回率

  • 延迟

更多详细信息,请参阅Couchbase文档中关于向量搜索的部分。

Metadata Filtering

你可以利用通用的、可移植的元数据过滤器与 Couchbase 存储一起使用。

例如,您可以使用文本表达式语言:

vectorStore.similaritySearch(
SearchRequest.defaults()
.query("The World")
.topK(TOP_K)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

或通过 Filter.Expression DSL 以编程方式实现:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
.query("The World")
.topK(TOP_K)
.filterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()));
备注

这些过滤表达式会被转换成等价的 Couchbase SQL++ 过滤条件。

手动配置

除了使用Spring Boot的自动配置,你也可以手动配置Couchbase向量存储。为此,你需要将spring-ai-couchbase-store添加到你的项目中:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-couchbase-store</artifactId>
</dependency>

或将其添加到您的Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store'
}

创建一个 Couchbase Cluster bean。如需深入了解自定义 Cluster 实例的配置,请参阅 Couchbase 文档

@Bean
public Cluster cluster() {
return Cluster.connect("couchbase://localhost", "username", "password");
}

然后使用构建器模式创建 CouchbaseSearchVectorStore bean:

@Bean
public VectorStore couchbaseSearchVectorStore(Cluster cluster,
EmbeddingModel embeddingModel,
Boolean initializeSchema) {
return CouchbaseSearchVectorStore
.builder(cluster, embeddingModel)
.bucketName("test")
.scopeName("test")
.collectionName("test")
.initializeSchema(initializeSchema)
.build();
}

// This can be any EmbeddingModel implementation.
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(this.openaiKey).build());
}

限制条件

备注

必须激活以下 Couchbase 服务:Data、Query、Index、Search。虽然 Data 和 Search 可能就足够了,但 Query 和 Index 是支持完整元数据过滤机制所必需的。