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
Add builder pattern to RedisVectorStore and refactor package name
Refactors RedisVectorStore to use the builder pattern for improved
configuration and usability. The changes include:
* Move classes to org.springframework.ai.vectorstore.redis package
* Add RedisBuilder with comprehensive configuration options
* Deprecate RedisVectorStoreConfig in favor of builder pattern
* Enhance documentation with detailed usage examples
* Improve error handling and parameter validation
This change makes RedisVectorStore configuration more intuitive and
consistent with other vector stores in the project.
@@ -45,41 +45,15 @@ TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Man
45
45
46
46
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
47
47
48
-
49
48
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.
50
49
51
50
NOTE: this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default.
52
51
52
+
Please have a look at the list of <<redisvector-properties,configuration parameters>> for the vector store to learn about the default values and configuration options.
53
53
54
54
Additionally, you will need a configured `EmbeddingModel` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information.
55
55
56
-
Here is an example of the needed bean:
57
-
58
-
[source,java]
59
-
----
60
-
@Bean
61
-
public EmbeddingModel embeddingModel() {
62
-
// Can be any other EmbeddingModel implementation.
63
-
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
64
-
}
65
-
----
66
-
67
-
To connect to Redis you need to provide access details for your instance.
68
-
A simple configuration can either be provided via Spring Boot's _application.properties_,
spring.ai.vectorstore.redis.index=<your index name>
74
-
spring.ai.vectorstore.redis.prefix=<your prefix>
75
-
76
-
# API key if needed, e.g. OpenAI
77
-
spring.ai.openai.api.key=<api-key>
78
-
----
79
-
80
-
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.
81
-
82
-
Now you can Auto-wire the Redis Vector Store in your application and use it
56
+
Now you can auto-wire the `RedisVectorStore` as a vector store in your application.
Properties starting with `spring.ai.vectorstore.redis.*` are used to configure the `RedisVectorStore`:
114
98
99
+
[cols="2,5,1",stripes=even]
115
100
|===
101
+
|Property | Description | Default Value
116
102
117
-
== Metadata filtering
103
+
|`spring.ai.vectorstore.redis.initialize-schema`| Whether to initialize the required schema | `false`
104
+
|`spring.ai.vectorstore.redis.index-name` | The name of the index to store the vectors | `spring-ai-index`
105
+
|`spring.ai.vectorstore.redis.prefix` | The prefix for Redis keys | `embedding:`
106
+
|`spring.ai.vectorstore.redis.batching-strategy` | Strategy for batching documents when calculating embeddings. Options are `TOKEN_COUNT` or `FIXED_SIZE` | `TOKEN_COUNT`
107
+
|===
108
+
109
+
== Metadata Filtering
118
110
119
-
You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with RedisVectorStore as well.
111
+
You can leverage the generic, portable xref:api/vectordbs.adoc#metadata-filters[metadata filters] with Redis as well.
120
112
121
113
For example, you can use either the text expression language:
122
114
123
115
[source,java]
124
116
----
125
-
vectorStore.similaritySearch(
126
-
SearchRequest
127
-
.query("The World")
128
-
.withTopK(TOP_K)
129
-
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
130
-
.withFilterExpression("country in ['UK', 'NL'] && year >= 2020"));
The portable filter expressions get automatically converted into link:https://redis.io/docs/interact/search-and-query/query/[Redis search queries].
150
-
For example, the following portable filter expression:
139
+
NOTE: Those (portable) filter expressions get automatically converted into link:https://redis.io/docs/interact/search-and-query/query/[Redis search queries].
140
+
141
+
For example, this portable filter expression:
151
142
152
143
[source,sql]
153
144
----
154
145
country in ['UK', 'NL'] && year >= 2020
155
146
----
156
147
157
-
is converted into Redis query:
148
+
is converted into the proprietary Redis filter format:
158
149
159
-
[source]
150
+
[source,text]
160
151
----
161
152
@country:{UK | NL} @year:[2020 inf]
162
153
----
163
154
164
-
== Manual configuration
155
+
== Manual Configuration
165
156
166
-
If you prefer not to use the auto-configuration, you can manually configure the Redis Vector Store.
167
-
Add the Redis Vector Store and Jedis dependencies
157
+
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:
168
158
169
159
[source,xml]
170
160
----
171
161
<dependency>
172
-
<groupId>org.springframework.ai</groupId>
173
-
<artifactId>spring-ai-redis-store</artifactId>
174
-
</dependency>
175
-
176
-
<dependency>
177
-
<groupId>redis.clients</groupId>
178
-
<artifactId>jedis</artifactId>
179
-
<version>5.1.0</version>
162
+
<groupId>org.springframework.ai</groupId>
163
+
<artifactId>spring-ai-redis-store</artifactId>
180
164
</dependency>
181
165
----
182
166
183
-
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
184
-
185
-
Then, create a `RedisVectorStore` bean in your Spring configuration:
167
+
or to your Gradle `build.gradle` build file.
186
168
187
-
[source,java]
169
+
[source,groovy]
188
170
----
189
-
@Bean
190
-
public VectorStore vectorStore(EmbeddingModel embeddingModel) {
It is more convenient and preferred to create the `RedisVectorStore` as a Bean.
207
-
But if you decide to create it manually, then you must call the `RedisVectorStore#afterPropertiesSet()` after setting the properties and before using the client.
208
-
====
209
-
210
-
[NOTE]
211
-
====
212
-
You must list explicitly all metadata field names and types (`TAG`, `TEXT`, or `NUMERIC`) for any metadata field used in filter expression.
213
-
The `withMetadataFields` above registers filterable metadata fields: `country` of type `TAG`, `year` of type `NUMERIC`.
214
-
====
215
-
216
-
Then in your main code, create some documents:
176
+
Create a `JedisPooled` bean:
217
177
218
178
[source,java]
219
179
----
220
-
List<Document> documents = List.of(
221
-
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)),
222
-
new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()),
223
-
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023)));
180
+
@Bean
181
+
public JedisPooled jedisPooled() {
182
+
return new JedisPooled("<host>", 6379);
183
+
}
224
184
----
225
185
226
-
Now add the documents to your vector store:
227
-
186
+
Then create the `RedisVectorStore` bean using the builder pattern:
228
187
229
188
[source,java]
230
189
----
231
-
vectorStore.add(documents);
232
-
----
233
-
234
-
And finally, retrieve documents similar to a query:
190
+
@Bean
191
+
public VectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel) {
192
+
return RedisVectorStore.builder()
193
+
.jedis(jedisPooled)
194
+
.embeddingModel(embeddingModel)
195
+
.indexName("custom-index") // Optional: defaults to "spring-ai-index"
196
+
.prefix("custom-prefix") // Optional: defaults to "embedding:"
197
+
.metadataFields( // Optional: define metadata fields for filtering
198
+
MetadataField.tag("country"),
199
+
MetadataField.numeric("year"))
200
+
.initializeSchema(true) // Optional: defaults to false
201
+
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
Copy file name to clipboardExpand all lines: spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/redis/RedisVectorStoreAutoConfiguration.java
Copy file name to clipboardExpand all lines: vector-stores/spring-ai-redis-store/src/main/java/org/springframework/ai/vectorstore/redis/RedisFilterExpressionConverter.java
0 commit comments