此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

GemFire 向量存储

本节将引导您设置用于存储文档嵌入并执行相似性搜索的GemFireVectorStorespring-doc.cadn.net.cn

GemFire 是一个分布式、内存中的键值存储,以闪电般的速度执行读写操作。它提供高度可用的并行消息队列、持续可用性和事件驱动架构,可以在不停机的情况下动态扩展。随着您的数据大小需求增加以支持高性能、实时应用程序,GemFire 可以轻松实现线性扩展。spring-doc.cadn.net.cn

GemFire 向量数据库 扩展了 GemFire 的能力,作为一个多功能的向量数据库,能够高效地存储、检索并执行向量相似性搜索。spring-doc.cadn.net.cn

前置条件

  1. 启用 GemFire VectorDB 扩展的 GemFire 集群spring-doc.cadn.net.cn

  2. 用于计算文档嵌入的 EmbeddingModel bean。有关更多信息,请参阅 EmbeddingModel 部分。 在本地机器上运行的一个选项是 ONNX 和 all-MiniLM-L6-v2 句子转换器。spring-doc.cadn.net.cn

Auto-configuration

Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。spring-doc.cadn.net.cn

将 GemFire VectorStore Spring Boot starter 添加到项目的 Maven 构建文件 pom.xml 中:spring-doc.cadn.net.cn

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

或到您的 Gradle build.gradle 文件spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-gemfire'
}

配置属性

你可以在 Spring Boot 配置中使用以下属性来进一步配置 GemFireVectorStorespring-doc.cadn.net.cn

<property> </property> 默认值

spring.ai.vectorstore.gemfire.hostspring-doc.cadn.net.cn

本机主机spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.portspring-doc.cadn.net.cn

8080spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.initialize-schemaspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.index-namespring-doc.cadn.net.cn

spring-ai-gemfire-storespring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.beam-widthspring-doc.cadn.net.cn

100spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.max-connectionsspring-doc.cadn.net.cn

16spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.vector-similarity-functionspring-doc.cadn.net.cn

COSINEspring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.fieldsspring-doc.cadn.net.cn

[]spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.bucketsspring-doc.cadn.net.cn

0spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.usernamespring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.passwordspring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.tokenspring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

手动配置

要仅使用GemFireVectorStore,而不使用Spring Boot的自动配置,请将以下依赖项添加到项目中的Mavenpom.xmlspring-doc.cadn.net.cn

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

对于使用 Gradle 的用户,请在 dependencies 块下将以下内容添加到您的 build.gradle 文件中以仅使用 GemFireVectorStorespring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-gemfire-store'
}

用法

这是一个示例,它创建了 GemfireVectorStore 的实例,而不是使用自动配置spring-doc.cadn.net.cn

@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
    return GemFireVectorStore.builder(embeddingModel)
        .host("localhost")
        .port(7071)
        .username("my-user-name")
        .password("my-password")
        .indexName("my-vector-index")
        .fields(new String[] {"country", "year", "activationDate"}) // Optional: fields for metadata filtering
        .initializeSchema(true)
        .build();
}

默认配置连接到位于 localhost:8080 的 GemFire 集群spring-doc.cadn.net.cn

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!!”的文档。spring-doc.cadn.net.cn

你还可以使用相似度阈值来限制结果数量:spring-doc.cadn.net.cn

List<Document> results = vectorStore.similaritySearch(
   SearchRequest.builder().query("Spring").topK(5)
      .similarityThreshold(0.5d).build());

元数据过滤

您可以将通用且可移植的元数据过滤器与GemFire VectorStore一起使用。spring-doc.cadn.net.cn

例如,您可以使用文本表达式语言:spring-doc.cadn.net.cn

vectorStore.similaritySearch(SearchRequest.builder()
        .query("The World")
        .topK(5)
        .similarityThreshold(0.7)
        .filterExpression("country == 'BG' && year >= 2020").build());

或以编程方式使用 Filter.Expression DSL:spring-doc.cadn.net.cn

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的专有查询格式。

例如,此可移植的过滤器表达式:spring-doc.cadn.net.cn

country == 'BG' && year >= 2020

被转换为专有的 GemFire VectorDB 过滤器格式:spring-doc.cadn.net.cn

country:BG AND year:[2020 TO *]

GemFire VectorStore 支持广泛的过滤操作:spring-doc.cadn.net.cn

要使用 GemFire VectorStore 的元数据过滤,您必须在创建向量存储时指定可以过滤的元数据字段。这是通过在构建器中使用 fields 参数来完成的:spring-doc.cadn.net.cn

GemFireVectorStore.builder(embeddingModel)
    .fields(new String[] {"country", "year", "activationDate"})
    .build();

通过配置属性:spring-doc.cadn.net.cn

spring.ai.vectorstore.gemfire.fields=country,year,activationDate