Skip to content

Commit

Permalink
unified IT
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin7-1 committed Jun 22, 2024
1 parent 7e67afb commit aa3e7d1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 56 deletions.
9 changes: 9 additions & 0 deletions langchian4j-elasticsearch-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
<scope>test</scope>
</dependency>

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

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
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 dev.langchain4j.store.embedding.spring.EmbeddingStoreAutoConfigurationIT;
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 {
class ElasticsearchEmbeddingStoreAutoConfigurationIT extends EmbeddingStoreAutoConfigurationIT {

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();
Expand All @@ -41,40 +28,27 @@ 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();
@Override
protected Class<?> autoConfigurationClass() {
return ElasticsearchEmbeddingStoreAutoConfiguration.class;
}

List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(embedding, 10);
assertThat(relevant).hasSize(1);
@Override
protected Class<? extends EmbeddingStore<TextSegment>> embeddingStoreClass() {
return ElasticsearchEmbeddingStore.class;
}

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);
});
@Override
protected String[] properties() {
return new String[]{
"langchain4j.elasticsearch.serverUrl=" + elasticsearch.getHttpHostAddress(),
"langchain4j.elasticsearch.indexName=" + randomUUID()
};
}

@Override
@SneakyThrows
private void awaitUntilPersisted() {
protected void awaitUntilPersisted() {
Thread.sleep(1000);
}
}
12 changes: 0 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,6 @@
<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>
Expand Down

0 comments on commit aa3e7d1

Please sign in to comment.