|
此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3! |
Couchbase
本节将引导您设置 CouchbaseSearchVectorStore 以存储文档嵌入并使用 Couchbase 执行相似性搜索。
Couchbase 是一个分布式的 JSON 文档数据库,具有关系型数据库的所有所需功能。除了其他特性外,它还允许用户使用基于向量的存储和检索来查询信息。
前置条件
一个正在运行的 Couchbase 实例。以下是可用选项: Couchbase * Docker * Capella - Couchbase 服务 * 本地安装 Couchbase * Couchbase Kubernetes 操作符
Auto-configuration
|
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 向量搜索仅在 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,您需要为您的实例提供访问详细信息。
可以通过 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 密钥等敏感信息,您有多种选择:
选项 1:使用 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>
选项 2:以编程方式访问环境变量
或者,您可以在 Java 代码中访问环境变量:
String apiKey = System.getenv("OPENAI_API_KEY");
这种方法在命名环境变量时为您提供了灵活性,同时将敏感信息排除在应用程序配置文件之外。
如果您选择创建一个 shell 脚本以便于未来的工作,请确保在启动应用程序之前通过“source”运行该文件,即 source <your_script_name>.sh。 |
Spring Boot 针对 Couchbase 集群的自动配置功能将创建一个 bean 实例,该实例将被 CouchbaseSearchVectorStore 使用。
以 spring.couchbase.* 开头的 Spring Boot 属性用于配置 Couchbase 集群实例:
| <property> </property> | <description> </description> | 默认值 |
|---|---|---|
|
Couchbase 连接字符串 |
|
|
用于与 Couchbase 进行身份验证的密码。 |
- |
|
用于与 Couchbase 进行身份验证的用户名。 |
- |
|
每个节点的最小套接字数量。 |
1 |
|
每个节点的最大套接字数量。 |
12 |
|
HTTP 连接在被关闭并从连接池中移除之前,可以保持空闲的最长时间。 |
1s |
|
是否启用 SSL 支持。如果提供了“bundle”,除非另有指定,否则将自动启用。 |
- |
|
SSL 证书包名称。 |
- |
|
存储桶连接超时。 |
10s |
|
存储桶断开连接超时。 |
10s |
|
针对特定键值对操作的超时时间。 |
2500ms |
|
针对具有持久性级别的特定键值操作的超时时间。 |
10s |
|
针对具有持久性级别的特定键值操作的超时时间。 |
10s |
|
SQL++ 查询操作超时。 |
75s |
|
常规视图操作和地理空间视图操作超时。 |
75s |
|
搜索服务的超时时间。 |
75s |
|
分析服务的超时时间。 |
75s |
|
管理操作的超时时间。 |
75s |
以 spring.ai.vectorstore.couchbase.* 前缀开头的属性用于配置 CouchbaseSearchVectorStore。
| <property> </property> | <description> </description> | 默认值 |
|---|---|---|
|
存储向量的索引名称。 |
spring-ai-document-index |
|
Couchbase Bucket 的名称,该 scope 的父级。 |
默认 |
|
Couchbase 范围的名称,该集合的父级。搜索查询将在该范围的上下文中执行。 |
默认 |
|
用于存储文档的 Couchbase 集合的名称。 |
默认 |
|
向量中的维度数量。 |
1536 |
|
要使用的相似性函数。 |
|
|
要使用的相似性函数。 |
|
|
是否初始化所需的模式 |
|
以下相似性函数可用:
-
l2_norm
-
dot_product
以下索引优化可用:
-
召回
-
延迟
有关向量的详细信息,请参阅 Couchbase 文档 中的向量搜索部分。
元数据过滤
您可以在 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。
阅读 Couchbase 文档 以获取有关自定义 Cluster 实例配置的更深入信息。
@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 是支持完整元数据过滤机制所必需的。 |