An introduction about Spring AI

Spring AI is a module of Spring that helps us easily work with applications that use AI, integrating with AI providers such as OpenAI, Azure OpenAI, Google Gemini, … In this tutorial, I will introduce to you all the basic knowledge about Spring AI so you can use it in applications that integrate with the AI providers!

First, I will create a new Spring Boot project as an example:

I will make a small application that integrates with OpenAI to query information, so I have declared to use the OpenAI dependency. In addition, we also need to declare Spring Web and Spring Reactive Web dependencies!


Basic AI concepts

Before going into implementing the example, there are some concepts about AI that you need to understand as follows:

  • Models are algorithms to process user request information and generate output appropriate to the request. These algorithms will use a large amount of input data to produce predictions, text, images, and other outputs.
  • Prompts can be roughly said as input requests from users, usually text input, for the AI model to rely on to generate output.
  • Prompt Templates are templates used to create prompts. We will need to replace the placeholders defined in the template with the values we want to create a prompt from these values.
  • Embeddings are objects that will transform user prompts into vector objects in the form of numbers that AI models can use to process requests.
  • Tokens are the basic linguistic units that the AI model uses to process prompts. At input, the AI model will convert words to tokens, and at output, the AI model will convert tokens to words.

Understanding the basic concepts above, you will imagine at a high level how an AI application works!

Basic Spring AI

To integrate with AI providers, Spring AI defines an interface ModelClient with ModelRequest and ModelResponse:

There are 3 sub-interfaces from the ModelClient interface:

  • ChatClient
  • ImageClient
  • and EmbeddingClient

Corresponding to each AI provider, there will be its own implementation!

For OpenAI, the implementations for the 3 interfaces ChatClient, ImageClient, EmbeddingClient are OpenAiChatClient, OpenAiImageClient and OpenAiEmbeddingClient respectively:

For my example, I will use the ChatClient class to work with OpenAI!

Implement the example

To integrate with OpenAI, first, you need to create a new API key in the OpenAI platform by going to and clicking on the “Create new secret key” button as follows:

This API key is used to authenticate with the OpenAI API! Please save this API key information somewhere so you can use it again later!

To configure OpenAI information with Spring AI, you can use the OpenAiApi class.

There are 4 overloading constructors to initialize the object of this OpenAiApi class with the required information that we need to configure about the API key:

  • OpenAiApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler)
  • OpenAiApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder)
  • OpenAiApi(String baseUrl, String openAiToken)
  • OpenAiApi(String openAiToken)

The first constructor will allow us to configure OpenAI’s baseUrl to query information, openAiToken is the API key we just created above, restClientBuilder and responseErrorHandler allow us to define configurations related to requests to OpenAI and handle errors when receiving response from OpenAI.

The following constructors will use Spring’s default configurations with the OpenAI API baseUrl defaulting to “”.

You can use the default configuration and only define the API key information using the last constructor as follows:

And initialize the object of the ChatClient class from the object of the OpenAiApi class as follows:

For the Spring Boot project, you only need to define API key information using the property:

Spring Boot will automatically create the OpenAiApi object, then the ChatClient object in the Spring container, we just need to ingest it and use it:

Now we will use the ChatClient class to query information from OpenAI. 

My example is as follows:

Please try to check the results! Mine is exceeded the free quota, so when I run it, I get the following error:

Add Comment