This is my second article in a series of introductions to Spring AI. You may find the first one, where I explained how to generate images using Spring AI and OpenAI DALL-E 3 models, here. Today, we will create simple applications using embeddings API and Spring AI.
In this article, I'll skip the explanation of some basic Spring concepts like bean management, starters, etc, as the main goal of this article is to discover Spring AI capabilities. For the same reason, I'll not create detailed instructions on generating the OpenAI API Key. In case you don't have one, follow the links in Step 0, which should give you enough context on how to create one.
The code I will share in this article will also be available in the GitHub repo. You may find this repo useful because to make this article shorter, I'll not paste here some precalculated values and simple POJOs.
Before we start code implementation, let's discuss what embeddings are.
In the Spring AI documentation, we can find the following definition of embeddings:
Embeddings are numerical representations of text, images, or videos that capture relationships between inputs.
Embeddings convert text, images, and video into arrays of floating-point numbers called vectors. These vectors are designed to capture the meaning of the text, images, and videos. The length of the embedding array is called the vector's dimensionality.
Key points we should pay attention to:
As with every other vector, we can find the distance between two embeddings. The closer the two embeddings are to each other, the more similar their context. We will use this approach in our application.
Let's imagine that we have an online shop with different electronics. Every single item has its ID and description. We need to create a module that will receive users' input describing the item the user wants to find or buy and return five of the most relevant products to this query.
We will achieve this goal using embeddings. The following are steps we need to implement:
If you don't have an active OpenAI API key, do the following steps:
To quickly generate a project template with all necessary dependencies, one may use https://start.spring.io/.
In my example, I'll be using Java 17 and Spring Boot 3.4.1. Also, we need to include the following dependency:
This dependency provides us with smooth integration with OpenAI just by writing a couple lines of code and a few lines of configurations.
After clicking generate, open the downloaded files in the IDE you are working on and validate that all necessary dependencies exist in .
At the moment of writing this article, Spring AI version 1.0.0-M4 has not yet been published in the central Maven repository and is only available in the Spring repository. That's why we need to add a link to that repo in our as well:
As a next step, we need to configure our property file. By default, Spring uses or file. In this example, I'm using the YAML format. You may reformat code into if you feel more comfortable working with this format.
As the main purpose of this article is to show the ease of Spring AI integration with embedding models, we will not go deeper into other configurations. You may find more config options in the Spring docs.
Let's create two files in the resource folder.
The first one is the JSON-formatted "database" of items in our shop. Every Item will have the following parameters: Id, Name, and Description. I named this file samples.json and saved it in the resource folder.
The second one is a list of embeddings of the product description. I executed embeddings API in a separate application and saved responses for every single product into a separate file, . I'll not share the whole file here, as it will make the article unreadable, but you still can download it from the GitHub repo of this project I shared at the beginning of the article.
Now, let's create the main service of our application -> embedding service.
To integrate our application with the embeddings API, we need to autowire . We have already configured OpenAI embeddings in the . Spring Boot will automatically create and configure the instance (Bean) of .
To fetch embeddings for a particular String or text, we just need to write one line of code:
To calculate similarities, we introduced Service. Let's take a deeper look at its implementation.
We will execute this application directly from the code, without using REST controllers or making API calls. You may use an approach similar to the one I used in the first article, when I created a separate endpoint to make execution smoother.
We are executing our code in the last line of the method, fetching the bean from the context, and executing the method with a provided query.
In my query, I've included three keywords: 5G, Phone, and IPS. We should receive quite a high similarity with Product 1 and Product 2. Both products are smartphones, but Product 1 is a 5G smartphone, while Product 2 has an IPS screen.
To start our application, we need to run the following command:
In a couple of seconds after executing, we may see the following result in the console:
We can see that Product 2 and Product 1 have the biggest similarities. What's also interesting is that as we included the IPS keyword, our top five similar products all are products with displays.
Spring AI is a great tool that helps developers smoothly integrate with different AI models. At the moment of writing this article, Spring AI supports 10 embedding models, including but not limited to Ollama and Amazon Bedrock.
I hope you found this article helpful and that it will inspire you to explore Spring AI deeper.