MediaPipeTasksText Framework Reference

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 kTfLiteInt32 of 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 kTfLiteInt32 of 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 MPPTextEmbedder from an absolute path to a TensorFlow Lite model file stored locally on the device and the default MPPTextEmbedderOptions.

    Declaration

    Swift

    convenience init(modelPath: String) throws

    Parameters

    modelPath

    An absolute path to a TensorFlow Lite model file stored locally on the device.

    error

    An optional error parameter populated when there is an error in initializing the text embedder.

    Return Value

    A new instance of MPPTextEmbedder with the given model path. nil if there is an error in initializing the text embedder.

  • Creates a new instance of MPPTextEmbedder from the given MPPTextEmbedderOptions.

    Declaration

    Swift

    init(options: TextEmbedderOptions) throws

    Parameters

    options

    The options of type MPPTextEmbedderOptions to use for configuring the MPPTextEmbedder.

    error

    An optional error parameter populated when there is an error in initializing the text embedder.

    Return Value

    A new instance of MPPTextEmbedder with the given options. nil if there is an error in initializing the text embedder.

  • Performs embedding extraction on the input text.

    Declaration

    Swift

    func embed(text: String) throws -> TextEmbedderResult

    Parameters

    text

    The NSString on which embedding extraction is to be performed.

    error

    An optional error parameter populated when there is an error in performing embedding extraction on the input text.

    Return Value

    A MPPTextEmbedderResult object that contains a list of embeddings.

  • Undocumented

  • Utility function to computecosine similarity between two MPPEmbedding objects.

    Declaration

    Swift

    class func cosineSimilarity(embedding1: Embedding, embedding2: Embedding) throws -> NSNumber

    Parameters

    embedding1

    One of the two MPPEmbeddings between whom cosine similarity is to be computed.

    embedding2

    One of the two MPPEmbeddings between whom cosine similarity is to be computed.

    error

    An optional error parameter populated when there is an error in calculating cosine similarity between two embeddings.

    Return Value

    An NSNumber which holds the cosine similarity of type double.

  • Undocumented