embedderInputOfContents function
contents as one embedder request: at most one text part, any image
parts and at most one audio part. Throws ArgumentError for contents an
engine of modalities does not take (checkEmbeddingRequest).
Implementation
EmbedderInput embedderInputOfContents(
List<Content> contents,
Set<EmbedderModality> modalities,
) {
String? text;
final images = <Uint8List>[];
Uint8List? audio;
for (final part in contents) {
switch (part) {
case TextContent(text: final t):
if (text != null) {
throw ArgumentError.value(contents, 'contents', 'has two texts');
}
text = t;
case ImageBytesContent(:final bytes):
images.add(bytes);
case AudioBytesContent(:final bytes):
if (audio != null) {
throw ArgumentError.value(contents, 'contents', 'has two audios');
}
audio = bytes;
case ImageFileContent() || AudioFileContent() || ToolResponseContent():
throw ArgumentError.value(
part,
'contents',
'an embedding takes text, image bytes and audio bytes',
);
}
}
checkEmbeddingRequest(
contents,
modalities: modalities,
hasText: text != null,
hasImages: images.isNotEmpty,
hasAudio: audio != null,
);
return EmbedderInput(text: text, images: images, audio: audio);
}