generateAILearningPrompt static method

Future<String> generateAILearningPrompt()

生成AI学习提示词

Implementation

static Future<String> generateAILearningPrompt() async {
  final dislikes = await getDislikes();
  final feedbackHistory = await getFeedbackHistory();
  final stylePrefs = await _getPreferenceMap(_keyStylePreferences);
  final colorPrefs = await _getPreferenceMap(_keyColorPreferences);

  StringBuffer prompt = StringBuffer();
  prompt.writeln('\n【用户偏好学习数据】');

  // 不喜欢的元素
  if (dislikes.isNotEmpty) {
    prompt.writeln('\n用户明确不喜欢的元素:');
    final recentDislikes = dislikes.reversed.take(10);
    for (var dislike in recentDislikes) {
      final reasons = (dislike['reasons'] as List).join('、');
      prompt.writeln('- ${dislike['itemType']}: $reasons');
    }
  }

  // 风格偏好
  if (stylePrefs.isNotEmpty) {
    final sortedStyles = stylePrefs.entries.toList()
      ..sort((a, b) => b.value.compareTo(a.value));

    final likedStyles = sortedStyles.where((e) => e.value > 0).take(3);
    final dislikedStyles = sortedStyles.where((e) => e.value < 0).take(3);

    if (likedStyles.isNotEmpty) {
      prompt.writeln('\n偏好风格:${likedStyles.map((e) => e.key).join('、')}');
    }
    if (dislikedStyles.isNotEmpty) {
      prompt.writeln('不喜欢的风格:${dislikedStyles.map((e) => e.key).join('、')}');
    }
  }

  // 颜色偏好
  if (colorPrefs.isNotEmpty) {
    final sortedColors = colorPrefs.entries.toList()
      ..sort((a, b) => b.value.compareTo(a.value));

    final likedColors = sortedColors.where((e) => e.value > 0).take(3);
    final dislikedColors = sortedColors.where((e) => e.value < 0).take(3);

    if (likedColors.isNotEmpty) {
      prompt.writeln('\n偏好颜色:${likedColors.map((e) => e.key).join('、')}');
    }
    if (dislikedColors.isNotEmpty) {
      prompt.writeln('不适合的颜色:${dislikedColors.map((e) => e.key).join('、')}');
    }
  }

  // 历史反馈总结
  if (feedbackHistory.isNotEmpty) {
    final recentFeedback = feedbackHistory.reversed.take(5);
    final loved = recentFeedback.where((f) => f['feeling'] == '超爱').length;
    final notSuitable = recentFeedback.where((f) => f['feeling'] == '不太适合我').length;

    if (loved > 0 || notSuitable > 0) {
      prompt.writeln('\n历史反馈:最近${recentFeedback.length}次推荐中,$loved次很满意,$notSuitable次不太适合');
    }
  }

  prompt.writeln('\n请根据以上学习数据,为用户提供更精准的个性化推荐。');

  return prompt.toString();
}