GemFire 向量存储
本节将引导您完成设置 GemFireVectorStore
以存储文档嵌入并执行相似性搜索的过程。
GemFire 是一个分布式的、内存中的键值存储系统,能够以极快的速度执行读写操作。它提供了高度可用的并行消息队列、持续可用性以及事件驱动的架构,您可以在不中断服务的情况下动态扩展。随着您的数据规模需求增长以支持高性能的实时应用程序,GemFire 可以轻松地线性扩展。
GemFire VectorDB 扩展了 GemFire 的功能,作为一个多功能的向量数据库,能够高效地存储、检索和执行向量相似性搜索。
先决条件
-
启用了 GemFire VectorDB 扩展的 GemFire 集群
-
一个用于计算文档嵌入的
EmbeddingModel
bean。有关更多信息,请参阅 EmbeddingModel 部分。可以在本地机器上运行的一个选项是 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。
自动配置
将 GemFire VectorStore Spring Boot starter 添加到您项目的 Maven 构建文件 pom.xml
中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store-spring-boot-starter</artifactId>
</dependency>
或者到你的 Gradle build.gradle
文件中
dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store-spring-boot-starter'
}
配置属性
你可以在 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 Boot 自动配置的情况下仅使用 GemFireVectorStore
,请将以下依赖项添加到项目的 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:
// 创建 GemfireVectorStore 实例
GemfireVectorStore vectorStore = new GemfireVectorStore();
// 配置 vectorStore
vectorStore.setRegionName("myVectorRegion");
vectorStore.setSerializer(new JacksonJsonSerializer());
vectorStore.setGemfireTemplate(gemfireTemplate);
// 使用 vectorStore
vectorStore.save(vector);
@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.indexName("my-vector-index")
.initializeSchema(true)
.build();
}
GemFire VectorStore 目前还不支持 元数据过滤器。
默认配置连接到 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());