context method

Future<List<ChatMessage>> context(
  1. String query, {
  2. int recentCount = 8,
  3. int recalled = 4,
})

Context for answering query: up to recalled relevant older messages (wrapped in a system note so the model knows they are excerpts), then the last recentCount messages.

Implementation

Future<List<ChatMessage>> context(
  String query, {
  int recentCount = 8,
  int recalled = 4,
}) async {
  final older = await recall(query, k: recalled, skipRecent: recentCount);
  final latest = await recent(recentCount);
  return [
    if (older.isNotEmpty)
      ChatMessage.system(
        'Earlier in this conversation (excerpts relevant to the current '
        'question):\n${older.map((m) => '${m.role.name}: ${m.content}').join('\n')}',
      ),
    ...latest,
  ];
}