embed method

  1. @override
  2. @nonVirtual
Future<List<double>> embed(
  1. EmbeddingInput input,
  2. TaskType taskType
)
override

tokenizer.encode(taskType.prefix, input.text!) — the tokenizer adapter decides what the prefix means (SigLIP drops it). Throws EmbeddingInputTooLongException when the ids — prefix, BOS and EOS included — are longer than the graph's maxInputTokens; otherwise runs runTokens and finalises the result with the effective mask (the one the graph echoed, else the tokenizer's).

Implementation

@override
@nonVirtual
Future<List<double>> embed(EmbeddingInput input, TaskType taskType) async {
  final tokenizer = _tokenizer;
  final graph = _graph;
  if (tokenizer == null || graph == null) {
    throw StateError('$runtimeType.embed() called before load() succeeded');
  }
  final text = input.text;
  if (text == null || input.modalities.length != 1) {
    throw StateError(
      '$runtimeType embeds text only; got ${input.modalities}',
    );
  }
  final tokenized = tokenizer.encode(taskType.prefix, text);
  final maxInputTokens = graph.maxInputTokens;
  final actualTokens = tokenized.ids.length;
  if (maxInputTokens != null && actualTokens > maxInputTokens) {
    throw EmbeddingInputTooLongException(
      'The input is $actualTokens tokens (task prefix, BOS and EOS '
      'included); this model takes at most $maxInputTokens. Split the text '
      'and embed the pieces.',
      maxInputTokens: maxInputTokens,
      actualTokens: actualTokens,
    );
  }
  final result = await runTokens(tokenized);
  return finalizeEmbedding(
    graph.outputContract,
    result,
    attentionMask: result.attentionMask ?? tokenized.attentionMask,
  );
}