
How to Connect OpenAI in Java Using Spring AI: Step-by-Step Guide
Spring AI is a powerful application framework designed for AI engineering, offering seamless integration with major AI model providers such as OpenAI, Anthropic, Microsoft, and Google. It enables developers to connect enterprise data and APIs with AI models efficiently while adhering to Spring's design principles of portability and modularity.
In this guide, we will walk through the process of integrating OpenAI into a Java application using Spring AI, leveraging its ChatClient API for AI interactions.
Boost your Java and AI expertise by earning the latest Java certifications and AI certifications.
Prerequisites
Before getting started, ensure you have:
- Java 17 or later installed
- Maven or Gradle as the build tool
- An OpenAI API key
- A basic understanding of Spring Boot
Step 1: Create a Spring Boot Web Application
The easiest way to bootstrap a Spring Boot application with Spring AI is by using Spring Initializr:
- Open Spring Initializr.
- Select the latest version of Spring Boot.
- Add the
spring-ai-openai
dependency. - Generate the project and import it into your preferred IDE.
Alternatively, you can manually add the dependency to your pom.xml
(for Maven users):
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai</artifactId>
<version>1.0.0</version>
</dependency>
For Gradle users, add the following to build.gradle
:
dependencies {
implementation 'org.springframework.ai:spring-ai-openai:1.0.0'
}
Step 2: Configure OpenAI API Key
In your application.properties
or application.yml
file, add the OpenAI API key:
spring.ai.openai.api-key=YOUR_OPENAI_API_KEY
This key is necessary to authenticate and communicate with OpenAI’s API.
Step 3: Implement OpenAI Chat Client
To interact with OpenAI, we need to configure a ChatClient
bean and execute a simple API call. Modify your main application class (SpringAiDemoApplication
) as follows:
import org.springframework.ai.chat.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringAiDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiDemoApplication.class, args);
}
@Bean
public CommandLineRunner runner(ChatClient.Builder builder) {
return args -> {
ChatClient chatClient = builder.build();
String response = chatClient.prompt("Tell me a joke").call().content();
System.out.println(response);
};
}
}
Step 4: Run the Application
To start the application, navigate to the project directory and execute the following command:
./mvnw spring-boot:run
Or, if using Gradle:
./gradlew bootRun
Once the application starts, it will call OpenAI’s API and print the response (a joke in this case) in the console.
Conclusion
Spring AI simplifies the integration of OpenAI into Java applications, making it easy to communicate with AI models using a portable and structured API. With just a few steps, you can set up a fully functional AI-powered chat client.
You can further extend this example by:
- Integrating a web-based UI
- Using OpenAI embeddings for intelligent search
- Storing chat history in a vector database
Explore the Spring AI documentation for more advanced use cases and features. Happy coding!
Author | JEE Ganesh | |
Published | 7 months ago | |
Category: | Artificial Intelligence | |
HashTags | #Java #Programming #Architecture #AI #ArtificialIntelligence |