|
最新快照版本请使用Spring AI 1.1.0! |
GemFire 矢量商店
本节将引导你如何设置宝石火矢量商店用于存储文档嵌入并进行相似性搜索。
GemFire 是一个分布式的内存键值存储器,能够以极快的速度执行读写作。它提供高度可用的并行消息队列、持续可用性,以及可以动态扩展且无停机的事件驱动架构。随着数据量需求增加以支持高性能实时应用,GemFire可以轻松线性扩展。
GemFire VectorDB 扩展了 GemFire 的功能,作为一个多功能的向量数据库,高效地存储、检索并执行向量相似性搜索。
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
把 GemFire VectorStore Spring Boot 启动程序添加到你项目的 Maven 构建文件中pom.xml:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-gemfire</artifactId>
</dependency>
或者去你的Gradlebuild.gradle文件
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-gemfire'
}
配置属性
你可以在 Spring Boot 配置中使用以下属性进一步配置宝石火矢量商店.
| 属性 | 默认值 |
|---|---|
|
本地主持 |
|
8080 |
|
|
|
春艾宝石火商店 |
|
100 |
|
16 |
|
余弦 |
|
[] |
|
0 |
手动配置
仅用宝石火矢量商店, 如果没有 Spring Boot 的自动配置,请在你的项目 Maven 中添加以下依赖pom.xml:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store</artifactId>
</dependency>
对于Gradle用户,请在你的build.gradle在依赖块下提交文件,仅使用宝石火矢量商店:
dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store'
}
用法
这里有一个示例,创建了宝石矢量商店而不是使用 AutoConfiguration
@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.indexName("my-vector-index")
.fields(new String[] {"country", "year", "activationDate"}) // Optional: fields for metadata filtering
.initializeSchema(true)
.build();
}
|
默认配置连接到一个 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());
你应该获取包含“春季AI太棒了!!”的文档。
你也可以用相似度阈值限制结果数量:
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());
或者程序化地使用滤波。表达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 查询格式。 |
例如,这个可移植的Filter表达式:
country == 'BG' && year >= 2020
转换为专有的 GemFire VectorDB 过滤格式:
country:BG AND year:[2020 TO *]
GemFire 矢量商店支持多种过滤作:
-
平等:
乡村 == 'BG'→国家:BG -
不平等:
城市!= “索菲亚”→城市:不是索菲亚 -
超越:
2020年>年→年份:{2020年至*] -
大于或等:
>年= 2020年→年份:[2020年至*] -
低于:
2025年<年→年份:[* 至2025年} -
小于或等:
2025⇐年→年份:[* 至2025年] -
阅读:
乡村音乐在['BG', 'NL']→国家:(黑道或国家联赛) -
不在:
乡村忍者 ['BG', 'NL']→不是国家:(英国或国家) -
AND/OR:用于组合条件的逻辑算子
-
分组:复杂表达式请使用括号
-
日期过滤:ISO 8601格式的日期值(例如,
2024-01-07T14:29:12Z)
|
要使用GemFire矢量存储的元数据过滤,必须在创建矢量存储时指定可过滤的元数据字段。这是通过
或者通过配置属性:
|