GemFire 向量存储

本节将引导您设置 GemFireVectorStore 来存储文档嵌入并执行相似性搜索。 GemFire 是一个分布式、内存中、键值存储,以极快的速度执行读写操作。它提供高可用并行消息队列、持续可用性和事件驱动架构,您可以动态扩展而无需停机。随着您的数据大小需求增加以支持高性能、实时应用程序,GemFire 可以轻松实现线性扩展。 GemFire VectorDB 扩展了 GemFire 的功能,作为一个多功能向量数据库,可高效存储、检索并执行向量相似性搜索。

先决条件

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

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

自动配置

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

将 GemFire VectorStore Spring Boot 启动器添加到您的项目 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 用户,将以下内容添加到您的 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 尚不支持 元数据过滤器

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