Embedding Model
Spring AI provides EmbeddingModel to embed text. EmbeddingModel extends from Model.
EmbeddingModel is rarely used directly, but used indirectly with Vector Store.
EmbeddingModel
The model request is EmbeddingRequest. This request contains a list of Strings as the input and an EmbeddingOptions as the options.
public class EmbeddingRequest implements ModelRequest<List<String>> {
private final List<String> inputs;
@Nullable
private final EmbeddingOptions options;
}
The model response is EmbeddingResponse. This response contains a list of Embeddings as the result.
public class EmbeddingResponse implements ModelResponse<Embedding> {
private final List<Embedding> embeddings;
private final EmbeddingResponseMetadata metadata;
}
An Embedding contains an array of float as the result.
public class Embedding implements ModelResult<float[]> {
private final float[] embedding;
private final Integer index;
private final EmbeddingResultMetadata metadata;
}
The call method of EmbeddingModel sends an EmbeddingRequest to an embedding model and returns an EmbeddingResponse.
public interface EmbeddingModel
extends Model<EmbeddingRequest, EmbeddingResponse> {
@Override
EmbeddingResponse call(EmbeddingRequest request);
}
EmbeddingModel also has some shortcut methods.
float[] embed(String text)float[] embed(Document document)List<float[]> embed(List<String> texts)List<float[]> embed(List<Document> documents, EmbeddingOptions options, BatchingStrategy batchingStrategy)EmbeddingResponse embedForResponse(List<String> texts)
The dimensions method of EmbeddingModel returns the number of dimensions of the embedded vectors.
EmbeddingOptions
EmbeddingOptions represents the options for embedding models.
getModelreturns the model used for embedding.getDimensionsreturns the requested dimensions.
EmbeddingOptions is created using EmbeddingOptionsBuilder.
var options = EmbeddingOptionsBuilder.builder()
.withModel("text-embedding-3-large")
.withDimensions(1024)
.build();
BatchingStrategy
BatchingStrategy is the strategy used for batching multiple documents.
public interface BatchingStrategy {
List<List<Document>> batch(List<Document> documents);
}
TokenCountBatchingStrategy is a built-in implementation of BatchingStrategy. This strategy batches documents based on their token counts, ensuring that each batch does not exceed the calculated maximum input token count.