test_evaluation_v2.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. """
  2. 测试评估V2模块
  3. 从现有run_context.json读取帖子,使用V2评估模块重新评估,生成统计报告
  4. """
  5. import asyncio
  6. import json
  7. import sys
  8. from pathlib import Path
  9. from datetime import datetime
  10. from collections import defaultdict
  11. # 导入必要的模块
  12. from knowledge_search_traverse import Post
  13. from post_evaluator_v2 import evaluate_post_v2, apply_evaluation_v2_to_post
  14. async def test_evaluation_v2(run_context_path: str, max_posts: int = 10):
  15. """
  16. 测试V2评估模块
  17. Args:
  18. run_context_path: run_context.json路径
  19. max_posts: 最多评估的帖子数量(用于快速测试)
  20. """
  21. print(f"\n{'='*80}")
  22. print(f"📊 评估V2测试 - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  23. print(f"{'='*80}\n")
  24. # 读取run_context.json
  25. print(f"📂 读取: {run_context_path}")
  26. with open(run_context_path, 'r', encoding='utf-8') as f:
  27. run_context = json.load(f)
  28. # 提取原始query
  29. original_query = run_context.get('o', '')
  30. print(f"🔍 原始Query: {original_query}\n")
  31. # 提取所有帖子 (从rounds -> search_results -> post_list)
  32. post_data_list = []
  33. rounds = run_context.get('rounds', [])
  34. for round_idx, round_data in enumerate(rounds):
  35. search_results = round_data.get('search_results', [])
  36. for search_idx, search in enumerate(search_results):
  37. post_list = search.get('post_list', [])
  38. for post_idx, post_data in enumerate(post_list):
  39. # 生成唯一ID
  40. post_id = f"r{round_idx}_s{search_idx}_p{post_idx}"
  41. post_data_list.append((round_idx, search_idx, post_id, post_data))
  42. total_posts = len(post_data_list)
  43. print(f"📝 找到 {total_posts} 个帖子 (来自 {len(rounds)} 轮)")
  44. # 限制评估数量(快速测试)
  45. if max_posts and max_posts < total_posts:
  46. post_data_list = post_data_list[:max_posts]
  47. print(f"⚡ 快速测试模式: 仅评估前 {max_posts} 个帖子\n")
  48. else:
  49. print()
  50. # 将post_data转换为Post对象
  51. posts = []
  52. for round_idx, search_idx, post_id, post_data in post_data_list:
  53. post = Post(
  54. note_id=post_data.get('note_id', post_id),
  55. title=post_data.get('title', ''),
  56. body_text=post_data.get('body_text', ''),
  57. images=post_data.get('images', []),
  58. type=post_data.get('type', 'normal')
  59. )
  60. posts.append((round_idx, search_idx, post_id, post))
  61. # 批量评估
  62. print(f"🚀 开始批量评估 (并发数: 5)...\n")
  63. semaphore = asyncio.Semaphore(5)
  64. tasks = []
  65. for round_idx, search_idx, post_id, post in posts:
  66. task = evaluate_post_v2(post, original_query, semaphore)
  67. tasks.append((round_idx, search_idx, post_id, post, task))
  68. results = []
  69. for i, (round_idx, search_idx, post_id, post, task) in enumerate(tasks, 1):
  70. print(f" [{i}/{len(tasks)}] 评估: {post.note_id}")
  71. knowledge_eval, relevance_eval = await task
  72. if knowledge_eval:
  73. # 应用评估结果(可能只有知识评估,没有相关性评估)
  74. apply_evaluation_v2_to_post(post, knowledge_eval, relevance_eval)
  75. results.append((round_idx, search_idx, post_id, post, knowledge_eval, relevance_eval))
  76. # 输出已经在 evaluate_post_v2 中打印过了,这里不重复打印
  77. else:
  78. print(f" ❌ 评估失败")
  79. print(f"\n✅ 评估完成: {len(results)}/{len(posts)} 成功\n")
  80. # 更新run_context.json中的帖子数据
  81. print("💾 更新 run_context.json...")
  82. for round_idx, search_idx, post_id, post, knowledge_eval, relevance_eval in results:
  83. # 定位到对应的post_list
  84. if round_idx < len(rounds):
  85. search_results = rounds[round_idx].get('search_results', [])
  86. if search_idx < len(search_results):
  87. post_list = search_results[search_idx].get('post_list', [])
  88. # 找到对应的帖子并更新
  89. for p in post_list:
  90. if p.get('note_id') == post.note_id:
  91. # 更新顶层字段
  92. p['is_knowledge'] = post.is_knowledge
  93. p['knowledge_reason'] = post.knowledge_reason
  94. p['knowledge_score'] = post.knowledge_score
  95. p['knowledge_level'] = post.knowledge_level
  96. p['relevance_score'] = post.relevance_score
  97. p['relevance_level'] = post.relevance_level
  98. p['relevance_reason'] = post.relevance_reason
  99. p['relevance_conclusion'] = post.relevance_conclusion
  100. p['evaluation_time'] = post.evaluation_time
  101. p['evaluator_version'] = post.evaluator_version
  102. # 更新嵌套字段
  103. p['knowledge_evaluation'] = post.knowledge_evaluation
  104. p['relevance_evaluation'] = post.relevance_evaluation
  105. break
  106. # 保存更新后的run_context.json
  107. output_path = run_context_path.replace('.json', '_v2.json')
  108. with open(output_path, 'w', encoding='utf-8') as f:
  109. json.dump(run_context, f, ensure_ascii=False, indent=2)
  110. print(f"✅ 已保存: {output_path}\n")
  111. # 生成统计报告
  112. print(f"\n{'='*80}")
  113. print("📊 统计报告")
  114. print(f"{'='*80}\n")
  115. # 知识评估统计
  116. knowledge_counts = defaultdict(int)
  117. knowledge_level_counts = defaultdict(int)
  118. knowledge_scores = []
  119. for _, _, _, post, _, _ in results:
  120. if post.is_knowledge:
  121. knowledge_counts['知识内容'] += 1
  122. else:
  123. knowledge_counts['非知识内容'] += 1
  124. if post.knowledge_level:
  125. knowledge_level_counts[post.knowledge_level] += 1
  126. if post.knowledge_score is not None:
  127. knowledge_scores.append(post.knowledge_score)
  128. total = len(results)
  129. print("📚 知识评估:")
  130. print(f" 知识内容: {knowledge_counts['知识内容']:3d} / {total} ({knowledge_counts['知识内容']/total*100:.1f}%)")
  131. print(f" 非知识内容: {knowledge_counts['非知识内容']:3d} / {total} ({knowledge_counts['非知识内容']/total*100:.1f}%)")
  132. print()
  133. if knowledge_scores:
  134. avg_score = sum(knowledge_scores) / len(knowledge_scores)
  135. print(f" 平均得分: {avg_score:.1f}分")
  136. print(f" 最高得分: {max(knowledge_scores):.0f}分")
  137. print(f" 最低得分: {min(knowledge_scores):.0f}分")
  138. print()
  139. print(" 星级分布:")
  140. for level in range(1, 6):
  141. count = knowledge_level_counts.get(level, 0)
  142. bar = '★' * count
  143. print(f" {level}星: {count:3d} {bar}")
  144. print()
  145. # 相关性评估统计
  146. relevance_conclusion_counts = defaultdict(int)
  147. relevance_scores = []
  148. purpose_scores = []
  149. category_scores = []
  150. for _, _, _, post, _, _ in results:
  151. if post.relevance_conclusion:
  152. relevance_conclusion_counts[post.relevance_conclusion] += 1
  153. if post.relevance_score is not None:
  154. relevance_scores.append(post.relevance_score)
  155. if post.relevance_evaluation:
  156. if 'purpose_score' in post.relevance_evaluation:
  157. purpose_scores.append(post.relevance_evaluation['purpose_score'])
  158. if 'category_score' in post.relevance_evaluation:
  159. category_scores.append(post.relevance_evaluation['category_score'])
  160. print("🎯 相关性评估:")
  161. for conclusion in ['高度匹配', '中度匹配', '低度匹配', '不匹配']:
  162. count = relevance_conclusion_counts.get(conclusion, 0)
  163. if count > 0:
  164. print(f" {conclusion}: {count:3d} / {total} ({count/total*100:.1f}%)")
  165. print()
  166. if relevance_scores:
  167. avg_score = sum(relevance_scores) / len(relevance_scores)
  168. high_relevance = sum(1 for s in relevance_scores if s >= 70)
  169. print(f" 平均得分: {avg_score:.1f}分")
  170. print(f" 高相关性: {high_relevance} / {total} ({high_relevance/total*100:.1f}%) [≥70分]")
  171. print(f" 最高得分: {max(relevance_scores):.0f}分")
  172. print(f" 最低得分: {min(relevance_scores):.0f}分")
  173. print()
  174. if purpose_scores and category_scores:
  175. avg_purpose = sum(purpose_scores) / len(purpose_scores)
  176. avg_category = sum(category_scores) / len(category_scores)
  177. print(f" 目的性平均: {avg_purpose:.1f}分 (权重70%)")
  178. print(f" 品类平均: {avg_category:.1f}分 (权重30%)")
  179. print()
  180. # 综合分析
  181. print("🔥 高质量内容 (知识内容 + 高相关性):")
  182. high_quality = sum(
  183. 1 for _, _, _, post, _, _ in results
  184. if post.is_knowledge and post.relevance_score and post.relevance_score >= 70
  185. )
  186. print(f" {high_quality} / {total} ({high_quality/total*100:.1f}%)")
  187. print()
  188. print(f"{'='*80}\n")
  189. return results
  190. if __name__ == "__main__":
  191. if len(sys.argv) < 2:
  192. print("用法: python3 test_evaluation_v2.py <run_context.json路径> [最大评估数量]")
  193. print()
  194. print("示例:")
  195. print(" python3 test_evaluation_v2.py input/test_case/output/knowledge_search_traverse/20251112/173512_dc/run_context.json")
  196. print(" python3 test_evaluation_v2.py input/test_case/output/knowledge_search_traverse/20251112/173512_dc/run_context.json 20")
  197. sys.exit(1)
  198. run_context_path = sys.argv[1]
  199. max_posts = int(sys.argv[2]) if len(sys.argv) > 2 else None
  200. asyncio.run(test_evaluation_v2(run_context_path, max_posts))