countMessageTokens method
Counts total tokens for a list of chat messages.
Each message is expected to have role and content keys. An overhead
of _messageOverhead tokens is added per message to account for special
tokens and role markers.
Implementation
int countMessageTokens(List<Map<String, String>> messages) {
var total = 3; // Every conversation has a fixed priming overhead.
for (final msg in messages) {
total += _messageOverhead;
final role = msg['role'] ?? '';
final content = msg['content'] ?? '';
total += _encoder.count(role);
total += _encoder.count(content);
}
return total;
}