main.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """主程序入口"""
  2. import sys
  3. from orchestrator import ColumnWriterOrchestrator
  4. from exporter import ColumnExporter
  5. from config import get_settings
  6. def main():
  7. """主函数"""
  8. print("\n" + "="*70)
  9. print("HelloAgents 专栏编写系统")
  10. print("="*70)
  11. # 获取配置
  12. settings = get_settings()
  13. # 获取主题
  14. if len(sys.argv) > 1:
  15. main_topic = " ".join(sys.argv[1:])
  16. else:
  17. print("\n请输入专栏主题(或直接回车使用默认主题):")
  18. main_topic = input("> ").strip()
  19. if not main_topic:
  20. main_topic = "Python异步编程完全指南"
  21. print(f"使用默认主题:{main_topic}")
  22. # 选择模式
  23. print("\n请选择写作模式:")
  24. print("1. ReActAgent 模式 (默认) - 推理、行动、工具调用 + 独立评审")
  25. print("2. ReflectionAgent 模式 - 自我反思、自动优化(内置评审)")
  26. mode_choice = input("> ").strip()
  27. use_reflection = mode_choice == "2"
  28. # 如果选择 ReAct 模式,询问是否启用评审
  29. if not use_reflection:
  30. print("\n是否启用独立评审流程?(评审后可自动修改优化)")
  31. print("1. 启用评审 (默认) - 生成后评审,不合格自动修改")
  32. print("2. 禁用评审 - 仅生成内容,不进行评审")
  33. review_choice = input("> ").strip()
  34. if review_choice == "2":
  35. settings.enable_review = False
  36. print("▸ 已禁用评审流程")
  37. else:
  38. print(f"▸ 已启用评审流程(通过阈值: {settings.approval_threshold}分)")
  39. try:
  40. # 创建编排器
  41. orchestrator = ColumnWriterOrchestrator(use_reflection_mode=use_reflection)
  42. # 创建专栏
  43. result = orchestrator.create_column(main_topic)
  44. # 导出结果
  45. from datetime import datetime
  46. output_dir = f"output_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
  47. ColumnExporter.export_to_files(result, output_dir)
  48. # 打印统计
  49. print(f"\n{'='*70}")
  50. print(f"▸ 创作统计")
  51. print(f"{'='*70}")
  52. stats = result['statistics']
  53. print(f"文章总数: {stats['total_articles']}")
  54. print(f"总字数: {stats['total_words']:,}")
  55. print(f"平均字数: {stats['avg_words_per_article']:,}")
  56. # 显示创作统计
  57. if 'creation_stats' in result:
  58. creation = result['creation_stats']
  59. print(f"\n创作流程:")
  60. print(f" 生成次数: {creation.get('total_generations', 0)}")
  61. if creation.get('total_reviews', 0) > 0:
  62. print(f" 评审次数: {creation.get('total_reviews', 0)}")
  63. print(f" 一次通过: {creation.get('approved_first_try', 0)}")
  64. if creation.get('total_revisions', 0) > 0:
  65. print(f" 修改次数: {creation.get('total_revisions', 0)}")
  66. if creation.get('total_rewrites', 0) > 0:
  67. print(f" 重写次数: {creation.get('total_rewrites', 0)}")
  68. if 'agent_modes' in result:
  69. print(f"\nAgent 模式:")
  70. print(f" Planner: {result['agent_modes']['planner']}")
  71. print(f" Writer: {result['agent_modes']['writer']}")
  72. if result['agent_modes'].get('reviewer'):
  73. print(f" Reviewer: {result['agent_modes']['reviewer']}")
  74. if result['agent_modes'].get('revision'):
  75. print(f" Revision: {result['agent_modes']['revision']}")
  76. print(f"\n{'='*70}")
  77. print(f"▸ 专栏创建完成!")
  78. print(f" 输出目录: {output_dir}")
  79. print(f"{'='*70}\n")
  80. except KeyboardInterrupt:
  81. print("\n\n⏸️ 用户中断,程序退出")
  82. sys.exit(0)
  83. except Exception as e:
  84. print(f"\n▸ 程序出错: {e}")
  85. import traceback
  86. traceback.print_exc()
  87. sys.exit(1)
  88. if __name__ == "__main__":
  89. main()