GemFire 向量存储
本节将指导您配置 GemFireVectorStore,以存储文档嵌入并执行相似性搜索。
GemFire 是一种分布式、内存中的键值存储,能够以极快的速度执行读写操作。它提供高可用的并行消息队列、持续可用性,以及一种无需停机即可动态扩展的事件驱动架构。随着数据规模需求的增长以支持高性能实时应用,GemFire 可以轻松实现线性扩展。
GemFire VectorDB 扩展了 GemFire 的功能,作为多功能的向量数据库,高效地存储、检索和执行向量相似度搜索。
前提条件
-
启用了 GemFire VectorDB 扩展的 GemFire 集群
-
一个
EmbeddingModelBean,用于计算文档嵌入。更多信息请参见 EmbeddingModel 部分。可在本地机器上运行的选项包括 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。
自动配置
Spring AI 自动配置(auto-configuration)和 starter 模块的构件名称发生了重大变更。详细信息请参考升级说明。
在您的项目 Maven 构建文件 pom.xml 中添加 GemFire VectorStore Spring Boot starter:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-gemfire</artifactId>
</dependency>
或在您的 Gradle build.gradle 文件中
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-gemfire'
}
配置属性
您可以在 Spring Boot 配置中使用以下属性来进一步配置 GemFireVectorStore。
| 属性 | 默认值 |
|---|---|
spring.ai.vectorstore.gemfire.host | localhost |
spring.ai.vectorstore.gemfire.port | 8080 |
spring.ai.vectorstore.gemfire.initialize-schema | false |
spring.ai.vectorstore.gemfire.index-name | spring-ai-gemfire-store |
spring.ai.vectorstore.gemfire.beam-width | 100 |
spring.ai.vectorstore.gemfire.max-connections | 16 |
spring.ai.vectorstore.gemfire.vector-similarity-function | COSINE |
spring.ai.vectorstore.gemfire.fields | [] |
spring.ai.vectorstore.gemfire.buckets | 0 |
spring.ai.vectorstore.gemfire.username | null |
spring.ai.vectorstore.gemfire.password | null |
spring.ai.vectorstore.gemfire.token | null |
手动配置
若只想使用 GemFireVectorStore 而不启用 Spring Boot 的自动配置,请在项目的 Maven pom.xml 中添加以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store</artifactId>
</dependency>
对于 Gradle 用户,请将以下内容添加到 build.gradle 文件的依赖项(dependencies)块中,以仅使用 GemFireVectorStore:
dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store'
}
使用方法
以下是一个示例,用于创建 GemfireVectorStore 实例,而不是使用 AutoConfiguration
@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.username("my-user-name")
.password("my-password")
.indexName("my-vector-index")
.fields(new String[] {"country", "year", "activationDate"}) // Optional: fields for metadata filtering
.initializeSchema(true)
.build();
}
默认配置连接到 localhost:8080 的 GemFire 集群
- 在您的应用中创建几个文档:
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)),
new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023)));
- 将文档添加到向量存储中:
vectorStore.add(documents);
- 以及使用相似度搜索检索文档:
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Spring").topK(5).build());
您应检索包含文本 “Spring AI rocks!!” 的文档。
你也可以使用相似度阈值来限制结果的数量:
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Spring").topK(5)
.similarityThreshold(0.5d).build());
元数据过滤
你也可以利用通用且可移植的元数据过滤器与 GemFire VectorStore 配合使用。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression("country == 'BG' && year >= 2020").build());
或者使用 Filter.Expression DSL 以编程方式定义:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression(b.and(
b.eq("country", "BG"),
b.gte("year", 2020)).build()).build());
这些(可移植的)过滤表达式会自动转换为专有的 GemFire VectorDB 查询格式。
例如,以下是一个可移植的过滤表达式:
country == 'BG' && year >= 2020
会被转换为专有的 GemFire VectorDB 过滤器格式:
country:BG AND year:[2020 TO *]
GemFire VectorStore 支持多种过滤操作:
-
相等:
country == 'BG'→country:BG -
不等:
city != 'Sofia'→city: NOT Sofia -
大于:
year > 2020→year:{2020 TO *] -
大于或等于:
year >= 2020→year:[2020 TO *] -
小于:
year < 2025→year:[* TO 2025} -
小于或等于:
year ⇐ 2025→year:[* TO 2025] -
包含(IN):
country in ['BG', 'NL']→country:(BG OR NL) -
不包含(NOT IN):
country nin ['BG', 'NL']→NOT country:(BG OR NL) -
逻辑与/或(AND/OR):用于组合条件的逻辑运算符
-
分组:对复杂表达式使用括号进行分组
-
日期筛选:日期值采用 ISO 8601 格式(例如
2024-01-07T14:29:12Z)
若要在 GemFire VectorStore 中使用元数据过滤,您必须在创建向量存储时指定可用于过滤的元数据字段。这是通过构建器中的 fields 参数完成的:
GemFireVectorStore.builder(embeddingModel)
.fields(new String[] {"country", "year", "activationDate"})
.build();
或者通过配置属性:
spring.ai.vectorstore.gemfire.fields=country,year,activationDate