GemFire 向量儲存
本節將引導您設定 GemFireVectorStore 以儲存文件嵌入並執行相似性搜尋。
GemFire 是一個分散式記憶體鍵值儲存,以極快的速度執行讀寫操作。它提供高可用並行訊息佇列、連續可用性和事件驅動架構,您可以動態擴充套件而無需停機。隨著資料大小需求增加以支援高效能即時應用程式,GemFire 可以輕鬆線性擴充套件。
GemFire VectorDB 擴充套件了 GemFire 的功能,作為一種通用的向量資料庫,可高效儲存、檢索和執行向量相似性搜尋。
先決條件
-
啟用了 GemFire VectorDB 擴充套件的 GemFire 叢集
-
用於計算文件嵌入的
EmbeddingModelBean。有關更多資訊,請參閱EmbeddingModel 部分。可在本地計算機上執行的選項是 ONNX 和所有 MiniLM-L6-v2 Sentence Transformers。
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
將 GemFire VectorStore Spring Boot 啟動器新增到您專案的 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。
| 財產 | 預設值 |
|---|---|
|
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 使用者,要僅使用 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")
.fields(new String[] {"country", "year", "activationDate"}) // Optional: fields for metadata filtering
.initializeSchema(true)
.build();
}
|
預設配置連線到 |
-
在您的應用程式中,建立一些文件
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());
元資料過濾
您還可以利用通用、可移植的元資料過濾器與 GemFire VectorStore 結合使用。
例如,您可以使用文字表示式語言
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression("country == 'BG' && year >= 2020").build());
或使用 `Filter.Expression` DSL 以程式設計方式
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 查詢格式。 |
例如,這個可移植的過濾器表示式
country == 'BG' && year >= 2020
轉換為專有的 GemFire VectorDB 過濾器格式
country:BG AND year:[2020 TO *]
GemFire VectorStore 支援廣泛的過濾操作
-
相等:
country == 'BG'→country:BG -
不相等:
city != 'Sofia'→city: NOT Sofia -
大於:
year > 2020→year:{2020 TO *] -
大於或等於:
year >= 2020→year:[2020 TO *] -
小於:
year < 2025→year:[* TO 2025} -
小於或等於:
year ⇐ 2025→year:[* TO 2025] -
IN:
country in ['BG', 'NL']→country:(BG OR NL) -
NOT IN:
country nin ['BG', 'NL']→NOT country:(BG OR NL) -
AND/OR: 用於組合條件的邏輯運算子
-
分組: 使用括號表示複雜表示式
-
日期過濾: ISO 8601 格式的日期值(例如,
2024-01-07T14:29:12Z)
|
要將元資料過濾與 GemFire VectorStore 結合使用,您必須在建立向量儲存時指定可過濾的元資料欄位。這透過構建器中的
或透過配置屬性
|