Redis
本部分将引导您设置 RedisVectorStore
以存储文档嵌入并执行相似性搜索。
This section walks you through setting up RedisVectorStore
to store document embeddings and perform similarity searches.
What is Redis?
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.
What is Redis Vector Search?
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:
-
Store vectors and the associated metadata within hashes or JSON documents
-
Retrieve vectors
-
Perform vector searches
Prerequisites
-
EmbeddingClient
instance to compute the document embeddings. Several options are available:-
Transformers Embedding
- computes the embedding in your local environment. Follow the ONNX Transformers Embedding instructions. -
OpenAI Embedding
- uses the OpenAI embedding endpoint. You need to create an account at OpenAI Signup and generate the api-key token at API Keys. -
You can also use the
Azure OpenAI Embedding
.
-
-
A Redis Stack instance[style="loweralpha"]
-
Redis Cloud (recommended)
-
Docker image redis/redis-stack:latest
-
Dependencies
将这些依赖项添加到你的项目中:
Add these dependencies to your project:
-
Embedding Client boot starter, required for calculating embeddings.
-
Transformers Embedding (Local) and follow the ONNX Transformers Embedding instructions.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
</dependency>
或使用 OpenAI(云)
or use OpenAI (Cloud)
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
你需要提供你的 OpenAI API 密钥。将其设置为一个环境变量,如下所示:
You’ll need to provide your OpenAI API Key. Set it as an environment variable like so:
export SPRING_AI_OPENAI_API_KEY='Your_OpenAI_API_Key'
-
Add the Redis Vector Store and Jedis dependencies
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
</dependency>
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Usage
创建一个连接到 Redis 数据库的 RedisVectorStore 实例:
Create a RedisVectorStore instance connected to your Redis database:
@Bean
public VectorStore vectorStore(EmbeddingClient embeddingClient) {
RedisVectorStoreConfig config = RedisVectorStoreConfig.builder()
.withURI("redis://localhost:6379")
// Define the metadata fields to be used
// in the similarity search filters.
.withMetadataFields(
MetadataField.tag("country"),
MetadataField.numeric("year"))
.build();
return new RedisVectorStore(config, embeddingClient);
}
创建为 Bean 的 |
It is more convenient and preferred to create the |
对于用于过滤表达式中的任何元数据字段,你必须显式列出所有元数据字段名称和类型 ( |
You must list explicitly all metadata field names and types ( |
然后在你的主代码中创建一些文档:
Then in your main code, create some documents:
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)));
现在向你的矢量存储添加文档:
Now add the documents to your vector store:
vectorStore.add(documents);
最后,检索与查询类似的文档:
And finally, retrieve documents similar to a query:
List<Document> results = vectorStore.similaritySearch(
SearchRequest
.query("Spring")
.withTopK(5));
如果一切都顺利,你应该检索包含文本 “Spring AI rocks!!” 的文档。
If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".
Metadata filtering
您还可以利用 RedisVectorStore 的通用可移植过滤器 metadata filters。
You can leverage the generic, portable metadata filters with RedisVectorStore as well.
例如,你可以使用文本表达式语言:
For example, you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("country in ['UK', 'NL'] && year >= 2020"));
或使用表达式 DSL 以编程方式:
or programmatically using the expression DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build()));
可移植筛选器表达式会自动转换成 Redis search queries。例如,以下可移植筛选器表达式:
The portable filter expressions get automatically converted into Redis search queries. For example, the following portable filter expression:
country in ['UK', 'NL'] && year >= 2020
将转换成 Redis 查询:
is converted into Redis query:
@country:{UK | NL} @year:[2020 inf]