EmbeddingModel

interface EmbeddingModel


Provides an interface for generating embeddings for text and multimodal Content.

Obtain instances via EmbeddingGeneration.getEmbeddingModels, and call close when the model is no longer needed to release underlying resources.

Typical usage:

val models = EmbeddingGeneration.getEmbeddingModels()
val model = models.firstOrNull() ?: return
models
.drop(1).forEach { it.close() }
try {
if (model.checkStatus() == FeatureStatus.DOWNLOADABLE) {
model.download().collect()
}
val request = EmbeddingRequest(
Content(listOf(TextPart("What is machine learning?"))),
TaskConfig.RetrievalQuery(),
)
val result = model.generateEmbedding(request)
val vector = result.embedding.vector
} finally {
model.close()
}

Summary

Public functions

suspend @FeatureStatus Int

Checks the current availability status of the embedding feature.

Unit

Releases resources associated with the underlying embedding engine.

Flow<DownloadStatus>

Downloads the required model assets for the embedding feature if they are not already available.

suspend EmbeddingResult

Performs asynchronous content embedding on the provided input request.

suspend EmbeddingMetadata?

Returns the EmbeddingMetadata of the embedding model, or null if metadata retrieval fails or is unavailable.

suspend String

Returns the model signature uniquely identifying the underlying embedding model and version.

suspend Int

Returns the maximum number of input tokens supported by this model in a single request.

suspend Boolean

Returns true if the model is currently deprecated.

suspend Unit

Warms up the inference engine for use by loading necessary models and initializing runtime components, reducing latency for the first generateEmbedding call.

Public functions

checkStatus

suspend fun checkStatus(): @FeatureStatus Int

Checks the current availability status of the embedding feature.

Returns
@FeatureStatus Int

a FeatureStatus constant indicating the feature's readiness.

close

fun close(): Unit

Releases resources associated with the underlying embedding engine.

Callers should invoke this method when the model is no longer needed, including on any unused EmbeddingModel instances returned by EmbeddingGeneration.getEmbeddingModels.

download

fun download(): Flow<DownloadStatus>

Downloads the required model assets for the embedding feature if they are not already available.

The download starts when the returned cold Flow is collected. If the download fails, the flow emits DownloadStatus.DownloadFailed and terminates with the corresponding com.google.mlkit.genai.common.GenAiException.

Returns
Flow<DownloadStatus>

a cold Flow that emits DownloadStatus updates for download progress.

generateEmbedding

suspend fun generateEmbedding(request: EmbeddingRequest): EmbeddingResult

Performs asynchronous content embedding on the provided input request.

Parameters
request: EmbeddingRequest

the EmbeddingRequest containing input Content and optional TaskConfig.

Returns
EmbeddingResult

an EmbeddingResult containing the generated embedding vector and model signature.

Throws
com.google.mlkit.genai.common.GenAiException

if embedding generation fails or the request content is invalid.

getMetadata

suspend fun getMetadata(): EmbeddingMetadata?

Returns the EmbeddingMetadata of the embedding model, or null if metadata retrieval fails or is unavailable.

getModelSignature

suspend fun getModelSignature(): String

Returns the model signature uniquely identifying the underlying embedding model and version.

Embeddings generated by models with different signatures are incompatible and should not be directly compared. Callers that persist embeddings should store this signature alongside the vectors to detect model updates and re-index when the signature changes.

Returns
String

the model signature string.

Throws
com.google.mlkit.genai.common.GenAiException

if retrieving the model signature fails.

getTokenLimit

suspend fun getTokenLimit(): Int

Returns the maximum number of input tokens supported by this model in a single request.

Returns
Int

the maximum input token capacity, -1 if the token limit is unknown, or Int.MAX_VALUE if there is no token limit restriction.

Throws
com.google.mlkit.genai.common.GenAiException

if retrieving the token limit fails.

isDeprecated

suspend fun isDeprecated(): Boolean

Returns true if the model is currently deprecated.

A model is considered deprecated if the current UTC date is on or after EmbeddingMetadata.deprecationDate. Returns false if model metadata or the deprecation date is unavailable.

warmup

suspend fun warmup(): Unit

Warms up the inference engine for use by loading necessary models and initializing runtime components, reducing latency for the first generateEmbedding call.

Throws
com.google.mlkit.genai.common.GenAiException

if initializing the inference engine fails.