GemFire Vector Store

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

This section walks you through setting up the GemFireVectorStore to store document embeddings and perform similarity searches.

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

GemFire is a distributed, in-memory, key-value store performing read and write operations at blazingly fast speeds. It offers highly available parallel message queues, continuous availability, and an event-driven architecture you can scale dynamically without downtime. As your data size requirements increase to support high-performance, real-time apps, GemFire can easily scale linearly.

GemFire VectorDB 扩展了 GemFire 的功能,充当一个多功能的向量数据库,可以高效地存储、检索和执行向量相似性搜索。

GemFire VectorDB extends GemFire’s capabilities, serving as a versatile vector database that efficiently stores, retrieves, and performs vector similarity searches.

Prerequisites

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

  1. A GemFire cluster with the GemFire VectorDB extension enabled

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

  2. An EmbeddingModel bean to compute the document embeddings. Refer to the EmbeddingModel section for more information. An option that runs locally on your machine is ONNX and the all-MiniLM-L6-v2 Sentence Transformers.

Auto-configuration

Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information.

将 GemFire VectorStore Spring Boot 启动器添加到您项目的 Maven 构建文件 pom.xml

Add the GemFire VectorStore Spring Boot starter to you project’s Maven build file pom.xml:

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

或添加到您的 Gradle build.gradle 文件

or to your Gradle build.gradle file

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

Configuration properties

您可以在 Spring Boot 配置中使用以下属性来进一步配置 GemFireVectorStore

You can use the following properties in your Spring Boot configuration to further configure the GemFireVectorStore.

Property Default value

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

Manual Configuration

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

To use just the GemFireVectorStore, without Spring Boot’s Auto-configuration add the following dependency to your project’s Maven pom.xml:

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

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

For Gradle users, add the following to your build.gradle file under the dependencies block to use just the GemFireVectorStore:

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

Usage

这是一个创建 GemfireVectorStore 实例而不是使用自动配置的示例。

Here is a sample that creates an instance of the GemfireVectorStore instead of using AutoConfiguration

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

GemFire VectorStore 尚不支持 metadata filters

The GemFire VectorStore does not yet support metadata filters.

好的,用 Gemini 将这段文字翻译成中文:默认配置连接到 localhost:8080 处的 GemFire 集群。

The default configuration connects to a GemFire cluster at localhost:8080

  • 以下是使用Gemini将该文本翻译成中文的结果:在您的应用程序中,创建一些文档:

  • In your application, create a few documents:

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)));
  • 将这段文字翻译成中文:将文档添加到向量数据库中:

  • Add the documents to the vector store:

vectorStore.add(documents);
  • 这是使用 Gemini 翻译后的中文文本:并且能使用相似度搜索来检索文档:

  • And to retrieve documents using similarity search:

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

好的,这是使用Gemini将这段文字翻译成中文的结果:你应该检索包含文本“Spring AI rocks!!”的文档。

You should retrieve the document containing the text "Spring AI rocks!!".

好的,这是用Gemini翻译的中文版本:您还可以使用相似性阈值来限制结果数量:

You can also limit the number of results using a similarity threshold:

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