Integrating Large Language Models into Java Applications
Integrating large language models (LLMs) into Java applications opens the door to a plethora of powerful capabilities, from intelligent automation to sophisticated conversational interfaces. In this guide, we’ll explore how to use LangChain4j and OpenAI to create a simple yet effective command-line interface (CLI)-based Java application. Think of this as the "Hello World" of Java and AI/LLM integration.
Obtain an OpenAI API Key
Acquiring your OpenAI API key is quite straightforward. Just follow these steps:
- Sign up or log in. Go to the OpenAI website and create an account or log in if you already have one.
- Navigate to the API Keys section. Find the "API Keys" section in your account dashboard.
- Generate and securely store your API key. Click the button to generate a new API key. Be sure to store it in a secure location, as you won’t be able to view it again.
Set Up Your Environment
Next, store your API key securely as an environment variable. Depending on your operating system, the steps will vary slightly:
- On Windows: Navigate to System Properties > User Variables > Add a new variable named
OPENAI_API_KEY. - On macOS or Linux: Open your terminal and add
export OPENAI_API_KEY='your-api-key'to your.bashrcor.zshrcfile.
Simple CLI Example: The ‘Hello World’ of Java and LLM
Let’s dive into a minimal example demonstrating how to use Java and OpenAI’s GPT model through a command-line interface. We will employ Java’s built-in HTTP client to make API requests to OpenAI.
java
package ca.bazlur;
import java.net.http.*;
import java.net.URI;
import java.time.Duration;
public class Main {
private static final String API_URL = "https://api.openai.com/v1/chat/completions";
public static void main(String[] args) throws Exception {
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null || apiKey.isBlank()) {
System.err.println("Error: OPENAI_API_KEY environment variable not set.");
System.exit(1);
}
if (args.length == 0 || args[0].isBlank()) {
System.err.println("Error: Please provide a prompt.");
System.exit(1);
}
String prompt = args[0];
String requestBody = """
{
"model": "gpt-4",
"messages": [{"role": "user", "content": "%s"}],
"temperature": 0.7,
"max_tokens": 150
}
""".formatted(prompt);
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.timeout(Duration.ofSeconds(30))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("ChatGPT Response: " + response.body());
}
}
}
Breakdown of the Code
In this example, we build the request body as a JSON string, specifying the model (gpt-4), user message, temperature, and maximum number of tokens.
- The
modelparameter indicates which OpenAI model to use. GPT-4 is a robust and versatile option. - The
messagesarray contains conversation history, and theroleparameter identifies who sent the message.
Two crucial parameters to control the output are temperature and max_tokens:
-
Temperature ranges from 0 to 1 and influences the randomness of the generated text. A higher temperature results in a more creative and unpredictable output, while a lower temperature yields more conservative responses.
- Max Tokens limits the length of the generated text, which is essential for both cost management and preventing excessively lengthy responses.
Running the Application
To run this code:
- Save it as
Main.java. - Execute the command:
java Main.java "What is the capital of Toronto?".
You should receive a JSON response containing information such as usage, the generated model, and the response content from ChatGPT.
Integrating with LangChain4j (OpenAI SDK)
While the previous example directly interacts with the OpenAI API, manually constructing HTTP requests and parsing JSON responses can become tedious and error-prone for complex applications.
Enter LangChain4j, a Java-native library that simplifies this process. It provides a high-level, intuitive API to interact with LLMs like OpenAI’s GPT models, abstracting the complexities of API calls so developers can focus on the app’s logic.
Setting Up LangChain4j
To set up a simple Maven project with dependencies, include the following in your pom.xml:
xml
For a Gradle project, you would include:
gradle
implementation ‘dev.langchain4j:langchain4j:1.0.0-beta2’
implementation ‘dev.langchain4j:langchain4j-open-ai:1.0.0-beta2’
Example with LangChain4j
Let’s create a Java class using LangChain4j to communicate with OpenAI:
java
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
public class LLMExample {
public static void main(String[] args) {
ChatLanguageModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4")
.temperature(0.7)
.maxTokens(150)
.build();
String response = model.chat("What is the capital of Toronto?");
System.out.println(response);
}
}
In this code, we create an instance of OpenAiChatModel using the builder pattern to set the API key, model name, temperature, and max tokens. Calling the chat() method sends the user’s prompt and returns the generated text, which is then printed to the console.
Running the LangChain4j Example
To run this example, follow these steps:
- Save the code as
LLMExample.javain thesrc/main/javadirectory of your Maven or Gradle project. - Compile and run the code using Maven:
mvn compile exec:java -Dexec.mainClass="LLMExample".
Alternatively, if you’re using Gradle, execute: ./gradlew run.
You should see a response from ChatGPT printed in your console.
LangChain4j and Other AI Models
Besides OpenAI, LangChain4j supports integration with other LLMs, allowing developers to experiment and choose the best fit for their needs. For instance, if you want to use Google’s Gemini API, you can adjust the code like so:
java
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
public class LLMGeminiExample {
public static void main(String[] args) {
ChatLanguageModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("GOOGLE_API_KEY"))
.baseUrl("https://generativelanguage.googleapis.com/v1beta/openai/")
.modelName("gemini-2.0-flash")
.temperature(0.7)
.maxTokens(150)
.build();
String response = model.chat("What is the capital of Toronto?");
System.out.println(response);
}
}
You must obtain the Gemini API key, which can be acquired from Google AI Studio. Again, store this in an environment variable.
This setup is uniform, allowing for seamless transitions between different models with minimal adjustments.
The source code for these Java and OpenAI LLM examples is also available on GitHub.
What to Do Next with Java, OpenAI and LLMs
Now that you’ve created your first Java and LLM application, the world of AI integration is yours to explore! Here are suggestions for further exploration:
- Build a chatbot. Create a simple chatbot to answer user questions on a specific topic.
- Implement text summarization. Use an LLM to summarize long articles or documents efficiently.
- Generate creative content. Experiment with LLMs to produce poems, stories, or snippets of code.
- Explore different AI models. Experiment with other LLMs, such as the Anthropic API.
With some practice and confidence, you will be well-equipped to delve into advanced topics surrounding Java and AI, such as optimizations and practical LLM implementations in Java.
— A N M Bazlur Rahman is a Java Champion and staff software developer at DNAstack. He also founded and moderates the Java User Group in Bangladesh.