GemFire 矢量商店
本节将指导您设置GemFireVectorStore
来存储文档嵌入并执行相似性搜索。
GemFire 是一种分布式内存中的键值存储,以极快的速度执行读写作。它提供高度可用的并行消息队列、持续可用性和事件驱动型架构,您可以在不停机的情况下动态扩展。随着您的数据大小要求增加以支持高性能、实时应用程序,GemFire 可以轻松地线性扩展。
GemFire VectorDB 扩展了 GemFire 的功能,作为一个多功能的矢量数据库,可以有效地存储、检索和执行矢量相似性搜索。
先决条件
-
启用了 GemFire VectorDB 扩展的 GemFire 集群
-
一
EmbeddingModel
bean 来计算文档嵌入。请参阅 EmbeddingModel 部分以了解更多信息。 在您的机器上本地运行的选项是 ONNX 和全 MiniLM-L6-v2 Sentence Transformers。
自动配置
将 GemFire VectorStore Spring Boot Starters添加到项目的 Maven 构建文件中pom.xml
:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
文件
dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store-spring-boot-starter'
}
配置属性
您可以在 Spring Boot 配置中使用以下属性来进一步配置GemFireVectorStore
.
财产 | 默认值 |
---|---|
|
本地主机 |
|
8080 |
|
|
|
spring-ai-gemfire-store (Spring-ai-gemfire-store) |
|
100 |
|
16 |
|
余弦 |
|
[] |
|
0 |
手动配置
要仅使用GemFireVectorStore
,在没有 Spring Boot 的 Auto-configuration 的情况下,将以下依赖项添加到项目的 Maven 中pom.xml
:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store</artifactId>
</dependency>
对于 Gradle 用户,请将以下内容添加到您的build.gradle
文件,以仅使用GemFireVectorStore
:
dependencies { implementation 'org.springframework.ai:spring-ai-gemfire-store' }
用法
下面是一个示例,该示例创建GemfireVectorStore
而不是使用 AutoConfiguration
@Bean
public VectorStore vectorStore(EmbeddingModel embeddingModel) {
return new GemFireVectorStore(new GemFireVectorStoreConfig()
.setIndexName("my-vector-index")
.setPort(7071), embeddingClient);
}
GemFire VectorStore 尚不支持元数据筛选器。 |
默认配置连接到 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.query("Spring").withTopK(5));
您应该检索包含文本 “Spring AI rocks!!” 的文档。
您还可以使用相似性阈值来限制结果的数量:
List<Document> results = vectorStore.similaritySearch(
SearchRequest.query("Spring").withTopK(5)
.withSimilarityThreshold(0.5d));