generateSuggestions method
Generate suggestions for follow-up questions
This method uses the standard chat API with a specialized prompt to generate relevant follow-up questions based on the conversation history. This is a common pattern used by many chatbot implementations.
Implementation
Future<List<String>> generateSuggestions(List<ChatMessage> messages) async {
try {
// Don't generate suggestions for empty conversations
if (messages.isEmpty) {
return [];
}
// Build conversation context (limit to recent messages to avoid token limits)
final recentMessages = messages.length > 10
? messages.sublist(messages.length - 10)
: messages;
final conversationContext =
recentMessages.map((m) => '${m.role.name}: ${m.content}').join('\n');
final systemPrompt = '''
You are a helpful assistant that generates relevant follow-up questions based on conversation history.
Rules:
1. Generate 3-5 questions that naturally continue the conversation
2. Questions should be specific and actionable
3. Avoid repeating topics already covered
4. Return only the questions, one per line
5. No numbering, bullets, or extra formatting
6. Keep questions concise and clear
''';
final userPrompt = '''
Based on this conversation, suggest follow-up questions:
$conversationContext
''';
final response = await _chat.chatWithTools(
[ChatMessage.system(systemPrompt), ChatMessage.user(userPrompt)],
null);
return _parseQuestions(response.text ?? '');
} catch (e) {
// Suggestions are optional, so we log the error but don't throw
_client.logger.warning('Failed to generate suggestions: $e');
return [];
}
}