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

[Feature] Milvus spring boot starter #28

Merged
merged 7 commits into from
Jul 2, 2024
Merged
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
89 changes: 89 additions & 0 deletions langchain4j-milvus-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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.32.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

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

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-milvus</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>milvus</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,53 @@
package dev.langchain4j.store.embedding.milvus.spring;

import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
import io.milvus.common.clientenum.ConsistencyLevelEnum;
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 org.springframework.lang.Nullable;

import java.util.Optional;

import static dev.langchain4j.store.embedding.milvus.spring.MilvusEmbeddingStoreProperties.*;

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

@Bean
@ConditionalOnMissingBean
public MilvusEmbeddingStore milvusEmbeddingStore(MilvusEmbeddingStoreProperties properties,
@Nullable EmbeddingModel embeddingModel) {
String host = Optional.ofNullable(properties.getHost()).orElse(DEFAULT_HOST);
int port = Optional.ofNullable(properties.getPort()).orElse(DEFAULT_PORT);
String collectionName = Optional.ofNullable(properties.getCollectionName()).orElse(DEFAULT_COLLECTION_NAME);
ConsistencyLevelEnum consistencyLevel = Optional.ofNullable(properties.getConsistencyLevel()).orElse(DEFAULT_CONSISTENCY_LEVEL);
Integer dimension = Optional.ofNullable(properties.getDimension()).orElseGet(() -> embeddingModel == null ? null : embeddingModel.dimension());

// get username and password from env variable
String username = Optional.ofNullable(properties.getUsername()).orElse(System.getenv("MILVUS_USERNAME"));
String password = Optional.ofNullable(properties.getPassword()).orElse(System.getenv("MILVUS_PASSWORD"));

return MilvusEmbeddingStore.builder()
.host(host)
.port(port)
.collectionName(collectionName)
.dimension(dimension)
.indexType(properties.getIndexType())
.metricType(properties.getMetricType())
.uri(properties.getUri())
.token(properties.getToken())
.username(username)
.password(password)
.consistencyLevel(consistencyLevel)
.retrieveEmbeddingsOnSearch(properties.getRetrieveEmbeddingsOnSearch())
.autoFlushOnInsert(properties.getAutoFlushOnInsert())
.databaseName(properties.getDatabaseName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.langchain4j.store.embedding.milvus.spring;

import io.milvus.common.clientenum.ConsistencyLevelEnum;
import io.milvus.param.IndexType;
import io.milvus.param.MetricType;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import static io.milvus.common.clientenum.ConsistencyLevelEnum.STRONG;

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

static final String PREFIX = "langchain4j.milvus";
static final String DEFAULT_HOST = "localhost";
static final int DEFAULT_PORT = 19530;
static final String DEFAULT_COLLECTION_NAME = "langchain4j_collection";
static final ConsistencyLevelEnum DEFAULT_CONSISTENCY_LEVEL = STRONG;

private String host;
private Integer port;
private String collectionName;
private Integer dimension;
private IndexType indexType;
private MetricType metricType;
private String uri;
private String token;
private String username;
private String password;
private ConsistencyLevelEnum consistencyLevel;
private Boolean retrieveEmbeddingsOnSearch;
private Boolean autoFlushOnInsert;
private String databaseName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev.langchain4j.store.embedding.elasticsearch.spring.ElasticsearchEmbeddingStoreAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.AllMiniLmL6V2QuantizedEmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
import dev.langchain4j.store.embedding.milvus.spring.MilvusEmbeddingStoreAutoConfiguration;
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.milvus.MilvusContainer;

class MilvusEmbeddingStoreAutoConfigurationIT extends EmbeddingStoreAutoConfigurationIT {

static MilvusContainer milvus = new MilvusContainer("milvusdb/milvus:v2.3.16");
static final String COLLECTION_NAME = "test_collection";

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

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

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

contextRunner
.withBean(AllMiniLmL6V2QuantizedEmbeddingModel.class)
.withPropertyValues(properties())
.run(context -> {
MilvusEmbeddingStore embeddingStore = context.getBean(MilvusEmbeddingStore.class);
embeddingStore.dropCollection(COLLECTION_NAME);
});
}

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

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

@Override
protected String[] properties() {
return new String[]{
"langchain4j.milvus.host=" + milvus.getHost(),
"langchain4j.milvus.port=" + milvus.getMappedPort(19530),
"langchain4j.milvus.collectionName=" + COLLECTION_NAME,
"langchain4j.milvus.retrieveEmbeddingsOnSearch=true"
};
}

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

<properties>
Expand Down
Loading