No history yet

Introduction to Spring AI

What is Spring AI?

Spring AI is an application framework that makes it easier for Java developers to build applications with artificial intelligence features. Just as the core Spring Framework simplifies building complex enterprise applications, Spring AI aims to simplify the integration of AI models.

The main goal is to provide a standard, easy-to-use API for interacting with various AI providers. This means you can write your code once and, with minimal configuration changes, switch between different models from providers like OpenAI, Google, or open-source alternatives running locally.

Think of Spring AI as a universal remote for different AI models. You learn one set of buttons, and it can control multiple devices.

Key Features

Spring AI is built around a few core concepts that make it powerful and flexible.

First, it offers a portable API. The central interfaces, like ChatClient and EmbeddingClient, abstract away the specific details of each AI provider. This prevents you from being locked into a single vendor. If a new, better model comes along, you can adopt it without rewriting your application's logic.

Second, it integrates seamlessly with the Spring ecosystem you already know. It uses familiar Spring Boot auto-configuration, dependency injection, and property-based settings. This makes adding AI capabilities to an existing Spring application feel natural and straightforward.

Finally, it supports a range of AI functionalities. Beyond simple text-in, text-out chat, it has support for more advanced features like function calling, text-to-image generation, and creating vector embeddings for retrieval-augmented generation (RAG).

Setting Up Your First Project

Let's build a basic Spring Boot application that uses Spring AI. You'll need a Java Development Kit (JDK) version 17 or higher and a build tool like Maven or Gradle.

The easiest way to start is with the Spring Initializr at start.spring.io. Create a new project with the following settings:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.x or higher
  • Dependencies: Add Spring Web.

Once you download and open the project, you need to add the Spring AI dependency to your pom.xml file. Spring AI requires a special repository, so first add this to your <repositories> section:

<repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>

Next, add the Spring AI starter for OpenAI to your <dependencies> section. We'll use OpenAI for this example because it's a common starting point.

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>1.0.0-M1</version>
</dependency>

Now, you need to configure your OpenAI API key. Open the src/main/resources/application.properties file and add the following line, replacing YOUR_API_KEY with your actual key from OpenAI.

spring.ai.openai.api-key=YOUR_API_KEY

Creating a Simple Chat Endpoint

With the setup complete, we can create a simple web controller to interact with the AI model. Create a new Java class called ChatController and add the following code. It uses Spring's dependency injection to get an AiClient and exposes an endpoint that takes a user's message and returns the AI's response.

package com.example.aidemo;

import org.springframework.ai.client.AiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    private final AiClient aiClient;

    @Autowired
    public ChatController(AiClient aiClient) {
        this.aiClient = aiClient;
    }

    @GetMapping("/ai/chat")
    public String chat(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return aiClient.generate(message);
    }
}

That's it! Run your Spring Boot application. Once it's running, you can open a web browser or use a tool like curl to access http://localhost:8080/ai/chat?message=Why%20is%20Java%20so%20popular?. The application will send your question to the OpenAI API and return the answer.

This simple example just scratches the surface, but it shows how quickly you can integrate powerful AI capabilities into a Java application using the Spring AI framework.