countSystemTokens method
Count system prompt tokens.
Implementation
Future<({int systemPromptTokens, List<SystemPromptSectionDetail> sections})>
countSystemTokens(List<String> effectiveSystemPrompt) async {
final namedEntries = effectiveSystemPrompt
.where((c) => c.isNotEmpty)
.map((c) => (name: extractSectionName(c), content: c))
.toList();
if (namedEntries.isEmpty) {
return (systemPromptTokens: 0, sections: <SystemPromptSectionDetail>[]);
}
final tokenCounts = await Future.wait(
namedEntries.map(
(entry) => _countTokensWithFallback([
{'role': 'user', 'content': entry.content},
], []),
),
);
final sections = List.generate(namedEntries.length, (i) {
return SystemPromptSectionDetail(
name: namedEntries[i].name,
tokens: tokenCounts[i] ?? 0,
);
});
final total = tokenCounts.fold<int>(0, (sum, t) => sum + (t ?? 0));
return (systemPromptTokens: total, sections: sections);
}