# TextEmbedder

    class TextEmbedder: NSObject, EmbeddingProvider

Performs embedding extraction on text.

This API expects a TFLite model with (optional) \[TFLite Model Metadata\](https://www.tensorflow.org/lite/convert/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.
- `
  ### [cosineSimilarity(embedding1:embedding2:)](https://developers.google.com/edge/api/mediapipe/swift/text/Classes/TextEmbedder.md.txt#/c:objc(cs)MPPTextEmbedder(cm)cosineSimilarityBetweenEmbedding1:andEmbedding2:error:)

  ` Utility function to compute\[cosine similarity\](https://en.wikipedia.org/wiki/Cosine_similarity) between two `MPPEmbedding` objects.

  #### Declaration

  Swift

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

  #### Parameters

  |---|---|
  | `embedding1` | One of the two `MPPEmbedding`s between whom cosine similarity is to be computed. |
  | `embedding2` | One of the two `MPPEmbedding`s 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`.
- `
  ### [init(modelPath:)](https://developers.google.com/edge/api/mediapipe/swift/text/Classes/TextEmbedder.md.txt#/c:objc(cs)MPPTextEmbedder(im)initWithModelPath:error:)

  ` 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.
- `
  ### [init(options:)](https://developers.google.com/edge/api/mediapipe/swift/text/Classes/TextEmbedder.md.txt#/c:objc(cs)MPPTextEmbedder(im)initWithOptions:error:)

  ` 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.
- `
  ### [embed(text:)](https://developers.google.com/edge/api/mediapipe/swift/text/Classes/TextEmbedder.md.txt#/c:objc(cs)MPPTextEmbedder(im)embedText:error:)

  ` 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.
- `
  ### [embed(text:textFormatContext:)](https://developers.google.com/edge/api/mediapipe/swift/text/Classes/TextEmbedder.md.txt#/c:objc(cs)MPPTextEmbedder(im)embedText:textFormatContext:error:)

  ` Performs embedding extraction on the input text with formatting options for models with prompt requirements.

  #### Declaration

  Swift

      func embed(text: String, textFormatContext: TextFormatContext?) throws -> TextEmbedderResult

  #### Parameters

  |---|---|
  | `text` | The `NSString` on which embedding extraction is to be performed. |
  | `textFormatContext` | The `MPPTextFormatContext` to use for formatting the input text. |
  | `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.