主题
Couchbase
本节引导你设置 CouchbaseSearchVectorStore,以使用 Couchbase 存储文档嵌入并进行相似度搜索。
Couchbase 是一个分布式、JSON 文档数据库,具备关系型 DBMS 的所有期望能力。除其他特性外,它允许用户使用基于向量的存储与检索来查询信息。
前置条件
一个正在运行的 Couchbase 实例。有以下可选方案: Couchbase
自动配置
NOTE
Spring AI 的自动配置、starter 模块的制品名称发生了重大变更。 更多信息请参阅 升级说明。
Spring AI 为 Couchbase 向量库提供 Spring Boot 自动配置。 要启用它,请将以下依赖添加到你的项目的 Maven pom.xml 文件:
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-couchbase</artifactId>
</dependency>或添加到你的 Gradle build.gradle 构建文件。
groovy
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-couchbase'
}NOTE
Couchbase 向量搜索需要 Couchbase Server 7.6 或更高版本,以及 Java SDK 3.6.0 或更高版本。
TIP
参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建文件。
TIP
参见 制品仓库 一节,把 Milestone 和/或 Snapshot 仓库添加到你的构建文件。
向量库实现可以在默认选项下为你初始化所配置的 bucket、scope、collection 和搜索索引,但你必须显式开启,方法是在相应的构造器中指定 initializeSchema 布尔值。
NOTE
这是一处破坏性变更!在较早的 Spring AI 版本中,此模式初始化默认进行。
请查看该向量库的配置参数 列表,了解默认值和配置选项。
此外,你还需要一个已配置的 EmbeddingModel bean。更多信息参见 EmbeddingModel 一节。
现在你可以在应用中把 CouchbaseSearchVectorStore 作为向量库自动装配。
java
@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 Couchbase
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());配置属性
要连接到 Couchbase 并使用 CouchbaseSearchVectorStore,你需要提供实例的访问详情。 配置可以通过 Spring Boot 的 application.properties 提供:
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 在应用配置中引用它们:
yaml
# In application.yml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
couchbase:
connection-string: ${COUCHBASE_CONN_STRING}
username: ${COUCHBASE_USER}
password: ${COUCHBASE_PASSWORD}bash
# 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 代码中访问环境变量:
java
String apiKey = System.getenv("OPENAI_API_KEY");这种方式让你在命名环境变量时拥有灵活性,同时把敏感信息留在应用配置文件之外。
NOTE
如果你选择创建一个 shell 脚本以便后续工作,务必在启动应用之前通过 "source" 该文件来运行它,即 source <your_script_name>.sh。
Couchbase 集群的 Spring Boot 自动配置特性会创建一个供 CouchbaseSearchVectorStore 使用的 bean 实例。
以 spring.couchbase.* 开头的 Spring Boot 属性用于配置 Couchbase 集群实例:
| 属性 |
|---|
| 描述 |
| 默认值 |
spring.couchbase.connection-string |
| 一个 couchbase 连接字符串 |
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.idleHttpConnectionTimeout |
| 一个 HTTP 连接在被关闭并从池中移除前可保持空闲的时长。 |
| 1s |
spring.couchbase.env.ssl.enabled |
| 是否启用 SSL 支持。若提供了 "bundle" 则自动启用,除非另有指定。 |
| - |
spring.couchbase.env.ssl.bundle |
| SSL bundle 名称。 |
| - |
spring.couchbase.env.timeouts.connect |
| Bucket 连接超时。 |
| 10s |
spring.couchbase.env.timeouts.disconnect |
| Bucket 断开连接超时。 |
| 10s |
spring.couchbase.env.timeouts.key-value |
| 针对特定键值操作的超时。 |
| 2500ms |
spring.couchbase.env.timeouts.key-value |
| 针对带有持久级别的特定键值操作的超时。 |
| 10s |
spring.couchbase.env.timeouts.key-value-durable |
| 针对带有持久级别的特定键值操作的超时。 |
| 10s |
spring.couchbase.env.timeouts.query |
| SQL++ 查询操作超时。 |
| 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-name |
| Couchbase Bucket 的名称,scope 的父级。 |
| default |
spring.ai.vectorstore.couchbase.scope-name |
| Couchbase scope 的名称,collection 的父级。搜索查询将在 scope 上下文中执行。 |
| default |
spring.ai.vectorstore.couchbase.collection-name |
| 用于存储文档的 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 |
| 是否初始化所需的模式 |
false |
以下相似度函数可用:
- l2_norm
- dot_product
以下索引优化选项可用:
- recall
- latency
关于每一种的更多细节,参见 Couchbase 文档 中关于向量搜索的部分。
元数据过滤
你可以在 Couchbase 向量库上利用通用、可移植的元数据过滤器。
例如,你可以使用文本表达式语言:
java
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'")
.build());或以编程方式使用 Filter.Expression DSL:
java
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.filterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build())
.build());NOTE
这些过滤表达式会被转换为等价的 Couchbase SQL++ 过滤器。
手动配置
与其使用 Spring Boot 自动配置,不如手动配置 Couchbase 向量库。为此你需要把 spring-ai-couchbase-store 添加到你的项目:
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-couchbase-store</artifactId>
</dependency>或添加到你的 Gradle build.gradle 构建文件。
groovy
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store'
}创建一个 Couchbase Cluster bean。 要更深入地了解自定义 Cluster 实例的配置,请阅读 Couchbase 文档。
java
@Bean
public Cluster cluster() {
return Cluster.connect("couchbase://localhost", "username", "password");
}然后使用构建器模式创建 CouchbaseSearchVectorStore bean:
java
@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 OpenAiEmbeddingModel.builder()
.options(OpenAiEmbeddingOptions.builder().apiKey(this.openaiKey).build())
.build();
}限制
NOTE
必须激活以下 Couchbase 服务:Data、Query、Index、Search。虽然 Data 和 Search 可能就够用,但 Query 和 Index 对于支持完整的元数据过滤机制是必需的。