Skip to content

Commit ae743f9

Browse files
alexcheng1982tzolov
authored andcommitted
Enhance ChromaVectorStore initialization and add test for disabled schema
- Improve ChromaVectorStore to throw exception on missing collection with disabled schema - Add test case to verify exception is thrown when collection doesn't exist and schema init is false - Correctly set Chroma collection id when initializeSchema set to false Co-authored-by: Christian Tzolov <ctzolov@vmware.com>
1 parent b1377d1 commit ae743f9

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/vectorstore/chroma/ChromaVectorStoreAutoConfigurationIT.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.context.annotation.Configuration;
4242

4343
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.junit.Assert.assertThrows;
4445
import static org.springframework.ai.autoconfigure.vectorstore.observation.ObservationTestUtil.assertObservationRegistry;
4546

4647
/**
@@ -60,13 +61,12 @@ public class ChromaVectorStoreAutoConfigurationIT {
6061
.withUserConfiguration(Config.class)
6162
.withPropertyValues("spring.ai.vectorstore.chroma.client.host=http://" + chroma.getHost(),
6263
"spring.ai.vectorstore.chroma.client.port=" + chroma.getMappedPort(8000),
63-
"spring.ai.vectorstore.chroma.initializeSchema=true",
6464
"spring.ai.vectorstore.chroma.collectionName=TestCollection");
6565

6666
@Test
6767
public void addAndSearchWithFilters() {
6868

69-
contextRunner.run(context -> {
69+
contextRunner.withPropertyValues("spring.ai.vectorstore.chroma.initializeSchema=true").run(context -> {
7070

7171
VectorStore vectorStore = context.getBean(VectorStore.class);
7272
TestObservationRegistry observationRegistry = context.getBean(TestObservationRegistry.class);
@@ -125,6 +125,16 @@ public void addAndSearchWithFilters() {
125125
});
126126
}
127127

128+
@Test
129+
public void throwExceptionOnMissingCollectionAndDisabledInitializedSchema() {
130+
131+
contextRunner.withPropertyValues("spring.ai.vectorstore.chroma.initializeSchema=false").run(context -> {
132+
assertThrows(
133+
"Collection TestCollection doesn't exist and won't be created as the initializeSchema is set to false.",
134+
java.lang.RuntimeException.class, () -> context.getBean(VectorStore.class));
135+
});
136+
}
137+
128138
@Configuration(proxyBeanMethods = false)
129139
static class Config {
130140

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
* their similarity to a query, using the {@link ChromaApi} and {@link EmbeddingModel} for
5151
* embedding calculations. For more information about how it does this, see the official
5252
* <a href="https://www.trychroma.com/">Chroma website</a>.
53+
*
54+
* @author Christian Tzolov
55+
* @author Fu Cheng
56+
*
5357
*/
5458
public class ChromaVectorStore extends AbstractObservationVectorStore implements InitializingBean {
5559

@@ -185,13 +189,16 @@ public String getCollectionId() {
185189

186190
@Override
187191
public void afterPropertiesSet() throws Exception {
188-
189-
if (!this.initializeSchema)
190-
return;
191-
192192
var collection = this.chromaApi.getCollection(this.collectionName);
193193
if (collection == null) {
194-
collection = this.chromaApi.createCollection(new ChromaApi.CreateCollectionRequest(this.collectionName));
194+
if (initializeSchema) {
195+
collection = this.chromaApi
196+
.createCollection(new ChromaApi.CreateCollectionRequest(this.collectionName));
197+
}
198+
else {
199+
throw new RuntimeException("Collection " + this.collectionName
200+
+ " doesn't exist and won't be created as the initializeSchema is set to false.");
201+
}
195202
}
196203
this.collectionId = collection.id();
197204
}

0 commit comments

Comments
 (0)