Spring AI EmbeddingModel
π Spring AI Vector Embeddings β A Fun & Engaging Guideβ
Welcome to the Spring AI Vector Embedding tutorial! π Here, we'll unravel the mysteries of vectors, embeddings, and how they supercharge semantic searches. Plus, weβll generate embeddings using popular LLM models like OpenAI and DeepseekAI using Ollama! π€β¨
π€ 1. What is a Vector Embedding?β
Imagine you're in a room full of people speaking different languages. How do you find someone who understands you? Vector embeddings work like a universal translatorβturning words, sentences, and even images into arrays of numbers that capture meaning! π’π‘
In AI terms, a vector (or embedding) is a magical array of numbers that represents an object, be it a word, sentence, image, audio snippet, or even a product description. π¦
Each number in the vector is a component, corresponding to a dimension in hyperspace (yes, like sci-fi π). More dimensions mean more details about the object!
π Example Timeβ
Letβs take the word βhappinessβ and give it some numbers:
[2.5, 1.3, 0.8]
- 2.5 β Emotional positivity (yay! π)
- 1.3 β Emotional intensity (oh wow! π)
- 0.8 β Context usage (poetry or science? π€)
Now, hereβs the kicker: these numbers donβt mean much alone, but together, they create a powerful representation! π§ β¨
π 2. Semantic Search: Finding Meaning, Not Just Wordsβ
Ever wondered how Google knows exactly what you're looking for, even when you type weird search queries? Thatβs semantic search in action! π΅οΈββοΈπ‘
Instead of matching exact words, semantic search looks at meaning. It uses vector distancesβsmall distances = high similarity, big distances = low similarity. π¬
π Real-world applications:
- Google Search π§ β Finds the perfect answer, even if your query is gibberish.
- Netflix & Spotify πΆ β Suggests movies & songs you actually like.
- Chatbots & AI Assistants π€ β Understands you better than your best friend!
π 3. Meet the EmbeddingModel Interfaceβ
Spring AI makes embedding easy with the EmbeddingModel interface! π―
Think of it as your personal AI librarian, transforming text into numerical vectors! ππ’
public interface EmbeddingModel extends Model<EmbeddingRequest, EmbeddingResponse> {
List<Double> embed(String text);
List<Double> embed(Document document);
EmbeddingResponse embedForResponse(List<String> texts);
EmbeddingResponse call(EmbeddingRequest request);
}
π‘ The embedForResponse()
method gives extra metadata, just in case you want more than just numbers!
β‘ Usage Exampleβ
String text = "...";
List<Double> embeddings = embeddingModel.embed(text);
Or, for more control:
List<Double> embeddings = embeddingModel.call(
new EmbeddingRequest(List.of(text), OpenAiEmbeddingOptions.builder()
.withModel("text-embedding-3-small")
.withUser("jon.snow") // Because he knows nothing, right? π
.build()))
.getResult().getOutput();
π 4. EmbeddingModel Implementationsβ
Spring AI supports tons of embedding models! π Each has its own implementation class.
Provider | Implementation Class | Supported Models |
---|---|---|
OpenAI | OpenAiEmbeddingModel | text-embedding-ada-002, text-embedding-3-large, text-embedding-3-small |
Azure OpenAI | AzureOpenAiEmbeddingModel | text-embedding-ada-002 |
Ollama | OllamaEmbeddingModel | mistral |
PostgresML | PostgresMlEmbeddingModel | distilbert-base-uncased |
Mistral AI | MistralAiEmbeddingModel | mistral-embed |
MiniMax | MiniMaxEmbeddingModel | embo-01 |
QianFan | QianFanEmbeddingModel | bge_large_zh |
Amazon Bedrock | BedrockTitanEmbeddingModel | amazon.titan-embed-image-v1 |
π For the full list, check the official docs!
π¬ 5. Letβs Get Our Hands Dirty: DeepseekAI Demoβ
Weβll use Ollamaβs mxbai-embed-large and nomic-embed-text model to generate vector embeddings. π β¨
π 5.1. Maven Setupβ
First, add the Spring Boot starter dependency:
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
</dependencies>
π Donβt forget to add your application properties!
server.port=8084
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=deepseek-r1:latest
spring.ai.ollama.chat.options.temperature=0.7
spring.ai.ollama.init.pull-model-strategy=always
spring.ai.ollama.init.pull-model-strategy.embedding.additional-models=mxbai-embed-large
π 5.2. DeepseekAiEmbeddingModel Beanβ
Spring Boot auto-configures an DeepseekAiEmbeddingModel
bean. Just inject it into your class:
@RestController
public class DeepseekAiEmbeddingController {
private final EmbeddingModel embeddingModel;
@Autowired
public DeepseekAiEmbeddingController(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/ai/embedding")
public Map embed(@RequestParam(value = "message", defaultValue = "tell me a joke") String message) {
EmbeddingResponse embeddingResponse = embeddingModel.embedForResponse(List.of(message));
return Map.of("embedding", embeddingResponse);
}
}
π Test it with a REST tool! π
π― 6. Wrapping It Upβ
π Hereβs what we covered:
β Vectors (embeddings) represent objects in numerical form. β Semantic search finds meaning instead of just words. β The EmbeddingModel interface is your AI magic wand! πͺ β Spring AI supports multiple embedding models. β We saw a live demo using Deepseek AI with Ollama!
π‘ Whatβs next? Start building cool AI-powered apps! ππ‘
π Happy Learning & Happy Coding! π¦Ύπ₯