send method

  1. @override
Stream<OmnixConversationEvent> send(
  1. String prompt, {
  2. Uint8List? imageBytes,
  3. Uint8List? audioBytes,
})
override

Adds a user turn and streams the model response.

Implementation

@override
Stream<OmnixConversationEvent> send(
  String prompt, {
  Uint8List? imageBytes,
  Uint8List? audioBytes,
}) async* {
  if (_closed) throw StateError('Conversation is closed.');
  if (_generating) throw StateError('Generation is already in progress.');
  final text = prompt.trim();
  if (text.isEmpty) throw ArgumentError.value(prompt, 'prompt', 'is empty');
  if (imageBytes != null && !_chat.supportsImages) {
    throw ArgumentError.value(
      imageBytes,
      'imageBytes',
      'Conversation was not opened with image support.',
    );
  }
  if (audioBytes != null && !_chat.supportAudio) {
    throw ArgumentError.value(
      audioBytes,
      'audioBytes',
      'Conversation was not opened with audio support.',
    );
  }

  _generating = true;
  try {
    await _chat.addQuery(
      Message(
        text: text,
        isUser: true,
        imageBytes: imageBytes,
        images: imageBytes == null ? const [] : [imageBytes],
        audioBytes: audioBytes,
      ),
    );
    await for (final response in _chat.generateChatResponseAsync()) {
      for (final event in mapFlutterGemmaResponse(response)) {
        yield event;
      }
    }
  } finally {
    _generating = false;
  }
}