injectMediaPathsToConfig method

GenerationConfig injectMediaPathsToConfig(
  1. List<VlmChatMessage> messages,
  2. GenerationConfig baseConfig
)

Inject media paths (images/audio) into generation config

Implementation

GenerationConfig injectMediaPathsToConfig(
  List<VlmChatMessage> messages,
  GenerationConfig baseConfig,
) {
  final imagePaths = <String>[];
  final audioPaths = <String>[];

  for (final message in messages) {
    for (final content in message.contents) {
      if (content.type == 'image') {
        imagePaths.add(content.content);
      } else if (content.type == 'audio') {
        audioPaths.add(content.content);
      }
    }
  }

  return GenerationConfig(
    maxTokens: baseConfig.maxTokens,
    stopWords: baseConfig.stopWords,
    stopCount: baseConfig.stopCount,
    nPast: baseConfig.nPast,
    samplerConfig: baseConfig.samplerConfig,
    imagePaths: imagePaths.isNotEmpty ? imagePaths : baseConfig.imagePaths,
    imageCount: imagePaths.isNotEmpty ? imagePaths.length : baseConfig.imageCount,
    audioPaths: audioPaths.isNotEmpty ? audioPaths : baseConfig.audioPaths,
    audioCount: audioPaths.isNotEmpty ? audioPaths.length : baseConfig.audioCount,
  );
}