Integrating OpenAI APIs in Spring Boot with Java
This tutorial will guide you on how to integrate OpenAI APIs, including ChatGPT, into your Spring Boot application. We will explore moderation, embedding, and chat completion requests as part of the process. If you're interested in this topic, you may also want to check out our Building an AI chatbot in Java series.
Before we begin, make sure you have an existing Spring Boot application. The example project used in this tutorial is a Spring Boot application with a React front-end created with Hilla. If you need to set up a new application, you can refer to the instructions provided in the Hilla documentation.
Handling Streaming Responses
In order to handle streaming responses, you need to add the following dependency to your pom.xml
file.

To access OpenAI APIs, you will require an API key. If you don't have one yet, you can create it on the OpenAI platform page. Save the key as an environment variable, OPENAI_API_KEY
. You can set this up in your IDE or system-wide based on your preferences.
All the code related to interacting with OpenAI will be encapsulated in a Spring service class that can be utilized throughout your application. Let's create a new class called OpenAIService.java
:
We will inject the API key environment variable into our service class using the @Value
annotation.
Next, we will leverage Spring WebClient to make REST service calls to the OpenAI API.
Handling Different Types of Requests
The first type of request we will address is moderation requests, which are essential for screening messages to ensure they comply with OpenAI's terms.

These methods moderate the entire chat history by sending each ChatCompletionMessage
to the moderation API. If any message is flagged, it returns false. For further details, you can refer to the Java classes associated with these requests and responses in the project GitHub repository.
The second type of request involves retrieving an embedding vector for a given text, which is useful for similarity searches.

This method calls the embeddings API and returns a vector of double values that can be utilized for conducting a similarity search in a vector database.
The final request type is to call the chat completion API, known as ChatGPT. To handle potentially long response times, we will stream the API response and display the answer as it is generated.
If you want to access the source code for the completed application, it is available on GitHub.
In the upcoming part of our Building an AI chatbot in Java series, we will delve into Using a Pinecone vector database with Spring Boot.

Make the most of Vaadin Pro components, UI test automation tool, and drag-and-drop UI editor to develop professional web applications. Stay ahead with the latest Vaadin resources and tips.