Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created May 8, 2024 11:30
Show Gist options
  • Save maxandersen/a5e668c50c386a8654ad77bb8b5d0b76 to your computer and use it in GitHub Desktop.
Save maxandersen/a5e668c50c386a8654ad77bb8b5d0b76 to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
//DESCRIPTION This is a simple example of using the LangChain4J Quarkus extension to create a simple AI service that answers questions.
//DESCRIPTION This example works with both InstructLab by default, adjust url for Podman AI Lab and remove url for OpenAI.
//DEPS io.quarkus.platform:quarkus-bom:3.9.3@pom
//DEPS io.quarkiverse.langchain4j:quarkus-langchain4j-core:RELEASE
//DEPS io.quarkiverse.langchain4j:quarkus-langchain4j-openai:RELEASE
//JAVAC_OPTIONS -parameters
//JAVA_OPTIONS -Djava.util.logging.manager=org.jboss.logmanager.LogManager
//JAVA_OPTIONS -Dquarkus.langchain4j.openai.base-url=http://localhost:8000/v1
//JAVA_OPTIONS -Dquarkus.langchain4j.openai.api-key=sk-dummy
//JAVA_OPTIONS -Dquarkus.langchain4j.openai.timeout=60s
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkus.logging.Log;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
@QuarkusMain
public class demo implements QuarkusApplication {
@RegisterAiService
public interface AiService {
@SystemMessage("You are a helpful assistant")
@UserMessage("{question}")
String request(String question);
}
@Inject
AiService ai;
@Override
@ActivateRequestContext
public int run(String... args) throws Exception {
Log.info(ai.request("What is the capital of Denmark?"));
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment