embed method

Future<List<Embedding>> embed(
  1. List<String> texts, {
  2. String? model,
  3. EmbedOptions? options,
})

Embed texts, returning one vector per input in input order.

final vectors = await RunAnywhere.embeddings.embed(['hello', 'world']);
print(vectors.first.vector.length);

Throws SDKException when no embedding model is loadable.

Implementation

Future<List<Embedding>> embed(
  List<String> texts, {
  String? model,
  EmbedOptions? options,
}) async {
  if (texts.isEmpty) return const <Embedding>[];
  await ModelGate.ensureLoaded(
    modelId: model,
    category: ModelCategory.MODEL_CATEGORY_EMBEDDING,
  );
  final modelId =
      await ModelGate.currentId(ModelCategory.MODEL_CATEGORY_EMBEDDING);
  if (modelId == null) {
    throw SDKException.componentNotReady('Embeddings');
  }
  final result = await DartBridgeEmbeddings.shared.embedBatchAsync(
    EmbeddingsRequest(
      texts: texts,
      options: (options ?? EmbedOptions()).toProto(),
      modelId: modelId,
    ),
  );
  // `EmbeddingsResult.error` was deleted outright (idl/embeddings_options.
  // proto) — failure surfaces as an empty `vectors` list, not a typed
  // error field.
  final vectors = result.vectors.map(Embedding.fromProto).toList()
    ..sort((a, b) => a.index.compareTo(b.index));
  return List<Embedding>.unmodifiable(vectors);
}