Get started with Spring AI and OpenAI


Introduction
In this article we look at how to get started with Spring AI project and connect to OpenAI Large Language Models (LLMs).
Feel free to browse other articles in this getting started with Spring AI series:
How to get started with Spring AI (with Docker Model Runner)
Create a simple Spring Application (Beginners)
We can use Spring Initializr to generate a skeleton project for our simple application. You can do that either in IDE itself, or directly with a pre-configured project (e.g. Maven, Java, Spring Boot 3.4.4, spring-ai-demo, com.example package name. JAR packaging, Java 24 with Spring Web).
Clicking on “Generate” button will download a project skeleton zip archive, e.g. spring-ai-demo.zip
which you can unzip and open in editor of your choice (e.g. IntelliJ, VS Code, etc.)
We can start adding specific AI model Spring Boot starters later on.
Let’s add a HomeController
, e.g.
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/")
public String home() {
return "Hello World!";
}
}
Let’s run our simple application, e.g.
./mvnw spring-boot:run
Feel free to browse from another terminal window, e.g. http localhost:8080
http localhost:8080
HTTP/1.1 200
Connection: keep-alive
Content-Length: 12
Content-Type: text/plain;charset=UTF-8
Date: Tue, 11 Apr 2025 08:00:00 GMT
Keep-Alive: timeout=60
Hello World!
or a directly in the browser at http://localhost:8080
Feel free to explore Spring Boot Getting Started Guide for more details on how to create a simple Spring Boot web application.
Connecting your Spring Application to OpenAI models
Configure OpenAI
In this section we will explore connecting your application to OpenAI Large Language Models (LLMs). If you don’t already have an account on OpenAI platform, please sign up for an account, e.g.
Once you have created and authenticated your account, you can create a new API Key,
Please save the key (e.g. sk-proj-qqZJqCSCbI4xoXtB4Yn1aLWUq99...
), as you won’t be able to view it again. Keep it secure, because anyone with your API key can access and consume your OpenAI API platform credits.
Finally, let’s add Spring AI code to start using OpenAI API.
Add Spring AI for OpenAI
We will add Spring AI Spring Boot Starter for OpenAI in our Maven pom.xml
, e.g.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.0.0-M7</version>
</dependency>
Notice, we are using version 1.0.0-M7
that’s newest at the time of writing this article. Please replace with the latest Spring AI version.
We also need to add few Spring properties to our src/main/resources/application.properties
file, e.g.
spring.application.name=spring-ai-demo
spring.ai.openai.api-key=sk-proj-qqZJqCSCbI4xoXtB4Yn1aLWUq99...
spring.ai.openai.chat.options.model=gpt-4o-mini
Notice, we have used gpt-4o-mini
large language model (LLM) for this simple use case. Feel free to explore all OpenAI LLM models. These models vary in size, speed, training data and eventually in price per 1M tokens. For example, gpt-4o-mini
model costs per 1M tokens are: (1) for input US$0.15, (2) for cached input US$0.075 and (3) for output US$0.60, whereas gpt-4o
model costs per 1M tokens are: (1) for input US$2.50, (2) for cached input US$1.25 and (3) for output US$10.00.
Finally, let’s add a call to a LLM from our HomeController
, e.g.
package com.example;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
private ChatClient chatClient;
public HomeController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/")
public String home() {
return chatClient
.prompt()
.user("who are you")
.call()
.content();
}
}
Run the application again, e.g.
./mvnw spring-boot:run
Test the application, in another terminal or browser, e.g.
http localhost:8080
HTTP/1.1 200
Connection: keep-alive
Content-Length: 227
Content-Type: text/plain;charset=UTF-8
Date: Tue, 11 Apr 2025 08:05:00 GMT
Keep-Alive: timeout=60
I am an AI language model created by OpenAI, designed to assist with a
variety of questions and tasks. I can provide information, answer
questions, and engage in conversation on a wide range of topics.
How can I help you today?
🏆 Congratulations! You have created your first application that talks to an OpenAI LLM service.
Possible Gotcha’s
If you run into Incorrect API key provided
(see below example) - this is probably because you have mistyped the OpenAI API key.
org.springframework.ai.retry.NonTransientAiException: 401 - {
"error": {
"message": "Incorrect API key provided: \"sk-proj********\".
You can find your API key at
https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
Also, you might run into Insufficient quota
(see below example) - this is probably because you have no funds available in your OpenAI account.
org.springframework.ai.retry.NonTransientAiException: 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"
}
}
Check that you have some funds available in your account, e.g.
You might need to set the billing and add $5 to get started, e.g.
Hope you enjoyed this beginner’s getting started with Spring AI article!
Connecting to other LLMs
If you want to run your Spring AI application with other LLM models, look at our articles:
References
Subscribe to my newsletter
Read articles from Neven Cvetkovic directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
