Kickstart Your Java Journey: A Guide to Integrating LLMs with AI - Tech Digital Minds
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.
Acquiring your OpenAI API key is quite straightforward. Just follow these steps:
Next, store your API key securely as an environment variable. Depending on your operating system, the steps will vary slightly:
OPENAI_API_KEY.export OPENAI_API_KEY='your-api-key' to your .bashrc or .zshrc file.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());
} }
}
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.
model parameter indicates which OpenAI model to use. GPT-4 is a robust and versatile option.messages array contains conversation history, and the role parameter 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.
To run this code:
Main.java.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.
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.
To set up a simple Maven project with dependencies, include the following in your pom.xml:
xml
dev.langchain4j
langchain4j
1.0.0-beta2
dev.langchain4j
langchain4j-open-ai
1.0.0-beta2
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’
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.
To run this example, follow these steps:
LLMExample.java in the src/main/java directory of your Maven or Gradle project.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.
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.
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:
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.
Unlocking the Full Potential of Your Pixel 10 Series Phone Google has just released its…
A New Era in AI Collaboration: 2026 and Beyond From Passive Observer to Active Participant…
Transformative Gadgets: The Best of What’s New in 2025 In a world where technology continuously…
Cybersecurity Week in Review: Key Highlights and Insights As the digital landscape continually evolves, so…
Navigating the Complex Landscape of Data Protection in Bangladesh The Value of Personal Data in…
Navigating the Cybersecurity Landscape in 2026: Strategies for Defense Against Evolving Threats As we edge…