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] Elasticsearch spring boot starter #27

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
80 changes: 80 additions & 0 deletions langchian4j-elasticsearch-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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>langchian4j-elasticsearch-spring-boot-starter</artifactId>
<name>LangChain4j Spring Boot starter for Elasticsearch Vector Store</name>
Martin7-1 marked this conversation as resolved.
Show resolved Hide resolved
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-elasticsearch</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>org.testcontainers</groupId>
<artifactId>elasticsearch</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,39 @@
package dev.langchain4j.store.embedding.elasticsearch.spring;

import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.elasticsearch.ElasticsearchEmbeddingStore;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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.util.Optional;

import static dev.langchain4j.store.embedding.elasticsearch.spring.ElasticsearchEmbeddingStoreProperties.*;

@AutoConfiguration
@EnableConfigurationProperties(ElasticsearchEmbeddingStoreProperties.class)
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
langchain4j marked this conversation as resolved.
Show resolved Hide resolved
public class ElasticsearchEmbeddingStoreAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(EmbeddingModel.class)
Martin7-1 marked this conversation as resolved.
Show resolved Hide resolved
public ElasticsearchEmbeddingStore elasticsearchEmbeddingStore(EmbeddingModel embeddingModel,
ElasticsearchEmbeddingStoreProperties properties) {
String serverUrl = Optional.ofNullable(properties.getServerUrl()).orElse(DEFAULT_SERVER_URL);
String indexName = Optional.ofNullable(properties.getIndexName()).orElse(DEFAULT_INDEX_NAME);
int dimension = Optional.ofNullable(properties.getDimension()).orElse(embeddingModel.dimension());

return ElasticsearchEmbeddingStore.builder()
.serverUrl(serverUrl)
.apiKey(properties.getApiKey())
.userName(properties.getUserName())
.password(properties.getPassword())
.indexName(indexName)
.dimension(dimension)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.langchain4j.store.embedding.elasticsearch.spring;

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

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

static final String PREFIX = "langchain4j.elasticsearch";
static final String DEFAULT_SERVER_URL = "http://localhost:9200";
static final String DEFAULT_INDEX_NAME = "langchain4j-index";

private String serverUrl;
private String apiKey;
private String userName;
private String password;
private String indexName;
private Integer dimension;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=dev.langchain4j.store.embedding.elasticsearch.spring.ElasticsearchEmbeddingStoreAutoConfiguration
Martin7-1 marked this conversation as resolved.
Show resolved Hide resolved
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,80 @@
package dev.langchain4j.store.embedding.elasticsearch.spring;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.AllMiniLmL6V2QuantizedEmbeddingModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.elasticsearch.ElasticsearchEmbeddingStore;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

import java.util.List;

import static dev.langchain4j.internal.Utils.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Percentage.withPercentage;

class ElasticsearchEmbeddingStoreAutoConfigurationIT {

static ElasticsearchContainer elasticsearch = new ElasticsearchContainer("elasticsearch:8.9.0")
.withEnv("xpack.security.enabled", "false")
.waitingFor(Wait.defaultWaitStrategy());

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ElasticsearchEmbeddingStoreAutoConfiguration.class));

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

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

@Test
void should_provide_elasticsearch_vector_store() {
TextSegment segment = TextSegment.from("hello");
contextRunner
.withBean(AllMiniLmL6V2QuantizedEmbeddingModel.class)
.withPropertyValues(
"langchain4j.elasticsearch.serverUrl=" + elasticsearch.getHttpHostAddress(),
"langchain4j.elasticsearch.indexName=" + randomUUID()
)
.run(context -> {
EmbeddingModel embeddingModel = context.getBean(AllMiniLmL6V2QuantizedEmbeddingModel.class);
Embedding embedding = embeddingModel.embed(segment.text()).content();

EmbeddingStore<TextSegment> embeddingStore = context.getBean(ElasticsearchEmbeddingStore.class);
assertThat(embeddingStore).isInstanceOf(ElasticsearchEmbeddingStore.class);

String id = embeddingStore.add(embedding, segment);
assertThat(id).isNotBlank();

awaitUntilPersisted();

List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(embedding, 10);
assertThat(relevant).hasSize(1);

EmbeddingMatch<TextSegment> match = relevant.get(0);
assertThat(match.score()).isCloseTo(1, withPercentage(1));
assertThat(match.embeddingId()).isEqualTo(id);
assertThat(match.embedding()).isEqualTo(embedding);
assertThat(match.embedded()).isEqualTo(segment);
});
}

@SneakyThrows
private void awaitUntilPersisted() {
Thread.sleep(1000);
}
}
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,29 @@
<module>langchain4j-open-ai-spring-boot-starter</module>
<module>langchain4j-azure-ai-search-spring-boot-starter</module>
<module>langchain4j-azure-open-ai-spring-boot-starter</module>
<module>langchian4j-elasticsearch-spring-boot-starter</module>
</modules>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>3.2.6</spring.boot.version>
<testcontainers.version>1.19.8</testcontainers.version>
<tinylog.version>2.6.2</tinylog.version>
</properties>

<dependencyManagement>
<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-bom</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
Expand Down Expand Up @@ -63,10 +74,22 @@
<version>1.18.32</version>
</dependency>

<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>${tinylog.version}</version>
</dependency>

<dependency>
<groupId>org.tinylog</groupId>
<artifactId>slf4j-tinylog</artifactId>
<version>${tinylog.version}</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.19.8</version>
<version>${testcontainers.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
Loading