Text embedding guide

Two example sentences that show the corresponding embeddings for each
word in the sentences as an array.

The MediaPipe Text Embedder task lets you create a numeric representation of text data to capture its semantic meaning. This functionality is frequently used to compare the semantic similarity of two pieces of text using mathematical comparison techniques such as Cosine Similarity. This task operates on text data with a machine learning (ML) model, and outputs a numeric representation of the text data as a list of high-dimensional feature vectors, also known as embedding vectors, in either floating-point or quantized form.

Try it!

Get Started

Start using this task by following one of these implementation guides for your target platform. These platform-specific guides walk you through a basic implementation of this task, including a recommended model, and code example with recommended configuration options:

Task details

This section describes the capabilities, inputs, outputs, and configuration options of this task.

Features

  • Input text processing - Supports out-of-graph tokenization for models without in-graph tokenization.
  • Embedding similarity computation - Built-in utility function to compute the cosine similarity between two feature vectors.
  • Quantization - Supports scalar quantization for the feature vectors.
Task inputs Task outputs
Text Embedder accepts the following input for processing:
  • Text (String)
  • Optional formatting context (TextFormatContext) for supported models like the EmbeddingGemma 300m model.
Text Embedder outputs a list of embeddings consisting of:
  • Embedding: the feature vector itself, either in floating-point form or scalar-quantized.
  • Head index: the index for the head that produced this embedding.
  • Head name (optional): the name of the head that produced this embedding.

Configurations options

This task has the following configuration options:

Option Name Description Value Range Default Value
l2_normalize Whether to normalize the returned feature vector with L2 norm. Use this option only if the model does not already contain a native L2_NORMALIZATION TFLite Op. In most cases, this is already the case and L2 normalization is thus achieved through TFLite inference with no need for this option. Boolean False
quantize Whether the returned embedding should be quantized to bytes via scalar quantization. Embeddings are implicitly assumed to be unit-norm and therefore any dimension is guaranteed to have a value in [-1.0, 1.0]. Use the l2_normalize option if this is not the case. Boolean False

Models

We support the smaller UniversalSentenceEncoder model and the more capable EmbeddingModel for more advanced use cases when you start developing with this task.

Embedding Gemma 300m model

The TextEmbedder SDK supports the embeddinggemma-300m. The provided task file uses a maximum sequence length of 512 tokens.

This model handles input differently based on the task type and context format you provide. For the query formats, the template follows task: <task> | query: <text>, and for document formats, it follows title: <title> | text: <text>. The formats follows the official EmbeddingGemma prompt instruction.

Task Type Formatting
RETRIEVAL_DOCUMENT Uses document format with the provided title.
RETRIEVAL_QUERY Uses query format with <task> as "search result".
SEMANTIC_SIMILARITY Uses query format with <task> as "sentence similarity".
CLASSIFICATION Uses query format with <task> as "classification".
CLUSTERING Uses query format with <task> as "clustering".
QUESTION_ANSWERING /
FACT_CHECKING /
CODE_RETRIEVAL
Depending on whether the role is a query or document, it uses the respective format. When acting as a query, the <task> string corresponds to "question answering", "fact checking", or "code retrieval" respectively.
Default Any other task type falls back to the query format with <task> as "search result".
Model name Input Quantization type Versions
Embedding Gemma string Mixed Precision (int4+int8) Latest

Universal Sentence Encoder model

This model uses a dual encoder architecture and was trained on various question-answer datasets.

Consider the following pairs of sentences:

  • ("it's a charming and often affecting journey", "what a great and fantastic trip")
  • ("I like my phone", "I hate my phone")
  • ("This restaurant has a great gimmick", "We need to double-check the details of our plan")

The text embeddings in the first two pairs will have a higher cosine similarity than the embeddings in the third pair because the first two pairs of sentences share a common topic of "trip sentiment" and "phone opinion" respectively while the third pair of sentences do not share a common topic.

Note that although the two sentences in the second pair have opposing sentiments, they have a high similarity score because they share a common topic.

Model name Input Quantization type Versions
Universal Sentence Encoder string, string, string None (float32) Latest

Task benchmarks

Here's the task benchmarks for the whole pipeline based on the above pre-trained models. The latency result is the average latency on Samsung S26 Ultra CPU with 4 threads. More detailed performance benchmark for EmbeddingGemma

Model Name CPU Latency (ms)
Universal Sentence Encoder 10
Embedding Gemma 300m 200