OpenAI integration with Spring Boot

Artificial Intelligence (AI) is rapidly transforming the landscape of modern applications, and the Spring Framework has been keeping pace with this revolution. With the introduction of spring-ai, Spring now offers seamless integration with popular AI models like OpenAI’s GPT series, Anthropic’s Claude, and CoPilot. In this blog post, we will explore how to integrate OpenAI with a Spring Boot application and interact with OpenAI’s GPT-3.5 Turbo model using the newly introduced Spring AI abstraction.


Getting Started

To begin, we need to create a new Spring Boot project. The easiest way to do this is by visiting Spring Initializr.

Select the following options:

Dependencies: Spring Web, Spring AI

Project: Maven Project

Language: Java

Spring Boot Version: 3.x.x

Adding Dependencies


Ensure your pom.xml includes the Spring AI dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.java2practice.ai</groupId>
    <artifactId>chat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>chat</name>
    <description>Java based AI Chat project</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0-RC1</spring-ai.version>
    </properties>
    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring AI -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-openai</artifactId>
        </dependency>
        <!-- Spring Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Creating the Chat Controller


Now, let’s create a ChatController.java to expose an endpoint /ai that accepts user input as a parameter and sends it to OpenAI’s API.

package com.java2practice.ai.chat.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    private final ChatClient chatClient;

    @Autowired
    public ChatController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/ai")
    String generation(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .call()
                .content();
    }
}

Understanding the Spring AI Abstraction

Spring AI introduces a ChatModel abstraction that wraps around multiple AI models, enabling seamless integration. The call() method allows interaction with the model using plain strings, messages, or a Prompt object.

public interface ChatModel extends Model<Prompt, ChatResponse> {

	default String call(String message) {...}
        default String call(Message... messages) {...}

    @Override
	ChatResponse call(Prompt prompt);
}

What is a Prompt?


A Prompt in Spring AI encapsulates multiple Message objects and provides contextual information for the model to generate a response.

For this example, we are using Open AI implementation of ChatModel – https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html

Configuring OpenAI in application.properties


Add the following properties to your application.properties file:

// application.properties

spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.7

Replace ${OPENAI_API_KEY} with your OpenAI API Key, which you can obtain from OpenAI API.

Parameter Explanation:

  • model: Defines the OpenAI model to use (e.g., gpt-3.5-turbo).
  • temperature: Controls the randomness of responses. Values closer to 1 make the responses more diverse, while values closer to 0 make them more focused.

Running the Application

Start your Spring Boot application and visit:

http://localhost:8080/ai?userInput=Hello, how are you?

If your OpenAI API key is valid and you have sufficient credits, you should receive a response from OpenAI’s GPT-3.5 Turbo model.

If you do not have credits, you would receive insffuicient_quota error

2025-05-18T23:18:08.318-05:00  WARN 46884 --- [nio-8080-exec-1] o.s.a.r.a.SpringAiRetryAutoConfiguration : Retry error. Retry count: 1, Exception: HTTP 429 - {
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
        "type": "insufficient_quota",
        "param": null,
        "code": "insufficient_quota"
    }
}

Code is available at https://github.com/rameshcharykotha/Java_AI_Practice

Leave a comment