Using Your Own Embeddings in S... Note
Spring

Using Your Own Embeddings in Spring AI 2.1

VectorStore.add() creates embeddings for text documents and saves them alongside the text. Spring AI 2.1 introduces upsert functionality, allowing applications to provide their own pre-generated embeddings for various media types like images, video, or audio. This feature bypasses the vector store's internal embedding model. A new similaritySearch overload accepts a query embedding, giving developers granular control over both data indexing and search queries. This is particularly useful for multimodal applications where text descriptions might not fully capture an item's characteristics shown in an image. For instance, a furniture catalog can store image embeddings alongside product details, enabling searches based on visual attributes. Similarly, video or audio content can be searched by embedding segments and matching them against text queries. Existing text embeddings, from any source, can also be managed efficiently through upsert, avoiding redundant re-embedding. Upsert ensures compatibility with the vector store's expected data formats for IDs, text, metadata, and vectors. When indexing product images, Spring AI's multimodal embedding models generate vectors that are paired with product metadata. This allows for a direct comparison between search text embeddings and stored image embeddings. Search queries are processed by embedding the search text with the same multimodal model. The similaritySearch method then uses this query embedding to find the most relevant results, filtering based on criteria like stock availability. It's crucial to use the same embedding model for both indexing and querying to ensure meaningful search results. Changing embedding models may necessitate re-generating stored embeddings, and tracking model versions in metadata is recommended. Upsert supports updating existing documents by replacing entries with matching IDs, preventing duplicate entries. While upsert calls are not transactional, retrying the entire list after resolving failures is advised. EmbeddedDocument validates vectors for completeness and finite values, and vector stores perform dimension checks before writing. Spring AI determines expected vector sizes from configuration or existing indexes, falling back to database-level checks if necessary. RedisVectorStore includes pre-write checks to prevent saving vectors of incorrect dimensions. As of Spring AI 2.1.0-RC1, PGvector, Redis, Elasticsearch, Qdrant, and OpenSearch support the new upsert and query embedding search capabilities. Applications can continue using add() for simple text embeddings, while the new features offer advanced control for complex multimodal scenarios.