Couchbase
本部分将引导您完成设置CouchbaseSearchVectorStore
来存储文档嵌入并使用 Couchbase 执行相似性搜索。
Couchbase 是一个分布式 JSON 文档数据库,具有关系 DBMS 所需的所有功能。除其他功能外,它还允许用户使用基于向量的存储和检索来查询信息。
先决条件
一个正在运行的 Couchbase 实例。以下选项可用: Couchbase * Docker * Capella - Couchbase 即服务 * 在本地安装 Couchbase * Couchbase Kubernetes Operator
自动配置
Spring AI 为 Couchbase Vector Store 提供了 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-couchbase-store-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store-spring-boot-starter'
}
Couchbase Vector 搜索仅在 7.6 版和 Java SDK 3.6.0 版中可用” |
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
请参阅 Repositories 部分,将 Milestone 和/或 Snapshot Repositories 添加到您的构建文件中。 |
vector store 实现可以使用默认选项为您初始化配置的 bucket、scope、collection 和 search index,但您必须通过指定initializeSchema
boolean 的值。
这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。 |
请查看 vector store 的配置参数列表,了解默认值和配置选项。
此外,您还需要配置一个EmbeddingModel
豆。请参阅 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>
环境变量 /
export SPRING_COUCHBASE_CONNECTION_STRINGS=<couchbase connection string like couchbase://localhost>
export SPRING_COUCHBASE_USERNAME=<couchbase username>
export SPRING_COUCHBASE_PASSWORD=<couchbase password>
# API key if needed, e.g. OpenAI
export SPRING_AI_OPENAI_API_KEY=<api-key>
也可以是这些的混合。
例如,如果您想将密码存储为环境变量,但将其余部分保留为普通application.yml
文件。
如果您选择创建 shell 脚本以方便将来的工作,请确保在启动应用程序之前通过“源”文件运行它,即source <your_script_name>.sh . |
Spring Boot 对 Couchbase Cluster 的自动配置功能将创建一个 bean 实例,该实例将由CouchbaseSearchVectorStore
.
Spring Boot 属性以spring.couchbase.*
用于配置 Couchbase 集群实例:
财产 | 描述 | 默认值 |
---|---|---|
|
一个 couchbase 连接字符串 |
|
|
用于使用 Couchbase 进行身份验证的密码。 |
- |
|
用于使用 Couchbase 进行身份验证的用户名。 |
- |
|
每个节点的最小套接字数。 |
1 |
|
每个节点的最大套接字数。 |
12 |
|
HTTP 连接在关闭并从池中删除之前可能保持空闲状态的时间长度。 |
1 秒 |
|
是否启用 SSL 支持。如果提供了 “bundle”,则自动启用,除非另有说明。 |
- |
|
SSL 捆绑包名称。 |
- |
|
Bucket connect 超时。 |
10 秒 |
|
存储桶断开连接超时。 |
10 秒 |
|
对特定键值执行的作超时。 |
2500 毫秒 |
|
对具有持久性级别的特定键值执行的作的超时。 |
10 秒 |
|
对具有持久性级别的特定键值执行的作的超时。 |
10 秒 |
|
SQL++ 查询作超时。 |
75 年代 |
|
常规和地理空间视图作超时。 |
75 年代 |
|
搜索服务的超时。 |
75 年代 |
|
分析服务的超时。 |
75 年代 |
|
管理作的超时。 |
75 年代 |
以spring.ai.vectorstore.couchbase.*
prefix 用于配置CouchbaseSearchVectorStore
.
财产 | 描述 | 默认值 |
---|---|---|
|
用于存储向量的索引的名称。 |
spring-ai-文档索引 |
|
Couchbase Bucket 的名称,范围的父级。 |
违约 |
|
Couchbase 范围的名称,集合的父级。搜索查询将在 scope 上下文中执行。 |
违约 |
|
用于存储 Documents 的 Couchbase 集合的名称。 |
违约 |
|
向量中的维数。 |
1536 |
|
要使用的 similarity 函数。 |
|
|
要使用的 similarity 函数。 |
|
|
是否初始化所需的 Schema |
|
可以使用以下相似性函数:
-
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++ 筛选条件。 |
手动配置
您可以手动配置 Couchbase 向量存储,而不是使用 Spring Boot 自动配置。为此,您需要添加spring-ai-couchbase-store
到您的项目:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-couchbase-store</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store'
}
创建 CouchbaseCluster
豆。
阅读 Couchbase 文档,了解有关自定义 Cluster 实例配置的更深入信息。
@Bean
public Cluster cluster() {
Cluster cluster = Cluster.connect("couchbase://localhost",
"username", "password");
}
,然后创建CouchbaseSearchVectorStore
使用 Builder 模式的 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 对于支持完整的元数据筛选机制是必需的。 |