Redis

本部分将引导您设置 RedisVectorStore 以存储文档嵌入并执行相似性搜索。

This section walks you through setting up RedisVectorStore to store document embeddings and perform similarity searches.

Redis 是开源(BSD 许可)内存数据结构存储,用作数据库、缓存、消息代理和流引擎。Redis 提供了数据结构,比如字符串、哈希、列表、集合、范围查询分类集合、位图、hyperloglog、地理空间索引和流。

Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

Redis Search and Query扩展了 Redis OSS 的核心特性,允许您使用 Redis 作为向量数据库:

Redis Search and Query extends the core features of Redis OSS and allows you to use Redis as a vector database:

  • 将向量和关联的元数据存储在哈希或 JSON 文档中

  • Store vectors and the associated metadata within hashes or JSON documents

  • Retrieve vectors

  • Perform vector searches

Prerequisites

  1. A Redis Stack instance

  1. 以下是使用Gemini将这段文本翻译成中文的结果:实例来计算文档嵌入。有几个选项可供选择:

    • 如果需要, EmbeddingModel 的 API 密钥用于生成由 RedisVectorStore 存储的嵌入。

    • If required, an API key for the EmbeddingModel to generate the embeddings stored by the RedisVectorStore.

  1. EmbeddingModel instance to compute the document embeddings. Several options are available:

    • 如果需要, EmbeddingModel 的 API 密钥用于生成由 RedisVectorStore 存储的嵌入。

    • If required, an API key for the EmbeddingModel to generate the embeddings stored by the RedisVectorStore.

Auto-configuration

Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information.

Spring AI 为 Redis 向量存储提供了 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:

Spring AI provides Spring Boot auto-configuration for the Redis Vector Store. To enable it, add the following dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-vector-store-redis</artifactId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-redis'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

将Maven Central和/或Snapshot存储库添加到您的构建文件中,请参阅 Artifact Repositories 部分。

Refer to the Artifact Repositories section to add Maven Central and/or Snapshot Repositories to your build file.

向量存储实现可以为你初始化所需模式,但你必须通过在适当的构造函数中指定 @s15 布尔值或在 @s17 文件中设置 @s16 来选择加入。

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中,此架构初始化是默认发生的。

this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default.

请查看向量存储的 configuration parameters 列表,了解默认值和配置选项。

Please have a look at the list of redisvector-properties for the vector store to learn about the default values and configuration options.

此外,您还需要一个配置好的 EmbeddingModel bean。有关更多信息,请参阅 EmbeddingModel 部分。

Additionally, you will need a configured EmbeddingModel bean. Refer to the EmbeddingModel section for more information.

现在,您可以将 RedisVectorStore 作为向量存储自动装配到您的应用程序中。

Now you can auto-wire the RedisVectorStore as a vector store in your application.

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

Configuration Properties

要连接到 Redis 并使用 RedisVectorStore ,您需要提供实例的访问详细信息。可以通过 Spring Boot 的 application.yml 提供一个简单的配置,

To connect to Redis and use the RedisVectorStore, you need to provide access details for your instance. A simple configuration can be provided via Spring Boot’s application.yml,

spring:
  data:
    redis:
      url: <redis instance url>
  ai:
    vectorstore:
      redis:
        initialize-schema: true
        index-name: custom-index
        prefix: custom-prefix

对于 Redis 连接配置,或者,可以通过 Spring Boot 的 application.properties 提供一个简单的配置。

For redis connection configuration, alternatively, a simple configuration can be provided via Spring Boot’s 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

Properties starting with spring.ai.vectorstore.redis.* are used to configure the RedisVectorStore:

Property Description Default Value

spring.ai.vectorstore.redis.initialize-schema

Whether to initialize the required schema

false

spring.ai.vectorstore.redis.index-name

The name of the index to store the vectors

spring-ai-index

spring.ai.vectorstore.redis.prefix

The prefix for Redis keys

embedding:

Metadata Filtering

您还可以将通用的、可移植的 metadata filters 与 Redis 结合使用。

You can leverage the generic, portable metadata filters with Redis as well.

例如,你可以使用文本表达式语言:

For example, you can use either the text expression language:

vectorStore.similaritySearch(SearchRequest.builder()
        .query("The World")
        .topK(TOP_K)
        .similarityThreshold(SIMILARITY_THRESHOLD)
        .filterExpression("country in ['UK', 'NL'] && year >= 2020").build());

或使用 Filter.Expression DSL 以编程方式:

or programmatically using the 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 search queries

Those (portable) filter expressions get automatically converted into Redis search queries.

例如,此可移植的筛选器表达式:

For example, this portable filter expression:

country in ['UK', 'NL'] && year >= 2020

转换为专有的 Redis 过滤格式:

is converted into the proprietary Redis filter format:

@country:{UK | NL} @year:[2020 inf]

Manual Configuration

除了使用 Spring Boot 自动配置外,您还可以手动配置 Redis 向量存储。为此,您需要将 spring-ai-redis-store 添加到您的项目:

Instead of using the Spring Boot auto-configuration, you can manually configure the Redis vector store. For this you need to add the spring-ai-redis-store to your project:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-redis-store</artifactId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-redis-store'
}

创建一个 JedisPooled bean:

Create a JedisPooled bean:

@Bean
public JedisPooled jedisPooled() {
    return new JedisPooled("<host>", 6379);
}

然后使用构建器模式创建 RedisVectorStore bean:

Then create the RedisVectorStore bean using the builder pattern:

@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")));
}

您必须明确列出过滤表达式中使用的任何元数据字段的元数据字段名称和类型( TAGTEXTNUMERIC )。上面的 metadataFields 注册了可过滤的元数据字段: country 类型为 TAGyear 类型为 NUMERIC

You must list explicitly all metadata field names and types (TAG, TEXT, or NUMERIC) for any metadata field used in filter expressions. The metadataFields above registers filterable metadata fields: country of type TAG, year of type NUMERIC.

Accessing the Native Client

Redis 向量存储实现通过 getNativeClient() 方法提供对底层原生 Redis 客户端( JedisPooled )的访问:

The Redis Vector Store implementation provides access to the underlying native Redis client (JedisPooled) through the getNativeClient() method:

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 接口公开。

The native client gives you access to Redis-specific features and operations that might not be exposed through the VectorStore interface.