Skip to content

Instantly share code, notes, and snippets.

@ehsavoie
Forked from jmesnil/myaiapp.java
Last active March 17, 2025 08:06
Show Gist options
  • Select an option

  • Save ehsavoie/b47977b4f3df3321869bc735f576c7bd to your computer and use it in GitHub Desktop.

Select an option

Save ehsavoie/b47977b4f3df3321869bc735f576c7bd to your computer and use it in GitHub Desktop.
myaiapp.java
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.wildfly.bom:wildfly-expansion:35.0.1.Final@pom
//DEPS org.wildfly:wildfly-ai-bom:1.0.0-SNAPSHOT@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
//GLOW --spaces=incubating
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) {
try {
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();
} catch (Exception e) {
return "My failure reason is:\n\n" + e.getMessage();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment