render method
Renders messages (with optional tools) into a Llama 3 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 = _toolsSection(tools);
if (toolsSection.isNotEmpty) systemParts.add(toolsSection);
if (systemParts.isNotEmpty) {
_writeTurn(out, 'system', systemParts.join('\n\n'));
}
for (final msg in all.sublist(loopStart)) {
final hasCall = msg.contents.any((c) => c is FunctionCallContent);
_writeTurn(
out,
_roleOf(msg),
_contentFor(msg, images),
terminator: hasCall ? eom : eot,
);
}
if (addGenerationPrompt) {
out
..write(headerStart)
..write('assistant')
..write(headerEnd)
..write('\n\n');
}
return RenderedPrompt(
text: out.toString(),
stopSequences: stopSequences,
media: images,
);
}