Class TransformersEmbeddingModel

java.lang.Object
org.springframework.ai.embedding.AbstractEmbeddingModel
org.springframework.ai.transformers.TransformersEmbeddingModel
All Implemented Interfaces:
EmbeddingModel, Model<EmbeddingRequest,EmbeddingResponse>, org.springframework.beans.factory.InitializingBean

public class TransformersEmbeddingModel extends AbstractEmbeddingModel implements org.springframework.beans.factory.InitializingBean
An implementation of the AbstractEmbeddingModel that uses ONNX-based Transformer models for text embeddings.

By default, it uses the all-MiniLM-L6-v2 model, but can be configured to use other ONNX-compatible models. The class supports both CPU and GPU inference, caching of model resources, and various tokenization options.

For more information on the underlying SBERT framework, see: SBERT Documentation SBERT Pre-trained Models

Since:
1.0.0
Author:
Christian Tzolov
  • Field Details

  • Constructor Details

    • TransformersEmbeddingModel

      public TransformersEmbeddingModel()
    • TransformersEmbeddingModel

      public TransformersEmbeddingModel(MetadataMode metadataMode)
    • TransformersEmbeddingModel

      public TransformersEmbeddingModel(MetadataMode metadataMode, io.micrometer.observation.ObservationRegistry observationRegistry)
  • Method Details

    • setTokenizerOptions

      public void setTokenizerOptions(Map<String,String> tokenizerOptions)
    • setDisableCaching

      public void setDisableCaching(boolean disableCaching)
    • setResourceCacheDirectory

      public void setResourceCacheDirectory(String resourceCacheDir)
    • setGpuDeviceId

      public void setGpuDeviceId(int gpuDeviceId)
    • setTokenizerResource

      public void setTokenizerResource(org.springframework.core.io.Resource tokenizerResource)
    • setModelResource

      public void setModelResource(org.springframework.core.io.Resource modelResource)
    • setTokenizerResource

      public void setTokenizerResource(String tokenizerResourceUri)
    • setModelResource

      public void setModelResource(String modelResourceUri)
    • setModelOutputName

      public void setModelOutputName(String modelOutputName)
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
    • embed

      public float[] embed(String text)
      Description copied from interface: EmbeddingModel
      Embeds the given text into a vector.
      Specified by:
      embed in interface EmbeddingModel
      Parameters:
      text - the text to embed.
      Returns:
      the embedded vector.
    • embed

      public float[] embed(Document document)
      Description copied from interface: EmbeddingModel
      Embeds the given document's content into a vector.
      Specified by:
      embed in interface EmbeddingModel
      Parameters:
      document - the document to embed.
      Returns:
      the embedded vector.
    • embedForResponse

      public EmbeddingResponse embedForResponse(List<String> texts)
      Description copied from interface: EmbeddingModel
      Embeds a batch of texts into vectors and returns the EmbeddingResponse.
      Specified by:
      embedForResponse in interface EmbeddingModel
      Parameters:
      texts - list of texts to embed.
      Returns:
      the embedding response.
    • embed

      public List<float[]> embed(List<String> texts)
      Description copied from interface: EmbeddingModel
      Embeds a batch of texts into vectors.
      Specified by:
      embed in interface EmbeddingModel
      Parameters:
      texts - list of texts to embed.
      Returns:
      list of embedded vectors.
    • call

      public EmbeddingResponse call(EmbeddingRequest request)
      Description copied from interface: Model
      Executes a method call to the AI model.
      Specified by:
      call in interface EmbeddingModel
      Specified by:
      call in interface Model<EmbeddingRequest,EmbeddingResponse>
      Parameters:
      request - the request object to be sent to the AI model
      Returns:
      the response from the AI model
    • setObservationConvention

      public void setObservationConvention(EmbeddingModelObservationConvention observationConvention)
      Use the provided convention for reporting observation data
      Parameters:
      observationConvention - The provided convention