Redis
本节将引导您完成配置RedisVectorStore以存储文档嵌入并执行相似性搜索的过程。
Redis 是一个基于BSD授权的开源内存数据结构存储程序,可作为数据库、缓存、消息中间件和流处理引擎使用。Redis提供了字符串、哈希表、列表、集合、有序集合(支持范围查询)、位图、哈希计数器、地理空间索引和流等数据结构。
Redis 搜索与查询 延伸了 Redis OSS 的核心功能,允许您将 Redis 用作向量数据库:
-
将向量及其相关的元数据存储在哈希表或JSON文档中。
-
获取向量
-
执行向量搜索
自动配置
|
There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。 |
Spring Boot自动生成Redis Vector Store的配置参数。
要启用它,只需在项目Maven pom.xml文件中添加以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-redis</artifactId>
</dependency>
或者添加到您的Gradle 构建脚本文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-redis'
}
| 参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。 |
| 引用 Artifact 仓库 部分,以在构建文件中添加 Maven Central 和/或 Snapshot 仓库。 |
The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the initializeSchema boolean in the appropriate constructor or by setting …initialize-schema=true in the application.properties file.
| 这是一个破坏性变更!在早期版本的 Spring AI 中,此模式初始化是默认进行的。 |
请查看以下向量存储的配置参数列表,了解默认值和配置选项。这些配置参数的类型为C0。
此外,您还需要配置一个EmbeddingModel的豆。有关详细信息,请参阅
现在您可以在应用程序中自动将RedisVectorStore作为向量存储进行注入。
@Autowired VectorStore vectorStore;
// ...
List <Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
// Add the documents to Redis
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
连接到 Redis 并使用 RedisVectorStore,你需要提供你的实例访问细节。
一个简单的配置可以通过 Spring Boot 的 application.yml 提供,
spring:
data:
redis:
url: <redis instance url>
ai:
vectorstore:
redis:
initialize-schema: true
index-name: custom-index
prefix: custom-prefix
For Redis 连接配置,或者,可以通过 Spring Boot 的 application.properties 提供一个简单的配置。
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.username=default
spring.data.redis.password=
以 spring.ai.vectorstore.redis.* 开头的属性用于配置 RedisVectorStore:
| 属性 | 描述 | 默认值 |
|---|---|---|
|
是否初始化所需的模式 |
|
|
存储向量的索引名称 |
|
|
Redis键的前缀 |
|
元数据过滤
You can leverage the generic, portable 元数据过滤器 with Redis as well.
例如,你可以使用文本表达式语言:
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("country in ['UK', 'NL'] && year >= 2020").build());
或通过编程方式使用 Filter.Expression DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build()).build());
| 那些(便携式)过滤表达式会自动转换为 Redis搜索查询。 |
例如,这个可移植的过滤表达式:
country in ['UK', 'NL'] && year >= 2020
被转换为专有 Redis 过滤器格式:
@country:{UK | NL} @year:[2020 inf]
手动配置
而不是使用Spring Boot的自动配置,你可以手动配置Redis向量存储。为此,你需要在你的项目中添加以下内容:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis-store</artifactId>
</dependency>
或者添加到您的Gradle 构建脚本文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-redis-store'
}
创建一个 零 bean:
@Bean
public JedisPooled jedisPooled() {
return new JedisPooled("<host>", 6379);
}
然后使用构建器模式创建 RedisVectorStore Bean:
@Bean
public VectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel) {
return RedisVectorStore.builder(jedisPooled, embeddingModel)
.indexName("custom-index") // Optional: defaults to "spring-ai-index"
.prefix("custom-prefix") // Optional: defaults to "embedding:"
.metadataFields( // Optional: define metadata fields for filtering
MetadataField.tag("country"),
MetadataField.numeric("year"))
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
|
必须明确列出所有用于过滤表达式的元数据字段名称及其类型( |
访问原生客户端
Redis向量存储的实现提供访问底层Redis客户端(代码块0)的方法:
RedisVectorStore vectorStore = context.getBean(RedisVectorStore.class);
Optional<JedisPooled> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
JedisPooled jedis = nativeClient.get();
// Use the native client for Redis-specific operations
}
本地客户端让你可以访问Redis特定功能和操作,这些功能和操作可能不会通过VectorStore接口暴露出来。