Skip to content

Commit e2825c9

Browse files
sobychackomarkpollack
authored andcommitted
Refactor ChromaVectorStore builder API
The commit restructures the ChromaVectorStore builder pattern to use a no-args constructor with fluent API for setting the ChromaApi. This change: - Makes builder creation consistent with other vector stores - Moves ChromaApi validation to the doValidate method - Improves builder API ergonomics The change requires updating all builder usages to use the new .chromaApi() method instead of passing it in the constructor.
1 parent ad87c12 commit e2825c9

File tree

7 files changed

+26
-14
lines changed

7 files changed

+26
-14
lines changed

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/chroma/ChromaVectorStoreAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public ChromaVectorStore vectorStore(EmbeddingModel embeddingModel, ChromaApi ch
8686
ChromaVectorStoreProperties storeProperties, ObjectProvider<ObservationRegistry> observationRegistry,
8787
ObjectProvider<VectorStoreObservationConvention> customObservationConvention,
8888
BatchingStrategy chromaBatchingStrategy) {
89-
return ChromaVectorStore.builder(chromaApi)
89+
return ChromaVectorStore.builder()
90+
.chromaApi(chromaApi)
9091
.embeddingModel(embeddingModel)
9192
.collectionName(storeProperties.getCollectionName())
9293
.initializeSchema(storeProperties.isInitializeSchema())

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public ChromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi, Str
102102
boolean initializeSchema, ObservationRegistry observationRegistry,
103103
VectorStoreObservationConvention customObservationConvention, BatchingStrategy batchingStrategy) {
104104

105-
this(builder(chromaApi).embeddingModel(embeddingModel)
105+
this(builder().chromaApi(chromaApi)
106+
.embeddingModel(embeddingModel)
106107
.collectionName(collectionName)
107108
.initializeSchema(initializeSchema)
108109
.observationRegistry(observationRegistry)
@@ -113,8 +114,11 @@ public ChromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi, Str
113114
/**
114115
* @param builder {@link Builder} for chroma vector store
115116
*/
116-
private ChromaVectorStore(ChromaBuilder builder) {
117+
protected ChromaVectorStore(ChromaBuilder builder) {
117118
super(builder);
119+
120+
Assert.notNull(builder.chromaApi, "ChromaApi must not be null");
121+
118122
this.chromaApi = builder.chromaApi;
119123
this.collectionName = builder.collectionName;
120124
this.initializeSchema = builder.initializeSchema;
@@ -151,8 +155,8 @@ public void afterPropertiesSet() throws Exception {
151155
}
152156
}
153157

154-
public static ChromaBuilder builder(ChromaApi chromaApi) {
155-
return new ChromaBuilder(chromaApi);
158+
public static ChromaBuilder builder() {
159+
return new ChromaBuilder();
156160
}
157161

158162
@Override
@@ -274,7 +278,7 @@ public VectorStoreObservationContext.Builder createObservationContextBuilder(Str
274278

275279
public static class ChromaBuilder extends AbstractVectorStoreBuilder<ChromaBuilder> {
276280

277-
private final ChromaApi chromaApi;
281+
private ChromaApi chromaApi;
278282

279283
private String collectionName = DEFAULT_COLLECTION_NAME;
280284

@@ -286,9 +290,10 @@ public static class ChromaBuilder extends AbstractVectorStoreBuilder<ChromaBuild
286290

287291
private boolean initializeImmediately = false;
288292

289-
public ChromaBuilder(ChromaApi chromaApi) {
293+
public ChromaBuilder chromaApi(ChromaApi chromaApi) {
290294
Assert.notNull(chromaApi, "ChromaApi must not be null");
291295
this.chromaApi = chromaApi;
296+
return this;
292297
}
293298

294299
/**

vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/BasicAuthChromaWhereIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public ChromaApi chromaApi(RestClient.Builder builder) {
111111

112112
@Bean
113113
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
114-
return ChromaVectorStore.builder(chromaApi)
114+
return ChromaVectorStore.builder()
115+
.chromaApi(chromaApi)
115116
.embeddingModel(embeddingModel)
116117
.collectionName("TestCollection")
117118
.initializeSchema(true)

vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaApiIT.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ void shouldUseExistingCollectionWhenSchemaInitializationDisabled() { // initiali
207207
assertThat(collection).isNotNull();
208208
assertThat(collection.name()).isEqualTo("test-collection");
209209

210-
ChromaVectorStore store = ChromaVectorStore.builder(this.chromaApi)
210+
ChromaVectorStore store = ChromaVectorStore.builder()
211+
.chromaApi(this.chromaApi)
211212
.embeddingModel(this.embeddingModel)
212213
.collectionName("test-collection")
213214
.initializeImmediately(true)
@@ -219,7 +220,7 @@ void shouldUseExistingCollectionWhenSchemaInitializationDisabled() { // initiali
219220

220221
@Test
221222
void shouldCreateNewCollectionWhenSchemaInitializationEnabled() {
222-
ChromaVectorStore store = new ChromaVectorStore.ChromaBuilder(this.chromaApi)
223+
ChromaVectorStore store = new ChromaVectorStore.ChromaBuilder().chromaApi(this.chromaApi)
223224
.embeddingModel(this.embeddingModel)
224225
.collectionName("new-collection")
225226
.initializeSchema(true)
@@ -236,7 +237,8 @@ void shouldCreateNewCollectionWhenSchemaInitializationEnabled() {
236237

237238
@Test
238239
void shouldFailWhenCollectionDoesNotExist() {
239-
assertThatThrownBy(() -> new ChromaVectorStore.ChromaBuilder(this.chromaApi).embeddingModel(this.embeddingModel)
240+
assertThatThrownBy(() -> new ChromaVectorStore.ChromaBuilder().chromaApi(this.chromaApi)
241+
.embeddingModel(this.embeddingModel)
240242
.collectionName("non-existent")
241243
.initializeSchema(false)
242244
.initializeImmediately(true)

vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ public ChromaApi chromaApi(RestClient.Builder builder) {
252252

253253
@Bean
254254
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
255-
return ChromaVectorStore.builder(chromaApi)
255+
return ChromaVectorStore.builder()
256+
.chromaApi(chromaApi)
256257
.embeddingModel(embeddingModel)
257258
.collectionName("TestCollection")
258259
.initializeSchema(true)

vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreObservationIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public ChromaApi chromaApi(RestClient.Builder builder) {
176176
@Bean
177177
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi,
178178
ObservationRegistry observationRegistry) {
179-
return ChromaVectorStore.builder(chromaApi)
179+
return ChromaVectorStore.builder()
180+
.chromaApi(chromaApi)
180181
.embeddingModel(embeddingModel)
181182
.collectionName("TestCollection")
182183
.initializeSchema(true)

vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/TokenSecuredChromaWhereIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ public ChromaApi chromaApi(RestClient.Builder builder) {
144144

145145
@Bean
146146
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
147-
return ChromaVectorStore.builder(chromaApi)
147+
return ChromaVectorStore.builder()
148+
.chromaApi(chromaApi)
148149
.embeddingModel(embeddingModel)
149150
.collectionName("TestCollection")
150151
.initializeSchema(true)

0 commit comments

Comments
 (0)