This example is based on a LangChain4j tutorial. It produces a GraalVM native version of a chatbot leveraging LangChain4j and the OpenAI API. It also uses gpt-4o
, which is supposed to produce quick and accurate results, but you can use other models as well.
-
GraalVM (
sdk install java 22.0.2-graal
) -
Maven
-
(optional) OPENAI API key
Note
|
To interact with the OpenAI API, you need to either get and store an OPENAI API key in an environment variable OPENAI_API_KEY , or replace the value with demo .
|
You can now chat with the bot. You can execute the jar directly:
$ java -jar ./target/native-langchain4j-1.0-jar-with-dependencies.jar "your prompt"
Or use the script from the repo to run:
$ ./run-jvm.sh "what is Java?"
"Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java is widely used for developing web applications, software, and mobile applications."
$ ./run-jvm.sh "what is GraalVM?"
"GraalVM is a high-performance runtime that provides significant improvements in application performance and efficiency. It is designed for applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Groovy, Kotlin, and LLVM-based languages such as C and C++. GraalVM offers capabilities like ahead-of-time compilation and the ability to compile to a native executable to improve startup time, reduce memory footprint, and enable distribution of pre-compiled executables. It also supports interoperability between different programming languages, allowing you to write polyglot applications."
$ ./run-jvm.sh "what is love?"
"Love is a complex set of emotions, behaviors, and beliefs associated with strong feelings of affection, protectiveness, warmth, and respect for another person. It can also be described as a profound, passionate affection for another person and can emerge in various forms, such as familial love, romantic love, platonic love, and self-love. Love can also be understood as a function to keep human beings together against threats and to facilitate the continuation of the species."
This demo works out of the box, but to get rid of a warning from a 4th party dependency, build it with config files (come with the repo):
native-image -jar ./target/native-langchain4j-1.0-jar-with-dependencies.jar -o native-langchain4j -H:+AllowDeprecatedBuilderClassesOnImageClasspath
Alternatively, you can run the build script: ./build-native.sh
.
This will produce a nice self-contained native version of the app:
$ eza -l
.rwxr-xr-x 44M ayurenko 16 Mar 16:45 native-langchain4j
You can now chat with the native app:
$ ./native-langchain4j "what's the answer to life, the universe, and everything?"
42, according to "The Hitchhiker's Guide to the Galaxy" by Douglas Adams