Class AbstractEmbeddingModel

java.lang.Object
org.springframework.ai.embedding.AbstractEmbeddingModel
All Implemented Interfaces:
EmbeddingModel, Model<EmbeddingRequest,EmbeddingResponse>
Direct Known Subclasses:
AzureOpenAiEmbeddingModel, BedrockCohereEmbeddingModel, BedrockTitanEmbeddingModel, MiniMaxEmbeddingModel, MistralAiEmbeddingModel, OCIEmbeddingModel, OllamaEmbeddingModel, OpenAiEmbeddingModel, PostgresMlEmbeddingModel, QianFanEmbeddingModel, TransformersEmbeddingModel, VertexAiTextEmbeddingModel, WatsonxAiEmbeddingModel, ZhiPuAiEmbeddingModel

public abstract class AbstractEmbeddingModel extends Object implements EmbeddingModel
Abstract implementation of the EmbeddingModel interface that provides dimensions calculation caching.
Author:
Christian Tzolov
  • Field Details

    • embeddingDimensions

      protected final AtomicInteger embeddingDimensions
      Cached embedding dimensions.
  • Constructor Details

    • AbstractEmbeddingModel

      public AbstractEmbeddingModel()
      Default constructor.
  • Method Details

    • dimensions

      public static int dimensions(EmbeddingModel embeddingModel, String modelName, String dummyContent)
      Return the dimension of the requested embedding generative name. If the generative name is unknown uses the EmbeddingModel to perform a dummy EmbeddingModel#embed and count the response dimensions.
      Parameters:
      embeddingModel - Fall-back client to determine, empirically the dimensions.
      modelName - Embedding generative name to retrieve the dimensions for.
      dummyContent - Dummy content to use for the empirical dimension calculation.
      Returns:
      Returns the embedding dimensions for the modelName.
    • dimensions

      public int dimensions()
      Description copied from interface: EmbeddingModel
      Get the number of dimensions of the embedded vectors. Note that by default, this method will call the remote Embedding endpoint to get the dimensions of the embedded vectors. If the dimensions are known ahead of time, it is recommended to override this method.
      Specified by:
      dimensions in interface EmbeddingModel
      Returns:
      the number of dimensions of the embedded vectors.