Skip to content

Commit 601b0f0

Browse files
committed
Move batching strategy to base vector store builder
Moving BatchingStrategy configuration from individual vector store implementations to the base AbstractVectorStoreBuilder to reduce code duplication and provide consistent batching behavior across all vector stores. The default TokenCountBatchingStrategy is now set in the base builder class.
1 parent 825de11 commit 601b0f0

File tree

22 files changed

+35
-309
lines changed

22 files changed

+35
-309
lines changed

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/AbstractVectorStoreBuilder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import io.micrometer.observation.ObservationRegistry;
2020

21+
import org.springframework.ai.embedding.BatchingStrategy;
2122
import org.springframework.ai.embedding.EmbeddingModel;
23+
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
2224
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
2325
import org.springframework.lang.Nullable;
2426
import org.springframework.util.Assert;
@@ -40,6 +42,8 @@ public abstract class AbstractVectorStoreBuilder<T extends AbstractVectorStoreBu
4042
@Nullable
4143
protected VectorStoreObservationConvention customObservationConvention;
4244

45+
protected BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();
46+
4347
public AbstractVectorStoreBuilder(EmbeddingModel embeddingModel) {
4448
Assert.notNull(embeddingModel, "EmbeddingModel must be configured");
4549
this.embeddingModel = embeddingModel;
@@ -49,6 +53,10 @@ public EmbeddingModel getEmbeddingModel() {
4953
return this.embeddingModel;
5054
}
5155

56+
public BatchingStrategy getBatchingStrategy() {
57+
return this.batchingStrategy;
58+
}
59+
5260
public ObservationRegistry getObservationRegistry() {
5361
return this.observationRegistry;
5462
}
@@ -81,4 +89,15 @@ public T customObservationConvention(@Nullable VectorStoreObservationConvention
8189
return self();
8290
}
8391

92+
/**
93+
* Sets the batching strategy.
94+
* @param batchingStrategy the strategy to use
95+
* @return the builder instance
96+
*/
97+
public T batchingStrategy(BatchingStrategy batchingStrategy) {
98+
Assert.notNull(batchingStrategy, "BatchingStrategy must not be null");
99+
this.batchingStrategy = batchingStrategy;
100+
return self();
101+
}
102+
84103
}

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.ai.document.Document;
2525
import org.springframework.ai.document.DocumentWriter;
26+
import org.springframework.ai.embedding.BatchingStrategy;
2627
import org.springframework.ai.vectorstore.observation.DefaultVectorStoreObservationConvention;
2728
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
2829
import org.springframework.lang.Nullable;
@@ -108,6 +109,13 @@ interface Builder<T extends Builder<T>> {
108109
*/
109110
T customObservationConvention(VectorStoreObservationConvention convention);
110111

112+
/**
113+
* Sets the batching strategy.
114+
* @param batchingStrategy the strategy to use
115+
* @return the builder instance for method chaining
116+
*/
117+
T batchingStrategy(BatchingStrategy batchingStrategy);
118+
111119
/**
112120
* Builds and returns a new VectorStore instance with the configured settings.
113121
* @return a new VectorStore instance

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.micrometer.observation.ObservationRegistry;
2323

2424
import org.springframework.ai.document.Document;
25+
import org.springframework.ai.embedding.BatchingStrategy;
2526
import org.springframework.ai.embedding.EmbeddingModel;
2627
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
2728
import org.springframework.ai.vectorstore.SearchRequest;
@@ -47,11 +48,14 @@ public abstract class AbstractObservationVectorStore implements VectorStore {
4748

4849
protected final EmbeddingModel embeddingModel;
4950

51+
protected final BatchingStrategy batchingStrategy;
52+
5053
private AbstractObservationVectorStore(EmbeddingModel embeddingModel, ObservationRegistry observationRegistry,
51-
@Nullable VectorStoreObservationConvention customObservationConvention) {
54+
@Nullable VectorStoreObservationConvention customObservationConvention, BatchingStrategy batchingStrategy) {
5255
this.embeddingModel = embeddingModel;
5356
this.observationRegistry = observationRegistry;
5457
this.customObservationConvention = customObservationConvention;
58+
this.batchingStrategy = batchingStrategy;
5559
}
5660

5761
/**
@@ -60,7 +64,8 @@ private AbstractObservationVectorStore(EmbeddingModel embeddingModel, Observatio
6064
* @param builder the builder containing configuration settings
6165
*/
6266
public AbstractObservationVectorStore(AbstractVectorStoreBuilder<?> builder) {
63-
this(builder.getEmbeddingModel(), builder.getObservationRegistry(), builder.getCustomObservationConvention());
67+
this(builder.getEmbeddingModel(), builder.getObservationRegistry(), builder.getCustomObservationConvention(),
68+
builder.getBatchingStrategy());
6469
}
6570

6671
/**

vector-stores/spring-ai-azure-cosmos-db-store/src/main/java/org/springframework/ai/vectorstore/cosmosdb/CosmosDBVectorStore.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,20 @@
5151
import com.fasterxml.jackson.databind.JsonNode;
5252
import com.fasterxml.jackson.databind.ObjectMapper;
5353
import com.fasterxml.jackson.databind.node.ObjectNode;
54-
import io.micrometer.observation.ObservationRegistry;
5554
import org.apache.commons.lang3.tuple.ImmutablePair;
5655
import org.slf4j.Logger;
5756
import org.slf4j.LoggerFactory;
5857
import reactor.core.publisher.Flux;
5958

6059
import org.springframework.ai.document.Document;
61-
import org.springframework.ai.embedding.BatchingStrategy;
6260
import org.springframework.ai.embedding.EmbeddingModel;
6361
import org.springframework.ai.embedding.EmbeddingOptionsBuilder;
64-
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
6562
import org.springframework.ai.observation.conventions.VectorStoreProvider;
6663
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
6764
import org.springframework.ai.vectorstore.SearchRequest;
6865
import org.springframework.ai.vectorstore.filter.Filter;
6966
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
7067
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
71-
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
7268
import org.springframework.lang.Nullable;
7369
import org.springframework.util.Assert;
7470

@@ -98,8 +94,6 @@ public class CosmosDBVectorStore extends AbstractObservationVectorStore implemen
9894

9995
private final List<String> metadataFieldsList;
10096

101-
private final BatchingStrategy batchingStrategy;
102-
10397
private CosmosAsyncContainer container;
10498

10599
/**
@@ -122,7 +116,6 @@ protected CosmosDBVectorStore(Builder builder) {
122116
this.vectorStoreThroughput = builder.vectorStoreThroughput;
123117
this.vectorDimensions = builder.vectorDimensions;
124118
this.metadataFieldsList = builder.metadataFieldsList;
125-
this.batchingStrategy = builder.batchingStrategy;
126119

127120
cosmosClient.createDatabaseIfNotExists(databaseName).block();
128121
initializeContainer(containerName, databaseName, vectorStoreThroughput, vectorDimensions, partitionKeyPath);
@@ -404,8 +397,6 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> {
404397

405398
private List<String> metadataFieldsList = new ArrayList<>();
406399

407-
private BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();
408-
409400
private Builder(CosmosAsyncClient cosmosClient, EmbeddingModel embeddingModel) {
410401
super(embeddingModel);
411402
Assert.notNull(cosmosClient, "CosmosClient must not be null");
@@ -483,18 +474,6 @@ public Builder metadataFields(List<String> metadataFieldsList) {
483474
return this;
484475
}
485476

486-
/**
487-
* Sets the batching strategy.
488-
* @param batchingStrategy the strategy to use
489-
* @return the builder instance
490-
* @throws IllegalArgumentException if batchingStrategy is null
491-
*/
492-
public Builder batchingStrategy(BatchingStrategy batchingStrategy) {
493-
Assert.notNull(batchingStrategy, "BatchingStrategy must not be null");
494-
this.batchingStrategy = batchingStrategy;
495-
return this;
496-
}
497-
498477
@Override
499478
public CosmosDBVectorStore build() {
500479
return new CosmosDBVectorStore(this);

vector-stores/spring-ai-azure-store/src/main/java/org/springframework/ai/vectorstore/azure/AzureVectorStore.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@
4242
import com.azure.search.documents.models.SearchOptions;
4343
import com.azure.search.documents.models.VectorSearchOptions;
4444
import com.azure.search.documents.models.VectorizedQuery;
45-
import io.micrometer.observation.ObservationRegistry;
4645
import org.slf4j.Logger;
4746
import org.slf4j.LoggerFactory;
4847

4948
import org.springframework.ai.document.Document;
5049
import org.springframework.ai.document.DocumentMetadata;
51-
import org.springframework.ai.embedding.BatchingStrategy;
5250
import org.springframework.ai.embedding.EmbeddingModel;
5351
import org.springframework.ai.embedding.EmbeddingOptionsBuilder;
54-
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
5552
import org.springframework.ai.model.EmbeddingUtils;
5653
import org.springframework.ai.observation.conventions.VectorStoreProvider;
5754
import org.springframework.ai.observation.conventions.VectorStoreSimilarityMetric;
@@ -60,7 +57,6 @@
6057
import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
6158
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
6259
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
63-
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
6460
import org.springframework.beans.factory.InitializingBean;
6561
import org.springframework.lang.Nullable;
6662
import org.springframework.util.Assert;
@@ -110,8 +106,6 @@ public class AzureVectorStore extends AbstractObservationVectorStore implements
110106

111107
private final boolean initializeSchema;
112108

113-
private final BatchingStrategy batchingStrategy;
114-
115109
/**
116110
* List of metadata fields (as field name and type) that can be used in similarity
117111
* search query filter expressions. The {@link Document#getMetadata()} can contain
@@ -146,7 +140,6 @@ protected AzureVectorStore(Builder builder) {
146140
this.searchIndexClient = builder.searchIndexClient;
147141
this.initializeSchema = builder.initializeSchema;
148142
this.filterMetadataFields = builder.filterMetadataFields;
149-
this.batchingStrategy = builder.batchingStrategy;
150143
this.defaultTopK = builder.defaultTopK;
151144
this.defaultSimilarityThreshold = builder.defaultSimilarityThreshold;
152145
this.indexName = builder.indexName;
@@ -389,8 +382,6 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> {
389382

390383
private List<MetadataField> filterMetadataFields = List.of();
391384

392-
private BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();
393-
394385
private int defaultTopK = DEFAULT_TOP_K;
395386

396387
private Double defaultSimilarityThreshold = DEFAULT_SIMILARITY_THRESHOLD;
@@ -423,17 +414,6 @@ public Builder filterMetadataFields(List<MetadataField> filterMetadataFields) {
423414
return this;
424415
}
425416

426-
/**
427-
* Sets the batching strategy.
428-
* @param batchingStrategy the strategy to use
429-
* @return the builder instance
430-
*/
431-
public Builder batchingStrategy(BatchingStrategy batchingStrategy) {
432-
Assert.notNull(batchingStrategy, "BatchingStrategy must not be null");
433-
this.batchingStrategy = batchingStrategy;
434-
return this;
435-
}
436-
437417
/**
438418
* Sets the index name for the Azure Vector Store.
439419
* @param indexName the name of the index to use

vector-stores/spring-ai-cassandra-store/src/main/java/org/springframework/ai/vectorstore/cassandra/CassandraVectorStore.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ public class CassandraVectorStore extends AbstractObservationVectorStore impleme
217217

218218
private final boolean closeSessionOnClose;
219219

220-
private final BatchingStrategy batchingStrategy;
221-
222220
private final ConcurrentMap<Set<String>, PreparedStatement> addStmts = new ConcurrentHashMap<>();
223221

224222
private final PreparedStatement deleteStmt;
@@ -239,7 +237,6 @@ protected CassandraVectorStore(Builder builder) {
239237
this.primaryKeyTranslator = builder.primaryKeyTranslator;
240238
this.executor = Executors.newFixedThreadPool(builder.fixedThreadPoolExecutorSize);
241239
this.closeSessionOnClose = builder.closeSessionOnClose;
242-
this.batchingStrategy = builder.batchingStrategy;
243240

244241
ensureSchemaExists(embeddingModel.dimensions());
245242
prepareAddStatement(Set.of());
@@ -777,8 +774,6 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> {
777774

778775
private int fixedThreadPoolExecutorSize = DEFAULT_ADD_CONCURRENCY;
779776

780-
private BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();
781-
782777
private FilterExpressionConverter filterExpressionConverter;
783778

784779
private DocumentIdTranslator documentIdTranslator = (String id) -> List.of(id);
@@ -917,18 +912,6 @@ public Builder disallowSchemaChanges(boolean disallowSchemaChanges) {
917912
return this;
918913
}
919914

920-
/**
921-
* Sets the batching strategy.
922-
* @param batchingStrategy the batching strategy to use
923-
* @return the builder instance
924-
* @throws IllegalArgumentException if batchingStrategy is null
925-
*/
926-
public Builder batchingStrategy(BatchingStrategy batchingStrategy) {
927-
Assert.notNull(batchingStrategy, "BatchingStrategy must not be null");
928-
this.batchingStrategy = batchingStrategy;
929-
return this;
930-
}
931-
932915
/**
933916
* Sets the filter expression converter.
934917
* @param converter the filter expression converter to use

vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ public class ChromaVectorStore extends AbstractObservationVectorStore implements
7979

8080
private final boolean initializeSchema;
8181

82-
private final BatchingStrategy batchingStrategy;
83-
8482
private final ObjectMapper objectMapper;
8583

8684
private boolean initialized = false;
@@ -95,7 +93,6 @@ protected ChromaVectorStore(Builder builder) {
9593
this.collectionName = builder.collectionName;
9694
this.initializeSchema = builder.initializeSchema;
9795
this.filterExpressionConverter = builder.filterExpressionConverter;
98-
this.batchingStrategy = builder.batchingStrategy;
9996
this.objectMapper = JsonMapper.builder().addModules(JacksonUtils.instantiateAvailableModules()).build();
10097

10198
if (builder.initializeImmediately) {
@@ -232,8 +229,6 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> {
232229

233230
private boolean initializeSchema = false;
234231

235-
private BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();
236-
237232
private FilterExpressionConverter filterExpressionConverter = new ChromaFilterExpressionConverter();
238233

239234
private boolean initializeImmediately = false;
@@ -266,18 +261,6 @@ public Builder initializeSchema(boolean initializeSchema) {
266261
return this;
267262
}
268263

269-
/**
270-
* Sets the batching strategy.
271-
* @param batchingStrategy the batching strategy to use
272-
* @return the builder instance
273-
* @throws IllegalArgumentException if batchingStrategy is null
274-
*/
275-
public Builder batchingStrategy(BatchingStrategy batchingStrategy) {
276-
Assert.notNull(batchingStrategy, "batchingStrategy must not be null");
277-
this.batchingStrategy = batchingStrategy;
278-
return this;
279-
}
280-
281264
/**
282265
* Sets the filter expression converter.
283266
* @param converter the filter expression converter to use

vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ public class ElasticsearchVectorStore extends AbstractObservationVectorStore imp
164164

165165
private final boolean initializeSchema;
166166

167-
private final BatchingStrategy batchingStrategy;
168-
169167
protected ElasticsearchVectorStore(Builder builder) {
170168
super(builder);
171169

@@ -174,7 +172,6 @@ protected ElasticsearchVectorStore(Builder builder) {
174172
this.initializeSchema = builder.initializeSchema;
175173
this.options = builder.options;
176174
this.filterExpressionConverter = builder.filterExpressionConverter;
177-
this.batchingStrategy = builder.batchingStrategy;
178175

179176
String version = Version.VERSION == null ? "Unknown" : Version.VERSION.toString();
180177
this.elasticsearchClient = new ElasticsearchClient(new RestClientTransport(builder.restClient,
@@ -371,8 +368,6 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> {
371368

372369
private boolean initializeSchema = false;
373370

374-
private BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();
375-
376371
private FilterExpressionConverter filterExpressionConverter = new ElasticsearchAiSearchFilterExpressionConverter();
377372

378373
/**
@@ -408,18 +403,6 @@ public Builder initializeSchema(boolean initializeSchema) {
408403
return this;
409404
}
410405

411-
/**
412-
* Sets the batching strategy for vector operations.
413-
* @param batchingStrategy the batching strategy to use
414-
* @return the builder instance
415-
* @throws IllegalArgumentException if batchingStrategy is null
416-
*/
417-
public Builder batchingStrategy(BatchingStrategy batchingStrategy) {
418-
Assert.notNull(batchingStrategy, "batchingStrategy must not be null");
419-
this.batchingStrategy = batchingStrategy;
420-
return this;
421-
}
422-
423406
/**
424407
* Sets the filter expression converter.
425408
* @param converter the filter expression converter to use

0 commit comments

Comments
 (0)