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 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.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.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(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
-
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.
-
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
-