GemFire 向量存储

本节将引导您设置 GemFireVectorStore 以存储文档嵌入并执行相似性搜索。

GemFire 是一种分布式、内存中、键值存储,可实现极速的读写操作。它提供了高可用性的并行消息队列、持续可用性以及可以动态扩展而无需停机的事件驱动架构。随着您的数据大小需求增加以支持高性能、实时应用程序,GemFire 可以轻松地进行线性扩展。

GemFire VectorDB 扩展了 GemFire 的能力,作为一个多功能向量数据库,可高效存储、检索并执行向量相似性搜索。

前提条件

  1. 一个启用了 GemFire VectorDB 扩展的 GemFire 集群

  2. 一个用于计算文档嵌入的 EmbeddingModel bean。更多信息请参阅 EmbeddingModel 部分。一个可以在本地机器上运行的选项是 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。

自动配置

Spring AI 自动配置、starter 模块的 artifact 名称发生了重大变化。更多信息请参阅升级说明

将 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

属性 默认值

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

手动配置

若要仅使用 GemFireVectorStore 而不使用 Spring Boot 的自动配置,请将以下依赖项添加到您项目的 Maven pom.xml

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-gemfire-store</artifactId>
</dependency>

对于 Gradle 用户,若要仅使用 GemFireVectorStore,请将以下内容添加到您的 build.gradle 文件中的 dependencies 块下

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 向量存储尚不支持 元数据过滤器

默认配置连接到 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());