generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
656edc6
commit 34e890c
Showing
9 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
micronaut-langchain4j-store-elasticsearch/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
id("io.micronaut.build.internal.langchain4j-module") | ||
id("io.micronaut.test-resources") | ||
} | ||
|
||
dependencies { | ||
annotationProcessor(projects.micronautLangchain4jProcessor) | ||
annotationProcessor(mn.micronaut.inject.java) | ||
annotationProcessor(mnSourcegen.micronaut.sourcegen.generator.java) | ||
api(projects.micronautLangchain4jCore) | ||
api(mnElasticsearch.micronaut.elasticsearch) | ||
implementation(libs.langchain4j.elasticsearch) | ||
testAnnotationProcessor(mn.micronaut.inject.java) | ||
testImplementation(mnTest.micronaut.test.junit5) | ||
testImplementation(mnSerde.micronaut.serde.jackson) | ||
testImplementation(libs.langchain4j.embeddings.all.minilm.l6.v2) | ||
testRuntimeOnly(mnLogging.logback.classic) | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") | ||
} |
44 changes: 44 additions & 0 deletions
44
...ore-elasticsearch/src/main/java/io/micronaut/langchain4j/elasticsearch/ElasticModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.langchain4j.elasticsearch; | ||
|
||
import dev.langchain4j.store.embedding.EmbeddingStore; | ||
import dev.langchain4j.store.embedding.elasticsearch.ElasticsearchEmbeddingStore; | ||
import io.micronaut.langchain4j.annotation.Lang4jConfig; | ||
|
||
@Lang4jConfig( | ||
models = @Lang4jConfig.Model( | ||
kind = EmbeddingStore.class, | ||
impl = ElasticsearchEmbeddingStore.class | ||
), | ||
properties = { | ||
@Lang4jConfig.Property( | ||
name = "restClient", | ||
injected = true, | ||
common = true | ||
), | ||
@Lang4jConfig.Property( | ||
name = "apiKey", | ||
common = true | ||
), | ||
@Lang4jConfig.Property( | ||
name = "serverUrl", | ||
common = true | ||
) | ||
} | ||
) | ||
final class ElasticModule { | ||
} |
40 changes: 40 additions & 0 deletions
40
...store-elasticsearch/src/test/java/io/micronaut/langchain4j/elasticsearch/ElasticTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.micronaut.langchain4j.elasticsearch; | ||
|
||
import dev.langchain4j.data.embedding.Embedding; | ||
import dev.langchain4j.data.segment.TextSegment; | ||
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel; | ||
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 io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@MicronautTest | ||
@Disabled("TODO: Fails with dependency conflict NoSuchMethodError: 'co.elastic.clients.elasticsearch._types.mapping.DenseVectorProperty") | ||
public class ElasticTest { | ||
@Test | ||
void testEmbeddingStore(EmbeddingStore<TextSegment> embeddingStore) { | ||
Assertions.assertNotNull(embeddingStore); | ||
Assertions.assertInstanceOf(ElasticsearchEmbeddingStore.class, embeddingStore); | ||
EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel(); | ||
|
||
TextSegment segment1 = TextSegment.from("I like football."); | ||
Embedding embedding1 = embeddingModel.embed(segment1).content(); | ||
embeddingStore.add(embedding1, segment1); | ||
|
||
TextSegment segment2 = TextSegment.from("The weather is good today."); | ||
Embedding embedding2 = embeddingModel.embed(segment2).content(); | ||
embeddingStore.add(embedding2, segment2); | ||
|
||
Embedding queryEmbedding = embeddingModel.embed("What is your favourite sport?").content(); | ||
List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(queryEmbedding, 1); | ||
EmbeddingMatch<TextSegment> embeddingMatch = relevant.get(0); | ||
|
||
System.out.println(embeddingMatch.score()); // 0.8144288608390052 | ||
Assertions.assertEquals("I like football.", embeddingMatch.embedded().text()); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
micronaut-langchain4j-store-elasticsearch/src/test/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
langchain4j.elasticsearch.embedding-store.dimension=384 |
14 changes: 14 additions & 0 deletions
14
micronaut-langchain4j-store-elasticsearch/src/test/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
|
||
<logger name="io.micronaut.context.env" level="TRACE" /> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
id("io.micronaut.build.internal.langchain4j-module") | ||
id("io.micronaut.test-resources") | ||
} | ||
|
||
dependencies { | ||
annotationProcessor(projects.micronautLangchain4jProcessor) | ||
annotationProcessor(mn.micronaut.inject.java) | ||
annotationProcessor(mnSourcegen.micronaut.sourcegen.generator.java) | ||
api(projects.micronautLangchain4jCore) | ||
api(mnOpensearch.micronaut.opensearch) | ||
implementation(libs.langchain4j.opensearch) | ||
testAnnotationProcessor(mn.micronaut.inject.java) | ||
testImplementation(mnTest.micronaut.test.junit5) | ||
testImplementation(mnSerde.micronaut.serde.jackson) | ||
testImplementation(libs.langchain4j.embeddings.all.minilm.l6.v2) | ||
testRuntimeOnly(mnLogging.logback.classic) | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") | ||
} |
49 changes: 49 additions & 0 deletions
49
...-store-opensearch/src/main/java/io/micronaut/langchain4j/opensearch/OpenSearchModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.langchain4j.opensearch; | ||
|
||
import dev.langchain4j.store.embedding.EmbeddingStore; | ||
import dev.langchain4j.store.embedding.opensearch.OpenSearchEmbeddingStore; | ||
import io.micronaut.langchain4j.annotation.Lang4jConfig; | ||
|
||
@Lang4jConfig( | ||
models = @Lang4jConfig.Model( | ||
kind = EmbeddingStore.class, | ||
impl = OpenSearchEmbeddingStore.class | ||
), | ||
properties = { | ||
@Lang4jConfig.Property( | ||
name = "openSearchClient", | ||
injected = true, | ||
common = true | ||
), | ||
@Lang4jConfig.Property( | ||
name = "serverUrl", | ||
common = true | ||
), | ||
@Lang4jConfig.Property( | ||
name = "apiKey", | ||
common = true | ||
), | ||
@Lang4jConfig.Property( | ||
name = "options", | ||
injected = true, | ||
common = true | ||
) | ||
} | ||
) | ||
final class OpenSearchModule { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters