embed method
The vector of input: its text (task prefix already in front), its
images and its audio, as one request. Throws
EmbedderInputTooLongException for an input the engine had to cut or
split (never the cut or averaged vector), EmbedderEngineException for
any other failure.
Implementation
Future<List<double>> embed(EmbedderInput input) async {
final engine = _engine;
if (_state != _EmbedderState.loaded || engine == null) {
throw const EmbedderEngineException(
'LiteRtLmWebEmbedder.embed() needs a loaded, open embedder',
);
}
// The native order (`embedding_parts.dart`): the text with its task
// prefix — the engine adds BOS and EOS — then the images in order, then
// the audio. A media part is the encoded file as it is, never prefixed.
final parts = <LiteRtLmWebInput>[
if (input.text case final text?) LiteRtLmWebText(text),
for (final image in input.images) LiteRtLmWebImage(image),
if (input.audio case final audio?) LiteRtLmWebAudio(audio),
];
final images = input.images.isNotEmpty;
final audio = input.audio != null;
if (parts.isEmpty || (images && !_images) || (audio && !_audio)) {
throw EmbedderEngineException(
'LiteRT-LM embeds text and the media $_file was loaded with: '
'${[if (input.text != null) 'text', if (images) '${input.images.length} image(s)', if (audio) 'audio'].join(', ')} '
'is not that.',
);
}
final request = parts.length == 1 ? parts.single : LiteRtLmWebParts(parts);
// A lone image is a fixed number of vision tokens, far below the limit:
// if the engine cuts or splits one, that is not an input the caller could
// shorten.
final loneImage = request is LiteRtLmWebImage;
final what = switch (request) {
LiteRtLmWebText() => 'text',
LiteRtLmWebImage() => 'image',
LiteRtLmWebAudio() => 'audio clip',
LiteRtLmWebParts() => 'combined input',
};
final LiteRtLmWebEmbedding response;
try {
response = await liteRtLmPageLock.run(() {
// Closed while it waited for the page: the engine is deleted.
_checkNotClosed();
return engine.computeEmbedding(request, _options);
});
} on LiteRtLmPagePoisonedError {
// The page's state, not this embedding's: the caller tells them apart.
rethrow;
} catch (e) {
throw EmbedderEngineException(
'LiteRT-LM could not embed the $what for $_file: $e',
cause: e,
);
}
final media = images || audio;
if (response.truncatedLength case final limit?) {
if (loneImage) {
throw EmbedderEngineException(
'LiteRT-LM truncated an image of $_file at $limit of '
'${response.inputLength} tokens',
);
}
throw _tooLong(response.inputLength, limit, media: media);
}
if (response.numChunks > 1) {
// WebGPU: the engine averaged windows of the input instead of cutting
// it — a vector of a different kind than every other in the index.
final limit = _maxInputTokens;
if (loneImage || limit == null) {
throw EmbedderEngineException(
'LiteRT-LM split the ${response.inputLength}-token $what for $_file '
'into ${response.numChunks} windows and averaged them'
'${limit == null ? ' (its window is unknown)' : ''}; nothing was '
'returned for it.${loneImage ? '' : ' Split the input and embed the pieces.'}',
);
}
throw _tooLong(response.inputLength, limit, media: media);
}
final vector = response.embedding;
if (vector.length != _dimension) {
throw EmbedderEngineException(
'$_file returned a vector of ${vector.length} values for dimension '
'$_dimension',
);
}
if (_unusable(vector)) {
throw EmbedderEngineException(
'LiteRT-LM returned a vector with NaN, infinity or only zeros for '
'the $what of $_file on ${_config.backend.name}',
);
}
return vector;
}