TextEmbedder

public final class TextEmbedder implements AutoCloseable

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.

Summary

Nested types

The embedding task type, used to format input text.

public abstract class TextEmbedder.TextEmbedderOptions extends TaskOptions

Options for setting up a TextEmbedder.

Builder for TextEmbedderOptions.

public abstract class TextEmbedder.TextFormatContext

Encapsulates formatting instructions for models that require it (like Gecko).

Builder for TextFormatContext.

The role of the text in the context of the embedding task.

Public methods

void

Closes and cleans up the TextEmbedder.

static double

Utility function to compute cosine similarity between two Embedding objects.

static TextEmbedder
createFromFile(Context context, File modelFile)

Creates a TextEmbedder instance from a model file and the default TextEmbedderOptions.

static TextEmbedder
createFromFile(Context context, String modelPath)

Creates a TextEmbedder instance from a model file and the default TextEmbedderOptions.

static TextEmbedder

Creates a TextEmbedder instance from TextEmbedderOptions.

TextEmbedderResult
embed(String inputText)

Performs embedding extraction on the input text.

TextEmbedderResult
embed(String inputText, TextEmbedder.TextFormatContext formatContext)

Performs embedding extraction on the input text, with optional formatting support for Gecko models.

@Nullable EmbeddingProvider

Returns a EmbeddingProvider for this embedder.

Public methods

close

public void close()

Closes and cleans up the TextEmbedder.

cosineSimilarity

public static double cosineSimilarity(Embedding u, Embedding v)

Utility function to compute cosine similarity between two Embedding objects.

Throws
java.lang.IllegalArgumentException

if the embeddings are of different types (float vs. quantized), have different sizes, or have an L2-norm of 0.

createFromFile

public static TextEmbedder createFromFile(Context context, File modelFile)

Creates a TextEmbedder instance from a model file and the default TextEmbedderOptions.

Parameters
Context context

an Android Context.

File modelFile

the text model File instance.

Throws
java.io.IOException

if an I/O error occurs when opening the tflite model file.

com.google.mediapipe.framework.MediaPipeException

if there is an error during TextEmbedder creation.

createFromFile

public static TextEmbedder createFromFile(Context context, String modelPath)

Creates a TextEmbedder instance from a model file and the default TextEmbedderOptions.

Parameters
Context context

an Android Context.

String modelPath

path to the text model with metadata in the assets.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is is an error during TextEmbedder creation.

createFromOptions

public static TextEmbedder createFromOptions(
    Context context,
    TextEmbedder.TextEmbedderOptions options
)

Creates a TextEmbedder instance from TextEmbedderOptions.

Parameters
Context context

an Android Context.

TextEmbedder.TextEmbedderOptions options

a TextEmbedderOptions instance.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during TextEmbedder creation.

embed

public TextEmbedderResult embed(String inputText)

Performs embedding extraction on the input text.

Parameters
String inputText

a String for processing.

embed

public TextEmbedderResult embed(String inputText, TextEmbedder.TextFormatContext formatContext)

Performs embedding extraction on the input text, with optional formatting support for Gecko models.

Parameters
String inputText

a String for processing.

TextEmbedder.TextFormatContext formatContext

a TextFormatContext for Gecko model formatting.

getProvider

public @Nullable EmbeddingProvider getProvider()

Returns a EmbeddingProvider for this embedder.