此版本仍在开发中,尚不被认为是稳定的。对于最新的快照版本,请使用 Spring AI 1.0.1spring-doc.cadn.net.cn

GemFire 矢量商店

本节将引导您完成设置GemFireVectorStore以存储文档嵌入并执行相似性搜索。spring-doc.cadn.net.cn

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

GemFire VectorDB 扩展了 GemFire 的功能,作为多功能向量数据库,可以有效地存储、检索和执行向量相似性搜索。spring-doc.cadn.net.cn

前提条件

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

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

自动配置

Spring AI 自动配置、入门模块的工件名称发生了重大变化。 有关更多信息,请参阅升级说明spring-doc.cadn.net.cn

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

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

或 Gradlebuild.gradle文件spring-doc.cadn.net.cn

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

配置属性

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

属性 默认值

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

余弦spring-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

手动配置

要仅使用GemFireVectorStore,无需 Spring Boot 的自动配置,请将以下依赖项添加到项目的 Mavenpom.xml:spring-doc.cadn.net.cn

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

对于 Gradle 用户,请将以下内容添加到您的build.gradle文件以仅使用GemFireVectorStore:spring-doc.cadn.net.cn

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

用法

下面是一个示例,用于创建GemfireVectorStore而不是使用 AutoConfigurationspring-doc.cadn.net.cn

@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
    return GemFireVectorStore.builder(embeddingModel)
        .host("localhost")
        .port(7071)
        .indexName("my-vector-index")
        .initializeSchema(true)
        .build();
}

GemFire VectorStore 尚不支持元数据过滤器spring-doc.cadn.net.cn

默认配置连接到 GemFire 集群,位于localhost:8080spring-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());