Class TransformersEmbeddingModel
java.lang.Object
org.springframework.ai.embedding.AbstractEmbeddingModel
org.springframework.ai.transformers.TransformersEmbeddingModel
- All Implemented Interfaces:
AutoCloseable,EmbeddingModel,Model<EmbeddingRequest,,EmbeddingResponse> org.springframework.beans.factory.InitializingBean
public class TransformersEmbeddingModel
extends AbstractEmbeddingModel
implements org.springframework.beans.factory.InitializingBean, AutoCloseable
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, Soby Chacko
-
Field Summary
FieldsFields inherited from class org.springframework.ai.embedding.AbstractEmbeddingModel
embeddingDimensions -
Constructor Summary
ConstructorsConstructorDescriptionTransformersEmbeddingModel(MetadataMode metadataMode) TransformersEmbeddingModel(MetadataMode metadataMode, io.micrometer.observation.ObservationRegistry observationRegistry) -
Method Summary
Modifier and TypeMethodDescriptionvoidcall(EmbeddingRequest request) Executes a method call to the AI model.voidclose()Release the native ONNX runtime session and tokenizer acquired inafterPropertiesSet().float[]Embeds the given text into a vector.List<float[]>Embeds a batch of texts into vectors.float[]Embeds the given document's content into a vector.embedForResponse(List<String> texts) Embeds a batch of texts into vectors and returns theEmbeddingResponse.@Nullable StringgetEmbeddingContent(Document document) Extracts the text content from aDocumentto be used for embedding.voidsetDisableCaching(boolean disableCaching) voidsetGpuDeviceId(int gpuDeviceId) voidsetModelOutputName(String modelOutputName) voidsetModelResource(String modelResourceUri) voidsetModelResource(org.springframework.core.io.Resource modelResource) voidsetObservationConvention(EmbeddingModelObservationConvention observationConvention) Use the provided convention for reporting observation datavoidsetResourceCacheDirectory(@Nullable String resourceCacheDir) voidsetTokenizerOptions(Map<String, String> tokenizerOptions) voidsetTokenizerResource(String tokenizerResourceUri) voidsetTokenizerResource(org.springframework.core.io.Resource tokenizerResource) Methods inherited from class org.springframework.ai.embedding.AbstractEmbeddingModel
dimensions, dimensionsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.embedding.EmbeddingModel
embed
-
Field Details
-
DEFAULT_ONNX_TOKENIZER_URI
- See Also:
-
DEFAULT_ONNX_MODEL_URI
- See Also:
-
DEFAULT_MODEL_OUTPUT_NAME
- See Also:
-
tokenizerOptions
-
-
Constructor Details
-
TransformersEmbeddingModel
public TransformersEmbeddingModel() -
TransformersEmbeddingModel
-
TransformersEmbeddingModel
public TransformersEmbeddingModel(MetadataMode metadataMode, io.micrometer.observation.ObservationRegistry observationRegistry)
-
-
Method Details
-
setTokenizerOptions
-
setDisableCaching
public void setDisableCaching(boolean disableCaching) -
setResourceCacheDirectory
-
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
-
setModelResource
-
setModelOutputName
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
close
public void close() throws ai.onnxruntime.OrtExceptionRelease the native ONNX runtime session and tokenizer acquired inafterPropertiesSet(). Spring registers this as the bean destroy method automatically (inferredclose()method), and the model can also be used with try-with-resources.- Specified by:
closein interfaceAutoCloseable- Throws:
ai.onnxruntime.OrtException
-
embed
Description copied from interface:EmbeddingModelEmbeds the given text into a vector.- Specified by:
embedin interfaceEmbeddingModel- Parameters:
text- the text to embed.- Returns:
- the embedded vector.
-
getEmbeddingContent
Description copied from interface:EmbeddingModelExtracts the text content from aDocumentto be used for embedding. By default, returnsDocument.getText(). Implementations that supportMetadataModeshould override this method to returnDocument.getFormattedContent(org.springframework.ai.document.MetadataMode)with the appropriate metadata mode, so that metadata is included in the text sent to the embedding API.- Specified by:
getEmbeddingContentin interfaceEmbeddingModel- Parameters:
document- the document to extract embedding content from.- Returns:
- the text content to embed.
-
embed
Description copied from interface:EmbeddingModelEmbeds the given document's content into a vector.- Specified by:
embedin interfaceEmbeddingModel- Parameters:
document- the document to embed.- Returns:
- the embedded vector.
-
embedForResponse
Description copied from interface:EmbeddingModelEmbeds a batch of texts into vectors and returns theEmbeddingResponse.- Specified by:
embedForResponsein interfaceEmbeddingModel- Parameters:
texts- list of texts to embed.- Returns:
- the embedding response.
-
embed
Description copied from interface:EmbeddingModelEmbeds a batch of texts into vectors.- Specified by:
embedin interfaceEmbeddingModel- Parameters:
texts- list of texts to embed.- Returns:
- list of embedded vectors.
-
call
Description copied from interface:ModelExecutes a method call to the AI model.- Specified by:
callin interfaceEmbeddingModel- Specified by:
callin interfaceModel<EmbeddingRequest,EmbeddingResponse> - Parameters:
request- the request object to be sent to the AI model- Returns:
- the response from the AI model
-
setObservationConvention
Use the provided convention for reporting observation data- Parameters:
observationConvention- The provided convention
-