Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chroma Spring Boot Starter #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions langchain4j-chroma-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring</artifactId>
<version>0.37.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>langchain4j-chroma-spring-boot-starter</artifactId>
<name>LangChain4j Spring Boot starter for Chroma</name>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-chroma</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- should be listed before spring-boot-configuration-processor -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- needed to generate automatic metadata about available config properties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-embeddings-all-minilm-l6-v2-q</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-tests</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>chromadb</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.tinylog</groupId>
<artifactId>slf4j-tinylog</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.langchain4j.store.embedding.chroma.spring;

import dev.langchain4j.store.embedding.chroma.ChromaEmbeddingStore;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.time.Duration;
import java.util.Optional;

import static dev.langchain4j.store.embedding.chroma.spring.ChromaEmbeddingStoreProperties.*;

@AutoConfiguration
@EnableConfigurationProperties(ChromaEmbeddingStoreProperties.class)
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class ChromaEmbeddingStoreAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public ChromaEmbeddingStore chromaEmbeddingStore(ChromaEmbeddingStoreProperties properties) {
String baseUrl = Optional.ofNullable(properties.getBaseUrl()).orElse(DEFAULT_BASE_URL);
String collectionName = Optional.ofNullable(properties.getCollectionName()).orElse(DEFAULT_COLLECTION_NAME);
Boolean logRequests = Optional.ofNullable(properties.getLogRequests()).orElse(false);
Boolean logResponses = Optional.ofNullable(properties.getLogResponses()).orElse(false);

ChromaEmbeddingStore.Builder builder = ChromaEmbeddingStore.builder()
.baseUrl(baseUrl)
.collectionName(collectionName)
.logRequests(logRequests)
.logResponses(logResponses);

if(properties.getTimeoutInSeconds() != null) {
Duration timeoutInSeconds = Duration.ofSeconds(properties.getTimeoutInSeconds());
builder.timeout(timeoutInSeconds);
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.langchain4j.store.embedding.chroma.spring;


import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = ChromaEmbeddingStoreProperties.PREFIX)
@Getter
@Setter
public class ChromaEmbeddingStoreProperties {

static final String PREFIX = "langchain4j.chroma";

static final String DEFAULT_BASE_URL = "http://localhost:8080/";
static final String DEFAULT_COLLECTION_NAME = "langchain4j_collection";

private String baseUrl;
private String collectionName;
private Boolean logRequests = false;
private Boolean logResponses = false;
private Integer timeoutInSeconds;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev.langchain4j.store.embedding.chroma.spring.ChromaEmbeddingStoreAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.langchain4j.store.embedding.chroma.spring;

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.onnx.allminilml6v2q.AllMiniLmL6V2QuantizedEmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.chroma.ChromaEmbeddingStore;
import dev.langchain4j.store.embedding.spring.EmbeddingStoreAutoConfigurationIT;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.testcontainers.chromadb.ChromaDBContainer;
import org.testcontainers.containers.wait.strategy.Wait;

class ChromaEmbeddingStoreAutoConfigurationIT extends EmbeddingStoreAutoConfigurationIT {

private static final String DEFAULT_DOCKER_IMAGE = "chromadb/chroma";
static ChromaDBContainer chromadb = new ChromaDBContainer(DEFAULT_DOCKER_IMAGE)
.waitingFor(Wait.defaultWaitStrategy());
static final String COLLECTION_NAME = "test_collection";

@BeforeAll
static void beforeAll() {
chromadb.start();
}

@AfterAll
static void afterAll() {
chromadb.stop();
}

@BeforeEach
void beforeEach() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(autoConfigurationClass()));

contextRunner
.withBean(AllMiniLmL6V2QuantizedEmbeddingModel.class)
.withPropertyValues(properties())
.run(context -> {
ChromaEmbeddingStore embeddingStore = context.getBean(ChromaEmbeddingStore.class);
embeddingStore.removeAll();
});
}

@Override
protected Class<?> autoConfigurationClass() {
return ChromaEmbeddingStoreAutoConfiguration.class;
}

@Override
protected Class<? extends EmbeddingStore<TextSegment>> embeddingStoreClass() {
return ChromaEmbeddingStore.class;
}

@Override
protected String[] properties() {
return new String[]{
"langchain4j.chroma.baseUrl=" + chromadb.getEndpoint(),
"langchain4j.chroma.collectionName=" + COLLECTION_NAME,
};
}

@Override
protected String dimensionPropertyKey() {
return "";
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<module>langchain4j-vertex-ai-gemini-spring-boot-starter</module>
<module>langchain4j-elasticsearch-spring-boot-starter</module>
<module>langchain4j-redis-spring-boot-starter</module>
<module>langchain4j-chroma-spring-boot-starter</module>
<module>langchain4j-qianfan-spring-boot-starter</module>
<module>langchain4j-milvus-spring-boot-starter</module>
<module>langchain4j-dashscope-spring-boot-starter</module>
Expand Down