generateEmbedding static method
Generate embedding for a single text.
text - Text to embed
Returns List<double> - Embedding vector (768 dimensions)
Throws Exception if not initialized or generation fails
Implementation
static Future<List<double>> generateEmbedding(String text) async {
if (!isInitialized()) {
throw StateError('LiteRT embeddings not initialized. Call initialize() first.');
}
if (text.trim().isEmpty) {
throw ArgumentError('Text must not be empty');
}
try {
// Call JS function and get Float32Array
final jsResult = await _generateEmbeddingJS(text.toJS).toDart;
// Convert JS Float32Array to Dart List<double>
final jsArray = jsResult as JSFloat32Array;
final length = jsArray.length.toDartInt;
final result = List<double>.filled(length, 0.0);
for (int i = 0; i < length; i++) {
result[i] = jsArray[i.toJS].toDartDouble;
}
return result;
} catch (e) {
throw Exception('Failed to generate embedding: $e');
}
}