You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This section walks you through setting up `RedisVectorStore` to store document embeddings and perform similarity searches.
4
4
5
-
== What is Redis?
6
-
7
5
link:https://redis.io[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.
8
6
9
-
== What is Redis Vector Search?
10
-
11
7
link:https://redis.io/docs/interact/search-and-query/[Redis Search and Query] extends the core features of Redis OSS and allows you to use Redis as a vector database:
12
8
13
9
* Store vectors and the associated metadata within hashes or JSON documents
@@ -16,53 +12,135 @@ link:https://redis.io/docs/interact/search-and-query/[Redis Search and Query] ex
16
12
17
13
== Prerequisites
18
14
19
-
1. `EmbeddingClient` instance to compute the document embeddings. Several options are available:
2. `EmbeddingClient` instance to compute the document embeddings. Several options are available:
20
+
- If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] to generate the embeddings stored by the `RedisVectorStore`.
21
+
22
+
== Auto-configuration
20
23
21
-
- `Transformers Embedding` - computes the embedding in your local environment. Follow the ONNX Transformers Embedding instructions.
22
-
- `OpenAI Embedding` - uses the OpenAI embedding endpoint. You need to create an account at link:https://platform.openai.com/signup[OpenAI Signup] and generate the api-key token at link:https://platform.openai.com/account/api-keys[API Keys].
23
-
- You can also use the `Azure OpenAI Embedding`.
24
+
Spring AI provides Spring Boot auto-configuration for the Redis Vector Sore.
25
+
To enable it, add the following dependency to your project's Maven `pom.xml` file:
24
26
25
-
2. A Redis Stack instance
26
-
a. https://app.redislabs.com/#/[Redis Cloud] (recommended)
27
-
b. link:https://hub.docker.com/r/redis/redis-stack[Docker] image _redis/redis-stack:latest_
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
33
45
34
-
* Embedding Client boot starter, required for calculating embeddings.
46
+
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
35
47
36
-
* Transformers Embedding (Local) and follow the ONNX Transformers Embedding instructions.
48
+
Additionally, you will need a configured `EmbeddingClient` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] section for more information.
spring.ai.vectorstore.redis.uri=<host of your redis instance>
67
+
spring.ai.vectorstore.redis.index=<your index name>
68
+
spring.ai.vectorstore.redis.prefix=<your prefix>
69
+
70
+
# API key if needed, e.g. OpenAI
71
+
spring.ai.openai.api.key=<api-key>
54
72
----
55
73
56
-
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
74
+
Please have a look at the list of xref:#_configuration_properties[configuration parameters] for the vector store to learn about the default values and configuration options.
75
+
76
+
Now you can Auto-wire the Redis Vector Store in your application and use it
57
77
58
-
You'll need to provide your OpenAI API Key. Set it as an environment variable like so:
78
+
[source,java]
79
+
----
80
+
@Autowired VectorStore vectorStore;
81
+
82
+
// ...
83
+
84
+
List <Document> documents = List.of(
85
+
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
86
+
new Document("The World is Big and Salvation Lurks Around the Corner"),
87
+
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with the Redis vector store.
59
112
60
-
[source,bash]
113
+
For example, you can use either the text expression language:
114
+
115
+
[source,java]
116
+
----
117
+
vectorStore.similaritySearch(
118
+
SearchRequest.defaults()
119
+
.withQuery("The World")
120
+
.withTopK(TOP_K)
121
+
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
122
+
.withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
123
+
----
124
+
125
+
or programmatically using the `Filter.Expression` DSL:
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
82
160
83
-
== Usage
84
-
85
-
Create a RedisVectorStore instance connected to your Redis database:
161
+
Then, create a `RedisVectorStore` bean in your Spring configuration:
86
162
87
163
[source,java]
88
164
----
@@ -101,14 +177,17 @@ public VectorStore vectorStore(EmbeddingClient embeddingClient) {
101
177
}
102
178
----
103
179
104
-
> [NOTE]
105
-
> It is more convenient and preferred to create the `RedisVectorStore` as a Bean.
106
-
> But if you decide to create it manually, then you must call the `RedisVectorStore#afterPropertiesSet()` after setting the properties and before using the client.
107
-
108
-
> [NOTE]
109
-
> You must list explicitly all metadata field names and types (`TAG`, `TEXT`, or `NUMERIC`) for any metadata field used in filter expression.
110
-
> The `withMetadataFields` above registers filterable metadata fields: `country` of type `TAG`, `year` of type `NUMERIC`.
111
-
>
180
+
[NOTE]
181
+
====
182
+
It is more convenient and preferred to create the `RedisVectorStore` as a Bean.
183
+
But if you decide to create it manually, then you must call the `RedisVectorStore#afterPropertiesSet()` after setting the properties and before using the client.
184
+
====
185
+
186
+
[NOTE]
187
+
====
188
+
You must list explicitly all metadata field names and types (`TAG`, `TEXT`, or `NUMERIC`) for any metadata field used in filter expression.
189
+
The `withMetadataFields` above registers filterable metadata fields: `country` of type `TAG`, `year` of type `NUMERIC`.
0 commit comments