AI is evolving at a breathtaking pace, with frameworks like LangChain quickly becoming the go-to choice for building applications powered by large language models (LLMs).
However, much of this innovation has been centered around Python, leaving many Java developers watching from the sidelines.
But now, Java developers can finally join the action with Spring AI.
1.What is Spring AI ?
Spring AI is a Spring project designed to make it easy for Java developers to integrate Large Language Models (LLMs) into their applications.
It is inspired by Python projects like LangChain and LlamaIndex, but it is not a direct port of those projects.
If you’re already familiar with Spring Boot, you’ll find the programming model for Spring AI very natural.
Think of it as the Spring Boot of AI – it provides a familiar, opinionated programming model that hides the complexity of working directly with AI/LLM providers.
2.Prerequisites
- Java 17+ installed
- Maven or Gradle build tool
- An IDE like IntelliJ IDEA or VS Code
- An API key from an AI provider (e.g., OpenAI)
3.Hello World with Spring AI
What we are going to build is a simple chat REST endpoint that forwards the user’s prompt to the AI model and returns the model’s response.
3.1 Setting up the Project
Create a new Spring Boot project using Spring Initializer with the following dependencies:
- Spring Web
- Spring AI
For example, in pom.xml :
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
</dependencies>
3.2 Configuring Spring AI
In your application.properties, configure your AI provider:
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o-mini
Make sure to export your API key into an environment variable:
export OPENAI_API_KEY=xxxxxxx
3.3 Creating the Chat Controller
Now, let’s write a simple REST controller that sends a prompt to the AI model and returns the response.
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
private final OpenAiChatClient chatClient;
public HelloWorldController(OpenAiChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ask-ai")
public String askAi(@RequestParam String prompt) {
// Send the user's prompt directly to the AI model
return chatClient.call(prompt);
}
}
Here’s what happens:
- We inject the OpenAiChatClient provided by Spring AI.
- When a user hits /ask-ai with a prompt, it sends to the AI model.
- The model responds, and we get the response.
3.4 Creating the Main Application
Now, let us create a classic Spring Boot Application.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiHelloApplication.class, args);
}
}
3.5 Running the Application
Run the Spring Boot Application:
./mvnw spring-boot:run
Then open a browser or use curl to test:
curl “http://localhost:8080/ask-ai?prompt=Say hello to Bob”
The AI will respond dynamically based on whatever text you pass in prompt:
Hello, Bob! Great to meet you. 👋
4.Conclusion
With just a few lines of code, we integrated an AI model into a Spring Boot Application using Spring AI.
This “Hello World” example shows how easily developers can start experimenting with AI in their existing Spring Boot Projects.