MediaPipeTasksText Framework Reference

MPPTextEmbedder


@interface MPPTextEmbedder : 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

    Objective-C

    - (nullable instancetype)initWithModelPath:(nonnull NSString *)modelPath
                                         error:(NSError *_Nullable *_Nullable)error;

    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

    Objective-C

    - (nullable instancetype)initWithOptions:
                                 (nonnull MPPTextEmbedderOptions *)options
                                       error:(NSError *_Nullable *_Nullable)error;

    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

    Objective-C

    - (nullable MPPTextEmbedderResult *)embedText:(nonnull NSString *)text
                                            error:(NSError *_Nullable *_Nullable)
                                                      error;

    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

    Declaration

    Objective-C

    - (instancetype)init NS_UNAVAILABLE;
  • Utility function to computecosine similarity between two MPPEmbedding objects.

    Declaration

    Objective-C

    + (nullable NSNumber *)
        cosineSimilarityBetweenEmbedding1:(nonnull MPPEmbedding *)embedding1
                            andEmbedding2:(nonnull MPPEmbedding *)embedding2
                                    error:(NSError *_Nullable *_Nullable)error;

    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

    Declaration

    Objective-C

    + (instancetype)new NS_UNAVAILABLE;