GemFire 向量儲存庫
本節將引導您設定 GemFireVectorStore
以儲存文件嵌入並執行相似度搜尋。
GemFire 是一個分散式、記憶體內、鍵值儲存庫,以極快的速度執行讀取和寫入操作。它提供高可用性的平行訊息佇列、持續可用性,以及可動態擴展而無需停機的事件驅動架構。隨著您的資料大小需求增加以支援高效能、即時應用程式,GemFire 可以輕鬆地線性擴展。
GemFire VectorDB 擴展了 GemFire 的功能,作為一個多功能的向量資料庫,可以有效率地儲存、檢索和執行向量相似度搜尋。
先決條件
-
具有啟用 GemFire VectorDB 擴充功能的 GemFire 叢集
-
一個
EmbeddingModel
Bean 來計算文件嵌入。有關更多資訊,請參閱 EmbeddingModel 章節。在本機電腦上執行的選項是 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。
自動配置
將 GemFire VectorStore Spring Boot Starter 新增至您的專案 Maven 建置檔案 pom.xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store-spring-boot-starter</artifactId>
</dependency>
或新增至您的 Gradle build.gradle
檔案
dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store-spring-boot-starter'
}
配置屬性
您可以使用 Spring Boot 配置中的以下屬性來進一步配置 GemFireVectorStore
。
屬性 | 預設值 |
---|---|
|
localhost |
|
8080 |
|
|
|
spring-ai-gemfire-store |
|
100 |
|
16 |
|
COSINE |
|
[] |
|
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 VectorStore vectorStore(EmbeddingModel embeddingModel) {
return new GemFireVectorStore(new GemFireVectorStoreConfig()
.setIndexName("my-vector-index")
.setPort(7071), embeddingClient);
}
GemFire VectorStore 尚不支援 metadata 過濾器。 |
預設配置連線到 |
-
在您的應用程式中,建立一些文件
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.query("Spring").withTopK(5));
您應該檢索包含文字 "Spring AI rocks!!" 的文件。
您也可以使用相似度閾值限制結果數量
List<Document> results = vectorStore.similaritySearch(
SearchRequest.query("Spring").withTopK(5)
.withSimilarityThreshold(0.5d));