This is a Java command-line chatbot application that leverages LangChain4j and the OPENAI API model "gpt-4".
LangChain4j is a Java version of LangChain to simplify integrating AI/LLM capabilities into Java applications.
The example is based on a LangChain4j tutorial. This application is enhanced with GraalVM Native Image Maven plugin that enables you to easily create a native executable for a Java application.
The goal of this project is to demonstrate:
- how easy it is to ahead-of-time compile a CLI application that uses the LLM
- the use of Profile-guided optimizations and SBOM Native Image features
- GraalVM JDK (
sdk install java 21.0.2-graal
) - Maven
- 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
.
-
Clone this repository with Git and then enter it:
git clone https://github.com/olyagpl/langchain4j-searchbot.git
cd langchain4j-searchbot
-
Compile and create a JAR file with all dependencies (required for Native Image):
$ mvn clean package
-
Create a native executable using the Maven plugin for Native Image:
$ mvn -Pnative package
This produces a Linux self-contained executable version of the seachbot!
-
Interact with the seachbot:
$ ./target/searchbot “Why should I visit Lviv?”
Profile-Guided Optimizations is the technique fetching the profiling information to the AOT compiler to improve the performance and file size of a native executable.
-
Build an “instrumented” executable and run it to gather profiles:
$ native-image -jar ./target/searchbot-1.0-jar-with-dependencies.jar --pgo-instrument -o ./target/searchbot-instrumented
$ ./target/searchbot-instrumented “What is the weather today?”
-
Build a PGO-optimized executable that will pick up the gathered profiles, and run:
$ native-image -jar ./target/searchbot-1.0-jar-with-dependencies.jar --pgo -o ./target/searchbot-pgo-optimized
$ ./target/searchbot-pgo-optimized “What is JavaDay Lviv”