TextEmbedder
class TextEmbedder : NSObject@brief Performs embedding extraction on text.
This API expects a TFLite model with (optional) TFLite Model Metadata.
Metadata is required for models with int32 input tensors because it contains the input process unit for the model’s Tokenizer. No metadata is required for models with string input tensors.
Input tensors:
- Three input tensors
kTfLiteInt32of shape[batch_size x bert_max_seq_len]representing the input ids, mask ids, and segment ids. This input signature requires a Bert Tokenizer process unit in the model metadata. - Or one input tensor
kTfLiteInt32of shape[batch_size x max_seq_len]representing the input ids. This input signature requires a Regex Tokenizer process unit in the model metadata. - Or one input tensor (
kTfLiteString) that is shapeless or has shape[1]containing the input string.
At least one output tensor (kTfLiteFloat32/kTfLiteUint8) with shape [1 x N] where N is
the number of dimensions in the produced embeddings.
-
Creates a new instance of
MPPTextEmbedderfrom an absolute path to a TensorFlow Lite model file stored locally on the device and the defaultMPPTextEmbedderOptions.Declaration
Swift
convenience init(modelPath: String) throwsParameters
modelPathAn absolute path to a TensorFlow Lite model file stored locally on the device.
errorAn optional error parameter populated when there is an error in initializing the text embedder.
Return Value
A new instance of
MPPTextEmbedderwith the given model path.nilif there is an error in initializing the text embedder. -
Creates a new instance of
MPPTextEmbedderfrom the givenMPPTextEmbedderOptions.Declaration
Swift
init(options: TextEmbedderOptions) throwsParameters
optionsThe options of type
MPPTextEmbedderOptionsto use for configuring theMPPTextEmbedder.errorAn optional error parameter populated when there is an error in initializing the text embedder.
Return Value
A new instance of
MPPTextEmbedderwith the given options.nilif there is an error in initializing the text embedder. -
Performs embedding extraction on the input text.
Declaration
Swift
func embed(text: String) throws -> TextEmbedderResultParameters
textThe
NSStringon which embedding extraction is to be performed.errorAn optional error parameter populated when there is an error in performing embedding extraction on the input text.
Return Value
A
MPPTextEmbedderResultobject that contains a list of embeddings. -
Undocumented
-
Utility function to computecosine similarity between two
MPPEmbeddingobjects.Declaration
Parameters
embedding1One of the two
MPPEmbeddings between whom cosine similarity is to be computed.embedding2One of the two
MPPEmbeddings between whom cosine similarity is to be computed.errorAn optional error parameter populated when there is an error in calculating cosine similarity between two embeddings.
Return Value
An
NSNumberwhich holds the cosine similarity of typedouble. -
Undocumented