<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L177-L334) |

Class that performs embedding extraction on text.

```python
mp.tasks.text.TextEmbedder(
    lib: serial_dispatcher.SerialDispatcher, handle: ctypes.c_void_p
)
```

This API expects a TFLite model with TFLite Model Metadata that contains the
mandatory (described below) input tensors and output tensors. Metadata should
contain the input process unit for the model's Tokenizer as well as input /
output tensor metadata.

<br />

<br />

<br />

| ## Input tensors ||
|---|---|
| (kTfLiteInt32) - 3 input tensors of size `[batch_size x bert_max_seq_len]` with names "ids", "mask", and "segment_ids" representing the input ids, mask ids, and segment ids respectively. - or 1 input tensor of size `[batch_size x max_seq_len]` representing the input ids. ||

<br />

At least one output tensor with:
(kTfLiteFloat32)
- `N` components corresponding to the `N` dimensions of the returned
feature vector for this output layer.
- Either 2 or 4 dimensions, i.e. `[1 x N]` or `[1 x 1 x 1 x N]`.

## Methods

### `close`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L318-L323)

```python
close()
```

Shuts down the MediaPipe task instance.

### `cosine_similarity`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L292-L316)

```python
@classmethod
cosine_similarity(
    u: `mp.tasks.components.containers.Embedding`,
    v: `mp.tasks.components.containers.Embedding`
) -> float
```

Utility function to compute cosine similarity between two embedding entries.

May return an InvalidArgumentError if e.g. the feature vectors are
of different types (quantized vs. float), have different sizes, or have a
an L2-norm of 0.

<br />

<br />

<br />

| Args ||
|---|---|
| `u` | An embedding entry. |
| `v` | An embedding entry. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The cosine similarity for the two embeddings. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | May return an error if e.g. the feature vectors are of different types (quantized vs. float), have different sizes, or have an L2-norm of 0. |

<br />

### `create_from_model_path`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L210-L228)

```python
@classmethod
create_from_model_path(
    model_path: str
) -> 'TextEmbedder'
```

Creates an `TextEmbedder` object from a TensorFlow Lite model and the default `TextEmbedderOptions`.

<br />

<br />

<br />

| Args ||
|---|---|
| `model_path` | Path to the model. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `TextEmbedder` object that's created from the model file and the default `TextEmbedderOptions`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If failed to create `TextEmbedder` object from the provided file such as invalid file path. |
| `RuntimeError` | If other types of error occurred. |

<br />

### `create_from_options`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L230-L251)

```python
@classmethod
create_from_options(
    options: `mp.tasks.text.TextEmbedderOptions`
) -> 'TextEmbedder'
```

Creates the `TextEmbedder` object from text embedder options.

<br />

<br />

<br />

| Args ||
|---|---|
| `options` | Options for the text embedder task. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `TextEmbedder` object that's created from `options`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If failed to create `TextEmbedder` object from `TextEmbedderOptions` such as missing the model. |
| `RuntimeError` | If other types of error occurred. |

<br />

### `embed`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L253-L290)

```python
embed(
    text: str,
    format_context: Optional[`mp.tasks.text.TextEmbeddingFormatContext`] = None
) -> `mp.tasks.components.containers.EmbeddingResult`
```

Performs text embedding extraction on the provided text.

<br />

<br />

<br />

| Args ||
|---|---|
| `text` | The input text. |
| `format_context` | Optional formatting options for models that require prompt formatting, such as the family of Gecko models. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An embedding result object that contains a list of embeddings. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If any of the input arguments is invalid. |
| `RuntimeError` | If text embedder failed to run. |

<br />

### `__enter__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L325-L327)

```python
__enter__()
```

Returns `self` upon entering the runtime context.

### `__exit__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_embedder.py#L329-L331)

```python
__exit__(
    unused_exc_type, unused_exc_value, unused_traceback
)
```

Shuts down the MediaPipe task instance on exit of the context manager.