Last active
March 17, 2025 09:54
-
-
Save jmesnil/4e4b7e44ec8174a79c85fe0c1c18e0d7 to your computer and use it in GitHub Desktop.
Simple AI Chatbot with WildFly & JBang
This file contains hidden or 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
| ///usr/bin/env jbang "$0" "$@" ; exit $? | |
| //DEPS org.wildfly.bom:wildfly-expansion:35.0.1.Final@pom | |
| //DEPS org.wildfly.glow:wildfly-glow:1.3.2.Final | |
| //DEPS jakarta.ws.rs:jakarta.ws.rs-api | |
| //DEPS jakarta.enterprise:jakarta.enterprise.cdi-api | |
| //DEPS dev.langchain4j:langchain4j-bom:1.0.0-beta2@pom | |
| //DEPS dev.langchain4j:langchain4j | |
| //GLOW --spaces=incubating | |
| // Do not forget to first pull the Ollama model with | |
| // | |
| // podman run -d --rm --name ollama -p 11434:11434 -v ollama:/root/.ollama ollama/ollama | |
| // podman exec -it ollama ollama pull llama3.2:1b | |
| // | |
| // and to use the env var OLLAMA_CHAT_MODEL_NAME=llama3.2:1b | |
| import dev.langchain4j.data.message.*; | |
| import dev.langchain4j.model.chat.ChatLanguageModel; | |
| import jakarta.enterprise.context.RequestScoped; | |
| import jakarta.inject.*; | |
| import jakarta.ws.rs.*; | |
| import jakarta.ws.rs.core.*; | |
| @ApplicationPath("/") | |
| public class myaiapp extends Application { | |
| @Path("/chat") | |
| @RequestScoped | |
| public static class Chat { | |
| @Inject | |
| @Named(value = "ollama") | |
| ChatLanguageModel chatModel; | |
| @GET | |
| @Produces(MediaType.TEXT_HTML) | |
| public String chatWithAssistant(@DefaultValue("Orange") @QueryParam("word") String word) throws Exception { | |
| return chatModel.chat( | |
| SystemMessage | |
| .from(""" | |
| You are a teacher that explain to kids the origin of some words. | |
| Your response must be polite, use the same language as the question, and be relevant to the question. | |
| Your answer must be embedded in HTML. | |
| """), | |
| UserMessage.from(String.format("What is the etymology of %s?", word))) | |
| .aiMessage().text(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment