Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
13d5b3b
support github
gkwan-ibm May 26, 2025
a562b88
support github
gkwan-ibm May 26, 2025
c0e9056
support ollama
gkwan-ibm May 28, 2025
3f00bcc
update instructions
gkwan-ibm Jun 9, 2025
0580e4a
Merge branch 'main' into support-github-ollama
gkwan-ibm Jun 9, 2025
bd82716
Bump org.junit.jupiter:junit-jupiter from 5.12.2 to 5.13.0 in /jakart…
dependabot[bot] Jun 9, 2025
a38b6f4
Bump org.slf4j:slf4j-reload4j from 2.0.16 to 2.0.17 in /jakartaee-mic…
dependabot[bot] Jun 9, 2025
3bc3d60
Bump org.jboss.resteasy:resteasy-client from 6.2.11.Final to 6.2.12.F…
dependabot[bot] Jun 9, 2025
445a7a5
Bump io.openliberty.tools:liberty-maven-plugin from 3.11.2 to 3.11.3 …
dependabot[bot] Jun 9, 2025
fe4fe05
Bump org.slf4j:slf4j-api from 2.0.16 to 2.0.17 in /jakartaee-micropro…
dependabot[bot] Jun 9, 2025
2ef5a64
Bump org.eclipse.jetty.websocket:websocket-jakarta-client from 11.0.2…
dependabot[bot] Jun 9, 2025
6288b45
Bump org.apache.maven.plugins:maven-failsafe-plugin from 3.5.2 to 3.5…
dependabot[bot] Jun 9, 2025
ec194cf
Bump org.jboss.resteasy:resteasy-json-binding-provider from 6.2.11.Fi…
dependabot[bot] Jun 9, 2025
22146c5
remve dependabot action
gkwan-ibm Jun 9, 2025
d3e891a
Update ModelResourceIT.java (#19)
AndrewSasmito Jun 9, 2025
f693003
update for Hugging Face
gkwan-ibm Jun 9, 2025
24c64e1
update for Hugging Face
gkwan-ibm Jun 9, 2025
2670d8f
support mistral ai
gkwan-ibm Jun 10, 2025
2039fda
Merge branch 'langchain4j:main' into support-github-ollama
gkwan-ibm Jul 2, 2025
8302ce9
Merge branch 'main' into support-github-ollama
gkwan-ibm Jul 7, 2025
d84e121
Merge branch 'main' into support-github-ollama
gkwan-ibm Aug 12, 2025
b5e11b6
update dependencies and support another github api key
gkwan-ibm Aug 12, 2025
6af64d1
Merge branch 'langchain4j:main' into support-github-ollama
gkwan-ibm Sep 5, 2025
28f115e
update dependencies
gkwan-ibm Sep 5, 2025
a9fd0da
fix github model
gkwan-ibm Sep 24, 2025
f438e47
Merge branch 'langchain4j:main' into support-github-ollama
gkwan-ibm Sep 24, 2025
a8f71cb
update dependencies
gkwan-ibm Sep 24, 2025
107ea0c
Merge branch 'langchain4j:main' into support-github-ollama
gkwan-ibm Oct 21, 2025
39b7d8d
Merge branch 'langchain4j:main' into support-github-ollama
gkwan-ibm Oct 30, 2025
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
68 changes: 56 additions & 12 deletions jakartaee-microprofile-example/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
# LangChain4j in Jakarta EE and MicroProfile
This example demonstrates LangChain4J in a Jakarta EE / MicroProfile application on Open Liberty. The application is a chatbot built with LangChain4J and uses Jakarta CDI, Jakarta RESTful Web Services, Jakarta WebSocket, MicroProfile Config, MicroProfile Metrics, and MicroProfile OpenAPI features.
This example demonstrates LangChain4J in a Jakarta EE / MicroProfile application on Open Liberty. The application is a chatbot built with LangChain4J and uses Jakarta CDI, Jakarta RESTful Web Services, Jakarta WebSocket, MicroProfile Config, MicroProfile Metrics, and MicroProfile OpenAPI features. The application allows to use models from either Github, Ollama, or Hugging Face.

## Prerequisites:

- [Java 21](https://developer.ibm.com/languages/java/semeru-runtimes/downloads)
- Hugging Face API Key
- Sign up and log in to https://huggingface.co.
- Go to [Access Tokens](https://huggingface.co/settings/tokens).
- Create a new access token with `read` role.

- Either one of the following model providers:
- Github
- Sign up and sign in to https://github.com.
- Go to your [Settings](https://github.com/settings/profile)/[Developer Settings](https://github.com/settings/developers)/[Persional access tokens](https://github.com/settings/personal-access-tokens).
- Generate a new token
- Ollama
- Download and install [Ollama](https://ollama.com/download)
- see the [README.md](https://github.com/ollama/ollama/blob/main/README.md#ollama)
- Pull the following models
- `ollama pull llama3.2`
- `ollama pull all-minilm`
- `ollama pull tinydolphin`
- Mistral AI
- Sign up and log in to https://console.mistral.ai/home.
- Go to [Your API keys](https://console.mistral.ai/api-keys).
- Create a new key.
- Hugging Face
- Sign up and log in to https://huggingface.co.
- Go to [Access Tokens](https://huggingface.co/settings/tokens).
- Create a new access token with `read` role.

## Environment Set Up

Expand All @@ -18,10 +33,40 @@ To run this example application, navigate to the `jakartaee-microprofile-exampl
cd langchain4j-examples/jakartaee-microprofile-example
```

Set the following environment variables:

Set the `JAVA_HOME` environment variable:
```
export JAVA_HOME=<your Java 21 home path>
```

Set the `GITHUB_API_KEY` environment variable if using Github.
```
unset HUGGING_FACE_API_KEY
unset OLLAMA_BASE_URL
unset MISTRAL_AI_API_KEY
export GITHUB_API_KEY=<your Github API token>
```

Set the `OLLAMA_BASE_URL` environment variable if using Ollama. Use your Ollama URL if not using the default.
```
unset HUGGING_FACE_API_KEY
unset GITHUB_API_KEY
unset MISTRAL_AI_API_KEY
export OLLAMA_BASE_URL=http://localhost:11434
```

Set the `MISTRAL_AI_API_KEY` environment variable if using Mistral AI.
```
unset GITHUB_API_KEY
unset OLLAMA_BASE_URL
unset MISTRAL_AI_API_KEY
export MISTRAL_AI_API_KEY=<your Mistral AI API key>
```

Set the `HUGGING_FACE_API_KEY` environment variable if using Hugging Face.
```
unset GITHUB_API_KEY
unset OLLAMA_BASE_URL
unset MISTRAL_AI_API_KEY
export HUGGING_FACE_API_KEY=<your Hugging Face read token>
```

Expand Down Expand Up @@ -52,17 +97,16 @@ Use the Maven wrapper to start the application by using the [Liberty dev mode](h

Navigate to the the [OpenAPI UI](http://localhost:9080/openapi/ui) URL for the following 3 REST APIs:

- [HuggingFaceLanguageModel](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/HuggingFaceLanguageModel.java)
- [LanguageModel](https://javadoc.io/doc/dev.langchain4j/langchain4j-core/latest/dev/langchain4j/model/language/LanguageModel.html)
- Expand the GET `/api/model/language` API.
1. Click the **Try it out** button.
2. Type `When was Hugging Face launched?`, or any question, in the question field.
3. Click the **Execute** button.
- Alternatively, run the following `curl` command from a command-line session:
- ```
curl 'http://localhost:9080/api/model/language?question=When%20was%20Hugging%20Face%20launched%3F'

```
- [HuggingFaceChatModel](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/HuggingFaceChatModel.java)
- [ChatModel](https://javadoc.io/doc/dev.langchain4j/langchain4j-core/latest/dev/langchain4j/model/chat/ChatModel.html)
- expand the GET `/api/model/chat` API
1. Click the **Try it out** button.
2. Type `Which are the most used Large Language Models?`, or any question, in the question field.
Expand All @@ -71,7 +115,7 @@ Navigate to the the [OpenAPI UI](http://localhost:9080/openapi/ui) URL for the f
- ```
curl 'http://localhost:9080/api/model/chat?userMessage=Which%20are%20the%20most%20used%20Large%20Language%20Models%3F' | jq
```
- [InProcessEmbeddingModel](https://github.com/langchain4j/langchain4j-embeddings)
- [EmbeddingModel](https://javadoc.io/doc/dev.langchain4j/langchain4j-core/latest/dev/langchain4j/model/embedding/EmbeddingModel.html)
- expand the GET `/api/model/similarity` API
1. Click the **Try it out** button.
2. Type `I like Jakarta EE and MicroProfile.`, or any text, in the the **text1** field.
Expand Down
21 changes: 18 additions & 3 deletions jakartaee-microprofile-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
<artifactId>langchain4j-hugging-face</artifactId>
<version>1.8.0-beta15</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-github-models</artifactId>
<version>1.5.0-beta11</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-ollama</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-mistral-ai</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
Expand All @@ -53,7 +68,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.12.2</version>
<version>5.13.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -77,7 +92,7 @@
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-jakarta-client</artifactId>
<version>11.0.25</version>
<version>11.0.26</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -94,7 +109,7 @@
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>3.11.3</version>
<version>3.11.5</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.langchain4j.example.chat;

import static java.time.Duration.ofSeconds;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import dev.langchain4j.example.chat.util.ModelBuilder;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.huggingface.HuggingFaceChatModel;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;
Expand All @@ -16,24 +15,7 @@
public class ChatAgent {

@Inject
@ConfigProperty(name = "hugging.face.api.key")
private String HUGGING_FACE_API_KEY;

@Inject
@ConfigProperty(name = "chat.model.id")
private String CHAT_MODEL_ID;

@Inject
@ConfigProperty(name = "chat.model.timeout")
private Integer TIMEOUT;

@Inject
@ConfigProperty(name = "chat.model.max.token")
private Integer MAX_NEW_TOKEN;

@Inject
@ConfigProperty(name = "chat.model.temperature")
private Double TEMPERATURE;
private ModelBuilder modelBuilder;

@Inject
@ConfigProperty(name = "chat.memory.max.messages")
Expand All @@ -45,16 +27,9 @@ interface Assistant {

private Assistant assistant = null;

public Assistant getAssistant() {
public Assistant getAssistant() throws Exception {
if (assistant == null) {
HuggingFaceChatModel model = HuggingFaceChatModel.builder()
.accessToken(HUGGING_FACE_API_KEY)
.modelId(CHAT_MODEL_ID)
.timeout(ofSeconds(TIMEOUT))
.temperature(TEMPERATURE)
.maxNewTokens(MAX_NEW_TOKEN)
.waitForModel(true)
.build();
ChatModel model = modelBuilder.getChatModelForWeb();
assistant = AiServices.builder(Assistant.class)
.chatModel(model)
.chatMemoryProvider(
Expand All @@ -64,10 +39,8 @@ public Assistant getAssistant() {
return assistant;
}

public String chat(String sessionId, String message) {
String reply = getAssistant().chat(sessionId, message).trim();
int i = reply.lastIndexOf(message);
return i > 0 ? reply.substring(i) : reply;
public String chat(String sessionId, String message) throws Exception {
return getAssistant().chat(sessionId, message).trim();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ChatMessageEncoder implements Encoder.Text<String> {
@Override
public String encode(String message) throws EncodeException {

if (!message.endsWith(".")) {
if (!message.endsWith(".") && !message.endsWith("!") && !message.endsWith("?")) {
message += " ...";
}

Expand Down
Loading