|
| 1 | +/* |
| 2 | + * Copyright 2023 - 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.ai.testcontainers.service.connection.chroma; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.springframework.ai.autoconfigure.vectorstore.chroma.ChromaVectorStoreAutoConfiguration; |
| 20 | +import org.springframework.ai.document.Document; |
| 21 | +import org.springframework.ai.embedding.EmbeddingModel; |
| 22 | +import org.springframework.ai.transformers.TransformersEmbeddingModel; |
| 23 | +import org.springframework.ai.vectorstore.SearchRequest; |
| 24 | +import org.springframework.ai.vectorstore.VectorStore; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 27 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.test.context.TestPropertySource; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 32 | +import org.testcontainers.chromadb.ChromaDBContainer; |
| 33 | +import org.testcontainers.junit.jupiter.Container; |
| 34 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +@SpringJUnitConfig |
| 42 | +@Testcontainers |
| 43 | +@TestPropertySource(properties = "spring.ai.vectorstore.chroma.store.collectionName=TestCollection") |
| 44 | +class ChromaWithToken2ContainerConnectionDetailsFactoryTest { |
| 45 | + |
| 46 | + @Container |
| 47 | + @ServiceConnection |
| 48 | + static ChromaDBContainer chroma = new ChromaDBContainer("ghcr.io/chroma-core/chroma:0.4.24") |
| 49 | + .withEnv("CHROMA_SERVER_AUTH_CREDENTIALS", "token") |
| 50 | + .withEnv("CHROMA_SERVER_AUTH_CREDENTIALS_PROVIDER", |
| 51 | + "chromadb.auth.token.TokenConfigServerAuthCredentialsProvider") |
| 52 | + .withEnv("CHROMA_SERVER_AUTH_PROVIDER", "chromadb.auth.token.TokenAuthServerProvider"); |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private VectorStore vectorStore; |
| 56 | + |
| 57 | + @Test |
| 58 | + public void addAndSearchWithFilters() { |
| 59 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 60 | + Map.of("country", "Bulgaria")); |
| 61 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 62 | + Map.of("country", "Netherlands")); |
| 63 | + |
| 64 | + vectorStore.add(List.of(bgDocument, nlDocument)); |
| 65 | + |
| 66 | + var request = SearchRequest.query("The World").withTopK(5); |
| 67 | + |
| 68 | + List<Document> results = vectorStore.similaritySearch(request); |
| 69 | + assertThat(results).hasSize(2); |
| 70 | + |
| 71 | + results = vectorStore |
| 72 | + .similaritySearch(request.withSimilarityThresholdAll().withFilterExpression("country == 'Bulgaria'")); |
| 73 | + assertThat(results).hasSize(1); |
| 74 | + assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId()); |
| 75 | + |
| 76 | + results = vectorStore |
| 77 | + .similaritySearch(request.withSimilarityThresholdAll().withFilterExpression("country == 'Netherlands'")); |
| 78 | + assertThat(results).hasSize(1); |
| 79 | + assertThat(results.get(0).getId()).isEqualTo(nlDocument.getId()); |
| 80 | + |
| 81 | + // Remove all documents from the store |
| 82 | + vectorStore.delete(List.of(bgDocument, nlDocument).stream().map(doc -> doc.getId()).toList()); |
| 83 | + } |
| 84 | + |
| 85 | + @Configuration(proxyBeanMethods = false) |
| 86 | + @ImportAutoConfiguration(ChromaVectorStoreAutoConfiguration.class) |
| 87 | + static class Config { |
| 88 | + |
| 89 | + @Bean |
| 90 | + public EmbeddingModel embeddingModel() { |
| 91 | + return new TransformersEmbeddingModel(); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments