Embeddings¶
Overview¶
On-device embeddings for search, clustering, deduplication and similarity. Every model returns an L2-normalized vector, so the cosine similarity of two vectors is their dot product.
Embeddings are reached through the singleton (JSON) path on both Kotlin and Flutter; the response shape is identical.
Each model has its own vector space. Compare only vectors from the same model
(and, on EmbeddingGemma 2, the same embedding_space_id): an EmbeddingGemma
vector and a MobileCLIP2 vector are not comparable, even when both come from
an image.
Supported Models¶
HF Repo |
Model type |
Inputs |
Vector |
Devices |
|---|---|---|---|---|
|
|
text |
768 floats |
NPU only |
|
|
text, image, audio, video |
768 floats, or a shorter Matryoshka size |
NPU only |
|
|
image |
512 floats |
NPU, CPU |
NPU bundles run on Snapdragon 8 Gen 2 (SM8550), 8 Gen 3 (SM8650), 8 Elite (SM8750) and 8 Elite Gen 5 (SM8850). Per-model device behaviour is under Models.
API Reference¶
Init & Infer¶
Every model is started with start_model and called with infer; only
model_type, engines_path and the input keys change. infer returns
a single-element list.
Kotlin:
import ai.thestage.qlip.TheStageAI
TheStageAI.registerContext(context)
TheStageAI.initialize(api_token = "your-api-token")
TheStageAI.start_model(
model_name = "embed",
model_type = "embedding-gemma",
engines_path = "TheStageAI/embeddinggemma-2",
)
val out = TheStageAI.infer(
model_name = "embed",
input_json = mapOf(
"text" to "How do I reset my password?",
"task" to "query",
),
)[0]
val vector = out["embedding"] as List<Double>
// MobileCLIP2: model_type "mobile-clip-image",
// engines_path "TheStageAI/MobileCLIP2-S0-image-encoder"
val clipOut = TheStageAI.infer(
model_name = "clip",
input_json = mapOf("image" to "/path/to/photo.jpg"),
)
val imageVector = clipOut.firstOrNull()?.get("embedding") as? List<Double>
Flutter:
import 'package:thestage_android_sdk/thestage_android_sdk.dart';
await TheStageFlutterSDK.initialize(api_token: 'your-api-token');
await TheStageFlutterSDK.start_model(
model_name: 'embed',
model_type: 'embedding-gemma',
engines_path: 'TheStageAI/embeddinggemma-2',
);
final out = (await TheStageFlutterSDK.infer(
model_name: 'embed',
input_json: {
'image_path': '/path/to/photo.jpg',
'task': 'document',
},
)).first;
final vector = (out['embedding'] as List).cast<double>();
print(out['embedding_space_id']);
// MobileCLIP2: model_type 'mobile-clip-image',
// engines_path 'TheStageAI/MobileCLIP2-S0-image-encoder'
final clipOut = await TheStageFlutterSDK.infer(
model_name: 'clip',
input_json: {'image': '/path/to/photo.jpg'},
);
final imageVector = clipOut.isEmpty
? null
: (clipOut.first['embedding'] as List).cast<double>();
Media inputs are file paths the app can read. The output always carries
embedding (List<Double>, unit length); the other keys depend on the
model.
Models¶
EmbeddingGemma¶
TheStageAI/embeddinggemma-300m (text) and TheStageAI/embeddinggemma-2
(text, image, audio, video), both under model type embedding-gemma.
Devices. Both models publish one NPU bundle for the chips listed above
and no CPU bundle, so device: "cpu" fails, and so does a start on another
chip. Use the availability check (see Availability in
Model Management) before offering the feature.
Inputs
Key |
Type |
Default |
Notes |
|---|---|---|---|
|
|
none |
Text to embed |
|
|
none |
A task prompt name, EmbeddingGemma 2 only |
|
|
full size |
A Matryoshka size, EmbeddingGemma 2 only |
|
|
none |
An image file, EmbeddingGemma 2 only |
|
|
none |
An audio file, EmbeddingGemma 2 only |
|
|
none |
A video file, EmbeddingGemma 2 only |
|
|
the model’s |
Video frames at most; see Video |
Give text, a media path, or both. On EmbeddingGemma-300m, task or a
dimension other than 768 fails the call.
Outputs
Key |
Type |
Notes |
|---|---|---|
|
|
The unit vector |
|
|
The same vector: base64 of float32 little-endian bytes |
|
|
|
|
|
Length of the vector |
|
|
Length of the vector; EmbeddingGemma 2 only |
|
|
Vectors compare only when this matches; EmbeddingGemma 2 only |
|
|
Tokens of the input sequence; EmbeddingGemma 2 only |
|
|
Input was dropped; EmbeddingGemma 2 only |
embedding_space_id names the model and ends with @<dimension>. Store
it next to each vector and compare only vectors with the same id: two models,
or two Matryoshka sizes of one model, give vectors that are not comparable.
Task prompts. EmbeddingGemma 2 embeds best with the prompt of the task:
task puts that prompt before the input. Use query for a search query
and document for the texts it searches; STS is for sentence
similarity. The bundle carries more names, and an unknown name fails the call
with the list of valid ones. Embed queries and documents with their own
tasks, but compare them in one space: the task does not change
embedding_space_id. EmbeddingGemma-300m takes no task; put any prefix
you want in text itself.
Matryoshka dimensions. dimension on EmbeddingGemma 2 keeps the first
values of the vector and normalizes them again: 768 (full), 512,
256 or 128. A smaller vector is cheaper to store and search. 0 or
768 returns the full vector; any other size fails the call with the list
of valid ones.
Long text. One pass reads up to 2048 tokens, prompt included.
EmbeddingGemma 2 cuts a longer text at that window: the rest is not embedded, and the result has
truncated: true. Split long documents yourself and embed each piece.EmbeddingGemma-300m reads a longer text in 2048-token pieces and returns their average, weighted by token count and normalized.
A text plus media input that passes the window fails the call; it is never cut.
Media inputs. EmbeddingGemma 2 embeds images, audio and video, alone or
together with text. Parts are embedded as one input, in this order: the
task prompt, text, the image, the audio, the video. An image with its
caption gives one vector for both; for separate vectors, make separate calls.
Image. Any format Android’s
BitmapFactorydecodes. A file that cannot be decoded fails the call.Audio. Any format Android’s
MediaCodecdecodes (WAV, MP3, AAC, FLAC, …): the first audio track, channels mixed to mono and resampled for the model. Audio past 30 s is dropped and the result hastruncated: true.
Video. The first video track, sampled at the model’s frame rate, up to 14 frames by default. When the video has more sampled frames than that, frames are taken evenly across the whole video.
max_frameslowers the limit; a larger value is capped to 14.
MobileCLIP2¶
The image encoder of MobileCLIP2-S0: TheStageAI/MobileCLIP2-S0-image-encoder,
model type mobile-clip-image. It turns one image into a 512-float vector.
The SDK ships the image encoder only: it does not embed text.
Devices. The model publishes two bundles:
an NPU bundle for the chips listed above;
a CPU bundle for every other phone, and as the fallback.
With device unset, the SDK starts the NPU bundle on those chips and the
CPU bundle on every other phone. If the NPU bundle does not open on a
supported chip, the start falls back to the CPU bundle on its own.
device: "npu" or "cpu" requires one bundle and fails instead of
falling back. Both bundles return vectors in the same format. See
Choosing the device in Model Management.
Inputs
Key |
Type |
Default |
Notes |
|---|---|---|---|
|
|
— |
Required. Path to an image file the app can read |
Any format Android’s BitmapFactory decodes. The encoder sees the
centered square of the image, scaled to its input size: crop first if the
subject is off-center. EXIF orientation is not applied, so save photos
upright.
Outputs
Key |
Type |
Notes |
|---|---|---|
|
|
512 floats, unit length |
|
|
|
|
|
Crop and scale time of this call |
|
|
Encoder time of this call |
|
|
|
The response is a single-element list, or an empty list when the file is missing or cannot be decoded.
Checklist¶
Model types:
embedding-gemmaforTheStageAI/embeddinggemma-300m(text) andTheStageAI/embeddinggemma-2(text, image, audio, video);mobile-clip-imageforTheStageAI/MobileCLIP2-S0-image-encoder(image).Read
embedding(doubles); vectors are unit length: cosine = dot product. EmbeddingGemma also returnsembedding_b64(float32, little-endian).Compare only vectors of one model: EmbeddingGemma and MobileCLIP2 vectors are in different spaces. On EmbeddingGemma 2, compare only vectors with equal
embedding_space_id.EmbeddingGemma 2:
task(query/document/STS/ …) anddimension(768, 512, 256, 128).EmbeddingGemma 2 cuts text past 2048 tokens (
truncated); EmbeddingGemma-300m averages 2048-token pieces.EmbeddingGemma media are file paths:
image_path,audio_path,video_path(+max_frames). MobileCLIP2 takesimage, a file path; an empty response means the image was not read.EmbeddingGemma runs on the NPU only; MobileCLIP2 runs on the NPU on supported chips and on the CPU everywhere else. Leave
deviceunset for MobileCLIP2 to get that choice.