DevTurtle logo DevTurtle

Spring AI – Generate embeddings with OpenAI

guide/ Spring AI guide

In one of our articles we have already discussed what embeddings are and how they are useful in the world of machine learning. Now it's time to do some practice so in this tutorial we will try to generate embeddings thanks to Spring AI and the models offered by OpenAI.

If you are new to the Spring AI world we suggest you follow our guide which explains step by step how the framework works thanks to practical examples. We will therefore start from the example project that we created in the previous articles.

Tutorial embeddings in Spring AI

In this exercise we will use OpenAI to generate an embedding of a word or phrase. To this end, we will create a REST controller that takes a string as input and returns its vector representation as output.

First you need to tell Spring AI which of the various embedding models offered by OpenAI to use. For the example we will use “text-embedding-3-small” which is the cheapest one and generates vectors with 1536 dimensions.

To correctly configure the framework, simply insert the following property into the application.properties file:

Plaintext
spring.ai.openai.embedding.options.model=text-embedding-3-small

At this point, all that remains is to implement our controller using the EmbeddingModel offered by SpringAI:

Java
package com.example.aidemo.controller;

import java.util.List;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
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 OpenAiEmbeddingController {

	private final EmbeddingModel embeddingClient;
	
	@Autowired
	public OpenAiEmbeddingController(EmbeddingModel embeddingClient) {
		this.embeddingClient = embeddingClient;
	}
	
	@GetMapping("/ai/embedding")
    public EmbeddingResponse embed(@RequestParam(value = "message") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return embeddingResponse;
    } 
	
}

As you can see, the code is really very simple and comments itself.

We can test the endpoint we have defined using the browser by connecting to localhost and inserting the message into the query string:

http://localhost:8080/ai/embedding?message=the cat is sleeping on the carpet

The response in JSON format will look something like this:

JSON
{
  "metadata": {
    "completion-tokens": null,
    "total-tokens": 7,
    "model": "text-embedding-3-small",
    "prompt-tokens": 7
  },
  "result": {
    "index": 0,
    "metadata": null,
    "output": [
      -0.03796489,
      -0.068526104,
      ...,
      -0.0016116675
      ]
    }
  ]
}

In the current version of Spring AI, a dimension reduction feature is not yet available, therefore, to obtain smaller embeddings it is necessary to use external libraries.

git icon
Git RepositoryDownload the source code

If you are interested in the topic, I invite you to read the next article which shows how integration with a vector database works.