Using the OpenAI Large Language Model APIs in Java
Large Language Models (LLMs) signify a significant leap forward in the progression of artificial intelligence, with a pronounced impact on the field of natural language processing (NLP), data transformation, translation, and serve as a source of real world knowledge in AI applications. They are trained on vast corpora of text data (literally most published books and most of the web), learning to predict subsequent words in a sequence, which imbues them with the ability to generate human-like text, comprehend the semantics of language, and perform a variety of language-related tasks. The architecture of these models, typically based on deep learning paradigms such as Transformer, empowers them to encapsulate intricate patterns and relationships within language. These models are trained utilizing substantial computational resources.
The utility of LLMs extends across a broad spectrum of applications including but not limited to text generation, translation, summarization, question answering, and sentiment analysis. Their ability to understand and process natural language makes them indispensable tools in modern AI-driven solutions. However, with great power comes great responsibility. The deployment of LLMs raises imperative considerations regarding ethics, bias, and the potential for misuse. Moreover, the black-box nature of these models presents challenges in interpretability and control, which are active areas of research in the quest to make LLMs more understandable and safe. The advent of LLMs has undeniably propelled the field of NLP to new heights, yet the journey towards fully responsible and transparent utilization of these powerful models is an ongoing endeavor.
In the development of practical AI systems, LLMs like those provided by OpenAI, Anthropic, and Hugging Face have emerged as pivotal tools for numerous applications including natural language processing, generation, and understanding. These models, powered by deep learning architectures, encapsulate a wealth of knowledge and computational capabilities. Here we look at the basics for getting you, dear reader, started using the OpenAI APIs for text completion tasks in Java code. In the next chapter we do the same ex pet we will run local LLMs on our laptops using the Ollama platform.
Java Library to Use OpenAI’s APIs
The library code defined in the directory Java-AI-Book-Code/openai-llm-client is designed to interact with the OpenAI API to accept a prompt string and get a text completion. Here’s a breakdown of what each part of the code does:
The getCompletion method performs the following steps:
- Client Initialization: We reuse a single client instance of
OpenAIClient, initialized viaOpenAIOkHttpClient.fromEnv(), which automatically pulls the API key from the standard environment variables. - Request Parameters Construction: Builds a
ChatCompletionCreateParamsobject using its builder. We add the user message (the prompt) and set the model toChatModel.GPT_5_MINI. - Sending the Request: Calls
client.chat().completions().create(params)which synchronously sends the request via OKHttp and receives the chat completion response. - Parsing the Response: Extracts the first choice’s message content using
chatCompletion.choices().get(0).message().content().orElse("")and returns it.
1 package com.markwatson.openai;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6
7 import com.openai.client.OpenAIClient;
8 import com.openai.client.okhttp.OpenAIOkHttpClient;
9 import com.openai.models.ChatModel;
10 import com.openai.models.chat.completions.ChatCompletion;
11 import com.openai.models.chat.completions.ChatCompletionCreateParams;
12
13 public class OpenAICompletions {
14
15 // Reuse a single client instance (connection pool + thread pool efficiency)
16 private static final OpenAIClient client = OpenAIOkHttpClient.fromEnv();
17
18 public static void main(String[] args) {
19 var prompt = "Translate the following English text to French: 'Hello, how are you?'";
20 var completion = getCompletion(prompt);
21 System.out.println("completion: " + completion);
22 }
23
24 public static String getCompletion(String prompt) {
25 System.out.println("prompt: " + prompt);
26
27 var params = ChatCompletionCreateParams.builder()
28 .addUserMessage(prompt)
29 .model(ChatModel.GPT_5_MINI)
30 .build();
31
32 ChatCompletion chatCompletion = client.chat().completions().create(params);
33
34 var content = chatCompletion.choices().get(0).message().content().orElse("");
35 System.out.println(content);
36 return content;
37 }
38
39
40 /***
41 * Utilities for using the OpenAI API
42 */
43
44 // read the contents of a file path into a Java string
45 public static String readFileToString(String filePath) throws IOException {
46 return Files.readString(Path.of(filePath));
47 }
48
49 public static String replaceSubstring(String originalString, String substringToReplace, String replacementString) {
50 return originalString.replace(substringToReplace, replacementString);
51 }
52 public static String promptVar(String prompt0, String varName, String varValue) {
53 String prompt = replaceSubstring(prompt0, varName, varValue);
54 return replaceSubstring(prompt, varName, varValue);
55 }
56 }
In the next section we write a unit test for this Java class to demonstrate text completion.
Example Applications
There is a unit test provided with this library that shows how to call the completion API:
1 var result = OpenAICompletions.getCompletion(
2 "Translate the following English text to French: 'Hello, how are you?'");
3 System.out.println("completion: " + result);
Sample output is:
1 $ make
2 mvn test -q # run test in quiet mode
3 prompt: Translate the following English text to French: 'Hello, how are you?'
4 completion: Bonjour, comment vas-tu ?
For reference, the JSON response object returned from the OpenAI completion API looks like this:
1 {"id": "chatcmpl-7LbgN6PJxHAfycOuHmGkw8nbpQMm1","object": "chat.completion","created": 1714936767,"model": "gpt-3.5-turbo-0125","choices": [{"index": 0,"message": {"role": "assistant","content": "Bonjour, comment vas-tu ?"},"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 22,"completion_tokens": 7,"total_tokens": 29},"system_fingerprint": "fp_b410720239"}
Extraction of Facts and Relationships from Text Data
Traditional methods for extracting email addresses, names, addresses, etc. from text included the use of hand-crafted regular expressions and custom software. LLMs are text processing engines with knowledge of grammar, sentence structure, and some real world embedded knowledge. Using LLMs can reduce the development time of information extraction systems.
The template we will use if in the file Java-AI-Book-Code /prompts/two-shot-2-var.txt:
1 Given the two examples below, extract the names, addresses, and email addresses of individuals mentioned later as Process Text. Format the extracted information in JSON, with keys for "name", "address", and "email". If any information is missing, use "null" for that field. Be very concise in your output by providing only the output JSON.
2
3 Example 1:
4 Text: "John Doe lives at 1234 Maple Street, Springfield. His email is johndoe@example.com."
5 Output:
6 {
7 "name": "John Doe",
8 "address": "1234 Maple Street, Springfield",
9 "email": "johndoe@example.com"
10 }
11
12 Example 2:
13 Text: "Jane Smith has recently moved to 5678 Oak Avenue, Anytown. She hasn't updated her email yet."
14 Output:
15 {
16 "name": "Jane Smith",
17 "address": "5678 Oak Avenue, Anytown",
18 "email": null
19 }
20
21 Process Text: "{input_text}"
22 Output:
The example code for this section is in a Java Unit Test method testTwoShotTemplate():
1 var inputText = "Mark Johnson enjoys living in Berkeley California at 102 Dunston Street and use mjess@foobar.com for contacting him.";
2 var prompt0 = OpenAICompletions.readFileToString("../prompts/two-shot-2-var.txt");
3 System.out.println("prompt0: " + prompt0);
4 var prompt = OpenAICompletions.promptVar(prompt0, "{input_text}", inputText);
5 System.out.println("prompt: " + prompt);
6 var result = OpenAICompletions.getCompletion(prompt);
7 System.out.println("two shot extraction completion: " + result);
The output looks like:
1 two shot extraction completion:
2 {
3 "name": "Mark Johnson",
4 "address": "102 Dunston Street, Berkeley California",
5 "email": "mjess@foobar.com"
6 }
Using LLMs to Summarize Text
LLMs bring a new level of ability to text summarization tasks. With their ability to process massive amounts of information and “understand” natural language, they’re able to capture the essence of lengthy documents and distill them into concise summaries. Two main types of summarization dominate with LLMs: extractive and abstractive. Extractive summarization pinpoints the most important sentences within the original text, while abstractive summarization requires the LLM to paraphrase or generate new text to represent the core ideas. If you are interested in extractive summarization there is a chapter on this topic in my Common Lisp AI book (link to read online).
Here we use the prompt template file Java-AI-Book-Code/prompts/summarization_prompt.txt:
1 Summarize the following text: "{input_text}"
2 Output:
The example code is in the Java Unit Test testSummarization():
1 var inputText = "Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[ and is on average the third-brightest natural object in the night sky after the Moon and Venus.";
2 var prompt0 = OpenAICompletions.readFileToString("../prompts/summarization_prompt.txt");
3 System.out.println("prompt0: " + prompt0);
4 var prompt = OpenAICompletions.promptVar(prompt0, "{input_text}", inputText);
5 System.out.println("prompt: " + prompt);
6 var result = OpenAICompletions.getCompletion(prompt);
7 System.out.println("summarization completion: " + result);
The output is:
1 summarization completion:
2
3 Jupiter is the largest planet in the Solar System and the fifth from the Sun. It is a gas giant with a mass one-thousandth that of the Sun. It is visible to the naked eye and has been known since ancient times. Jupiter is named after the Roman god Jupiter and is the third-brightest natural object in the night sky.