Micronaut LangChain4j

Integration between Micronaut and Langchain4j

Version: 0.3.0

1 Introduction

This module provides integration between Micronaut and Langchain4j.

This module is regarded as experimental and subject to change since the underlying technology (Langchain4j) is not yet 1.0.0 or regarded as stable.

Various modules are provided that allow automatically configuring common Langchain4j types like ChatModel, ImageModel etc. Refer to the sections below for the supported Langchain4j extensions.

2 Quick Start

Add the following annotation processor dependency:

GradleMaven
annotationProcessor("io.micronaut.langchain4j:micronaut-langchain4j-processor")
Copy to Clipboard

Then the core module:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-core")
Copy to Clipboard

You are now ready to configure one of the Chat Language Models, for the quick start we will use Ollama:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-ollama")
Copy to Clipboard

To test the integration add the test resources integration to your Maven build or Gradle build.

GradleMaven
testResourcesService("io.micronaut.langchain4j:micronaut-langchain4j-ollama-testresource")
Copy to Clipboard

Add the necessary configuration to configure the model name you want to use:

Configuring the Model Name
PropertiesYamlTomlGroovyHoconJSON
langchain4j.ollama.model-name: orca-mini
Copy to Clipboard

Now you can inject an instance of

You can also define new AI services:

Defining @AiService interfaces
package example;

import dev.langchain4j.service.SystemMessage;
import io.micronaut.langchain4j.annotation.AiService;

@AiService // (1)
public interface Friend {

    @SystemMessage("You are a good friend of mine. Answer using slang.") // (2)
    String chat(String userMessage);
}
Copy to Clipboard
1 Define an interface annotated with @AiService
2 Use Langchain4j annotations like @SystemMessage

You can now inject the @AiService definition into any Micronaut component including tests:

Calling @AiService definitions
package example;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import dev.langchain4j.model.chat.ChatLanguageModel;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@MicronautTest
@Disabled("Ollama Testcontainers broken?")
public class AiServiceTest {
    @Test
    void testAiService(Friend friend, ChatLanguageModel languageModel) {
        String result = friend.chat("Hello");

        assertNotNull(result);
        assertNotNull(languageModel);
    }
}
Copy to Clipboard

3 Chat Language Models

The following modules provide integration with Langchain4j Language Models.

Each module configures one or more ChatLanguageModel beans, making them available for dependency injection based on configuration.

3.1 Anthropic

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-anthropic")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.anthropic.api-key: YOUR_KEY
Copy to Clipboard

3.2 Azure

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-azure")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.azure-open-ai.api-key: YOUR_KEY
langchain4j.azure-open-ai.endpoint: YOUR_ENDPOINT
Copy to Clipboard

You will additionally need to define a bean of type TokenCredentials.

One way to do this is to include the Azure SDK module.

3.3 Bedrock

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-bedrock")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.bedrock-llama.api-key: YOUR_KEY
Copy to Clipboard

You will additionally need to define a bean of type AwsCredentialsProvider.

One way to do this is to include the AWS SDK module.

3.4 HuggingFace

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-hugging-face")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.hugging-face.access-token: YOUR_ACCESS_TOKEN
Copy to Clipboard

3.5 MistralAi

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-mistralai")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.mistral-ai.api-key: YOUR_KEY
Copy to Clipboard

3.6 Ollama

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-ollama")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.ollama.base-url: YOUR_URL
Copy to Clipboard

3.7 OpenAi

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-openai")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.open-ai.api-key: YOUR_KEY
Copy to Clipboard

3.8 Google AI Gemini

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-googleai-gemini")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.google-ai-gemini.api-key: YOUR_API_KEY
Copy to Clipboard

3.9 VertexAi

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-vertexai")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.vertex-ai.endpoint: YOUR_ENDPOINT
langchain4j.vertex-ai.model-name: YOUR_MODEL
langchain4j.vertex-ai.project: YOUR_PROJECT
langchain4j.vertex-ai.location: YOUR_LOCATION
langchain4j.vertex-ai.publisher: YOUR_PUBLISHER
Copy to Clipboard

3.10 VertexAi Gemini

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-vertexai-gemini")
Copy to Clipboard

Then add the necessary configuration.

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.vertex-ai-gemini.model-name: YOUR_MODEL
langchain4j.vertex-ai-gemini.project: YOUR_PROJECT
langchain4j.vertex-ai-gemini.location: YOUR_LOCATION
Copy to Clipboard

4 Embedding Stores

4.1 Elastic Search

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-elasticsearch")
Copy to Clipboard

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
elasticsearch.httpHosts: "http://localhost:9200,http://127.0.0.2:9200"
langchain4j.elasticsearch.embedding-stores.default.dimension: 384
Copy to Clipboard

4.2 MongoDB

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-mongodb-atlas")
Copy to Clipboard

Configuring a MongoDB server
mongodb.servers.default.uri: mongodb://username:password@localhost:27017/databaseName
Example Configuration
PropertiesYamlTomlGroovyHoconJSON
langchain4j.mongodb-atlas.embedding-stores.default.database-name: testdb
langchain4j.mongodb-atlas.embedding-stores.default.collection-name: testcol
langchain4j.mongodb-atlas.embedding-stores.default.index-name: testindex
Copy to Clipboard

4.3 Neo4j

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-neo4j")
Copy to Clipboard

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
neo4j.uri: bolt://localhost
langchain4j.neo4j.embedding-stores.default.dimension: 384
Copy to Clipboard

4.4 Oracle

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-oracle")
Copy to Clipboard

Then add one of the supported JDBC connection pools, for example Hikari:

GradleMaven
runtimeOnly("io.micronaut.sql:micronaut-jdbc-hikari")
Copy to Clipboard

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
datasources.default.dialect: oracle
langchain4j.oracle.embedding-stores.default.table: test
langchain4j.oracle.embedding-stores.default.table.create-option: create_if_not_exists
Copy to Clipboard

4.5 Open Search

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-opensearch")
Copy to Clipboard

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
micronaut.opensearch.rest-client.http-hosts: "http://localhost:9200,http://127.0.0.2:9200"
langchain4j.opensearch.embedding-stores.default.dimension: 384
Copy to Clipboard

4.6 PGVector

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-pgvector")
Copy to Clipboard

Then add one of the supported JDBC connection pools, for example Hikari:

GradleMaven
runtimeOnly("io.micronaut.sql:micronaut-jdbc-hikari")
Copy to Clipboard

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
datasources.default.dialect: postgres
langchain4j.pgvector.embedding-stores.default.table: "mytable"
langchain4j.pgvector.embedding-stores.default.dimension: 384

# Add this if you plan to use testresources
test-resources.containers.postgres.image-name: pgvector/pgvector:pg16
Copy to Clipboard

4.7 Redis

TODO

4.8 Qdrant

Add the following dependency:

GradleMaven
implementation("io.micronaut.langchain4j:micronaut-langchain4j-store-qdrant")
Copy to Clipboard

To use Testcontainers & Test Resources add the following dependency:

GradleMaven
testResourcesService("io.micronaut.langchain4j:micronaut-langchain4j-qdrant-testresource")
Copy to Clipboard

Example Configuration
PropertiesYamlTomlGroovyHoconJSON
# Omitt the following 2 properties if you use Test resources
langchain4j.qdrant.embedding-store.host: localhost
langchain4j.qdrant.embedding-store.port: 6334

# Minimal configuration required for Test resources
langchain4j.qdrant.embedding-store.collection-name: mycollection
Copy to Clipboard

5 Repository

You can find the source code of this project in this repository:

6 Release History

For this project, you can find a list of releases (with release notes) here: