render method

RenderedPrompt render(
  1. Iterable<ChatMessage> messages, {
  2. Iterable<AIFunctionDeclaration> tools = const <AIFunctionDeclaration>[],
  3. bool addGenerationPrompt = true,
})

Renders messages (with optional tools) into a ChatML prompt.

Implementation

RenderedPrompt render(
  Iterable<ChatMessage> messages, {
  Iterable<AIFunctionDeclaration> tools = const <AIFunctionDeclaration>[],
  bool addGenerationPrompt = true,
}) {
  final all = messages.toList();
  final out = StringBuffer();
  final images = <Uint8List>[];

  var loopStart = 0;
  final systemParts = <String>[];
  if (all.isNotEmpty && all.first.role == ChatRole.system) {
    final text = all.first.text;
    if (text.isNotEmpty) systemParts.add(text);
    loopStart = 1;
  }
  final toolsSection = hermesToolsSection(tools);
  if (toolsSection.isNotEmpty) systemParts.add(toolsSection);
  if (systemParts.isNotEmpty) {
    out
      ..write(imStart)
      ..write('system\n')
      ..write(systemParts.join('\n\n'))
      ..write(imEnd)
      ..write('\n');
  }

  for (final msg in all.sublist(loopStart)) {
    out
      ..write(imStart)
      ..write(msg.role.value)
      ..write('\n')
      ..write(_contentFor(msg, images))
      ..write(imEnd)
      ..write('\n');
  }

  if (addGenerationPrompt) {
    out
      ..write(imStart)
      ..write('assistant\n');
  }

  return RenderedPrompt(
    text: out.toString(),
    stopSequences: stopSequences,
    media: images,
  );
}