Today, we will create simple applications using the Cohere Embed Multilingual v3 model via Amazon Bedrock and Spring AI.
We'll skip over basic Spring concepts like bean management and starters, as the main goal of this article is to explore the capabilities of Spring AI and Amazon Bedrock.
The full code for this project is available in the accompanying GitHub repository. To keep this article concise, I won't include some pre-calculated values and simple POJOs here -- you can find them in the repo if needed.
Before we start code implementation, let's discuss embeddings.
In the Spring AI documentation, we 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 mattresses. 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 returns 5 most relevant products to this query.
We will achieve this goal using embeddings. Steps we need to implement
Note: Please be aware that executing this application may cost you some money for running AWS Bedrock.
If you don't have an active AWS access key, do the following steps (copied and pasted from this SOF thread):
After you have your keys generated you should choose and enable the foundational model in Bedrock. Go to Amazon Bedrock, and from the Model Access menu on the left, configure access to the Cohere Embed Multilingual model.
To quickly generate a project template with all necessary dependencies, one may use https://start.spring.io/
In our example, we'll be using Java 17 and Spring Boot 3.4.1. Also, we need to include the following dependency:
Amazon Bedrock: This dependency provides us with smooth integration with Amazon Bedrock 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 pom.xml.
At the moment of writing this article, Spring AI version 1.0.0-M6 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.
Copy and paste the security credential pair we generated in step 1. Make sure you are not pushing these credentials to the remote repo.
We will be using . We may also use the Titan embedding model, but in this case, the config file should be set in a slightly different way. You may find more information in the Spring AI docs.
As the main purpose of this article is to show the ease of Spring AI integration with Amazon Bedrock 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 Bedrock 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 code without using any Rest Controllers and API calls. If you want to make this app a little bit more flexible by triggering it by an endpoint execution you may use an approach similar to the one used in this article.
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 our query, we've included 3 keywords: . Let's execute our code and validate the result.
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 13 has the highest similarity, as it is both a king-sized and hypoallergenic mattress. Even though it doesn't exactly match the search query, it closely aligns with what we were looking for. All of the other recommended mattresses are either king-sized or hypoallergenic.
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 Open AI. On the other hand, Amazon Bedrock offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, DeepSeek, Luma, Meta, Mistral AI, poolside (coming soon), Stability AI, and Amazon through a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI.
I hope you found this article helpful and that it will inspire you to explore Spring AI and AWS Bedrock more deeply.