GemFire 向量存储
本节将指导您设置 GemFireVectorStore
来存储文档嵌入和执行相似性搜索。
GemFire 是一个分布式、内存中的键值存储,以极快的速度执行读写操作。它提供高可用的并行消息队列、持续可用性和事件驱动架构,您可以动态扩展而无需停机。随着您的数据大小需求增加以支持高性能、实时应用程序,GemFire 可以轻松线性扩展。
GemFire VectorDB 扩展了 GemFire 的功能,作为一个多功能向量数据库,可以高效地存储、检索和执行向量相似性搜索。
前提条件
-
启用了 GemFire VectorDB 扩展的 GemFire 集群
-
一个
EmbeddingModel
bean 来计算文档嵌入。请参阅 EmbeddingModel 部分了解更多信息。 一个可以在您的机器上本地运行的选项是 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。
自动配置
Spring AI 自动配置、starter 模块的构件名称发生了重大变化。 请参阅 升级说明 了解更多信息。 |
将 GemFire VectorStore Spring Boot starter 添加到您项目的 Maven 构建文件 pom.xml
中:
<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
。
属性 | 默认值 |
---|---|
|
localhost |
|
8080 |
|
|
|
spring-ai-gemfire-store |
|
100 |
|
16 |
|
COSINE |
|
[] |
|
0 |
手动配置
要仅使用 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
的实例而不是使用自动配置:
@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.indexName("my-vector-index")
.initializeSchema(true)
.build();
}
GemFire 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("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());