Created
March 17, 2025 07:38
-
-
Save jmesnil/e1989aec6d095952c63a39ab572bc6a7 to your computer and use it in GitHub Desktop.
myaiapp.java
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:1.0.0-beta1 | |
//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