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());