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
Fields inherited from class org.springframework.ai.embedding.AbstractEmbeddingModel
embeddingDimensions
-
Constructor Summary
ConstructorDescriptionTransformersEmbeddingModel
(MetadataMode metadataMode) TransformersEmbeddingModel
(MetadataMode metadataMode, io.micrometer.observation.ObservationRegistry observationRegistry) -
Method Summary
Modifier and TypeMethodDescriptionvoid
call
(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
.void
setDisableCaching
(boolean disableCaching) void
setGpuDeviceId
(int gpuDeviceId) void
setModelOutputName
(String modelOutputName) void
setModelResource
(String modelResourceUri) void
setModelResource
(org.springframework.core.io.Resource modelResource) void
setObservationConvention
(EmbeddingModelObservationConvention observationConvention) Use the provided convention for reporting observation datavoid
setResourceCacheDirectory
(String resourceCacheDir) void
setTokenizerOptions
(Map<String, String> tokenizerOptions) void
setTokenizerResource
(String tokenizerResourceUri) void
setTokenizerResource
(org.springframework.core.io.Resource tokenizerResource) Methods inherited from class org.springframework.ai.embedding.AbstractEmbeddingModel
dimensions, dimensions
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
-
embed
Description copied from interface:EmbeddingModel
Embeds the given text into a vector.- Specified by:
embed
in interfaceEmbeddingModel
- Parameters:
text
- the text to embed.- Returns:
- the embedded vector.
-
embed
Description copied from interface:EmbeddingModel
Embeds the given document's content into a vector.- Specified by:
embed
in interfaceEmbeddingModel
- Parameters:
document
- the document to embed.- Returns:
- the embedded vector.
-
embedForResponse
Description copied from interface:EmbeddingModel
Embeds a batch of texts into vectors and returns theEmbeddingResponse
.- Specified by:
embedForResponse
in interfaceEmbeddingModel
- Parameters:
texts
- list of texts to embed.- Returns:
- the embedding response.
-
embed
Description copied from interface:EmbeddingModel
Embeds a batch of texts into vectors.- Specified by:
embed
in interfaceEmbeddingModel
- Parameters:
texts
- list of texts to embed.- Returns:
- list of embedded vectors.
-
call
Description copied from interface:Model
Executes a method call to the AI model.- Specified by:
call
in interfaceEmbeddingModel
- Specified by:
call
in 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
-