Skip to content

Commit a342417

Browse files
committed
Add builder pattern to SimpleVectorStore
- Create SimpleVectorStoreBuilder which can be used to create the SimpleVectorStore instance - Remove the field embeddingModel from SimpleVectorStore as it can be retrieved from AbstractObservationVectorStore - Deprecate the existing SimpleVectorStore constructors and use the builder to instantiate it - Fix the tests
1 parent be9d6a1 commit a342417

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.slf4j.LoggerFactory;
4444

4545
import org.springframework.ai.document.Document;
46-
import org.springframework.ai.document.DocumentMetadata;
4746
import org.springframework.ai.embedding.EmbeddingModel;
4847
import org.springframework.ai.observation.conventions.VectorStoreProvider;
4948
import org.springframework.ai.observation.conventions.VectorStoreSimilarityMetric;
@@ -79,22 +78,38 @@ public class SimpleVectorStore extends AbstractObservationVectorStore {
7978

8079
protected Map<String, SimpleVectorStoreContent> store = new ConcurrentHashMap<>();
8180

82-
protected EmbeddingModel embeddingModel;
83-
81+
/**
82+
* use {@link #SimpleVectorStore(SimpleVectorStoreBuilder)} instead.
83+
*/
84+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
8485
public SimpleVectorStore(EmbeddingModel embeddingModel) {
8586
this(embeddingModel, ObservationRegistry.NOOP, null);
8687
}
8788

89+
/**
90+
* use {@link #SimpleVectorStore(SimpleVectorStoreBuilder)} instead.
91+
*/
92+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
8893
public SimpleVectorStore(EmbeddingModel embeddingModel, ObservationRegistry observationRegistry,
8994
VectorStoreObservationConvention customObservationConvention) {
95+
this(builder().embeddingModel(embeddingModel)
96+
.observationRegistry(observationRegistry)
97+
.customObservationConvention(customObservationConvention));
98+
}
9099

91-
super(observationRegistry, customObservationConvention);
92-
93-
Objects.requireNonNull(embeddingModel, "EmbeddingModel must not be null");
94-
this.embeddingModel = embeddingModel;
100+
protected SimpleVectorStore(SimpleVectorStoreBuilder builder) {
101+
super(builder);
95102
this.objectMapper = JsonMapper.builder().addModules(JacksonUtils.instantiateAvailableModules()).build();
96103
}
97104

105+
/**
106+
* Creates an instance of SimpleVectorStore builder.
107+
* @return the SimpleVectorStore builder.
108+
*/
109+
public static SimpleVectorStoreBuilder builder() {
110+
return new SimpleVectorStoreBuilder();
111+
}
112+
98113
@Override
99114
public void doAdd(List<Document> documents) {
100115
Objects.requireNonNull(documents, "Documents list cannot be null");
@@ -280,4 +295,14 @@ public static float norm(float[] vector) {
280295

281296
}
282297

298+
public static final class SimpleVectorStoreBuilder extends AbstractVectorStoreBuilder<SimpleVectorStoreBuilder> {
299+
300+
@Override
301+
public SimpleVectorStore build() {
302+
validate();
303+
return new SimpleVectorStore(this);
304+
}
305+
306+
}
307+
283308
}

spring-ai-core/src/test/java/org/springframework/ai/vectorstore/SimpleVectorStoreTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void setUp() {
5757
when(this.mockEmbeddingModel.dimensions()).thenReturn(3);
5858
when(this.mockEmbeddingModel.embed(any(String.class))).thenReturn(new float[] { 0.1f, 0.2f, 0.3f });
5959
when(this.mockEmbeddingModel.embed(any(Document.class))).thenReturn(new float[] { 0.1f, 0.2f, 0.3f });
60-
this.vectorStore = new SimpleVectorStore(this.mockEmbeddingModel);
60+
this.vectorStore = new SimpleVectorStore(SimpleVectorStore.builder().embeddingModel(this.mockEmbeddingModel));
6161
}
6262

6363
@Test

0 commit comments

Comments
 (0)