main.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """StockInsightAgent — 智能股票分析助手
  2. 用法:
  3. python main.py 交互菜单
  4. python main.py "问题" 快速分析 (框架 ReAct)
  5. python main.py -d "问题" 深度分析 (框架 PlanSolve)
  6. python main.py -r "问题" 批判分析 (框架 Reflection)
  7. """
  8. import sys
  9. from llm_client import HelloAgentsLLM
  10. from agent import StockInsightAgent
  11. from plan_agent import PlanAndSolveStockAgent
  12. from reflection_agent import ReflectionStockAgent
  13. from framework_agent import FrameworkStockAgent
  14. from memory import memory_get_watchlist, memory_get_history
  15. from rag import rag_import, rag_stats
  16. def show_banner():
  17. print()
  18. print(" ╔═════════════════════════════════════════════════════════════╗")
  19. print(" ║ ║")
  20. print(" ║ StockInsightAgent v2.0 智能股票分析助手 ║")
  21. print(" ║ ║")
  22. print(" ║ 数据: akshare 实时行情 + 财务 + 新闻 ║")
  23. print(" ║ 记忆: 关注列表 | 分析历史 | 用户偏好 ║")
  24. print(" ║ 知识: 估值体系 | 技术指标 | 风控原则 ║")
  25. print(" ║ ║")
  26. print(" ╚═════════════════════════════════════════════════════════════╝")
  27. print()
  28. MENU = """
  29. ┌────────────────────────────────────────────────────────────┐
  30. │ │
  31. │ [1] 快速分析 框架 ReAct ~30秒 │
  32. │ [2] 深度分析 框架 PlanSolve ~2分钟 │
  33. │ [3] 批判分析 框架 Reflection ~3分钟 │
  34. │ │
  35. ├────────────────────────────────────────────────────────────┤
  36. │ │
  37. │ [4] ReAct 手写解析+循环 │
  38. │ [5] PlanSolve 手写规划+执行 │
  39. │ [6] Reflection 手写记忆+反思 │
  40. │ │
  41. ├────────────────────────────────────────────────────────────┤
  42. │ │
  43. │ [w] 查看关注列表 │
  44. │ [h] 查看分析历史 │
  45. │ [k] 导入投资文档到知识库 │
  46. │ │
  47. ├────────────────────────────────────────────────────────────┤
  48. │ │
  49. │ [m] 重新显示菜单 │
  50. │ [0] 退出 │
  51. │ │
  52. └────────────────────────────────────────────────────────────┘
  53. """
  54. EXAMPLES = """
  55. 直接输入问题即可开始分析,例如:
  56. Stock> 分析贵州茅台600519当前估值
  57. Stock> 对比五粮液和茅台的估值
  58. 先选模式,再输入问题:
  59. Stock> 2 (切换到深度分析)
  60. Stock> 全面评估比亚迪002594
  61. """
  62. def main():
  63. # ── 命令行参数快捷模式 ──
  64. if len(sys.argv) > 1:
  65. a = FrameworkStockAgent()
  66. if "-d" in sys.argv:
  67. sys.argv.remove("-d")
  68. q = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else input("问题: ").strip()
  69. print(a.plan_solve(q))
  70. elif "-r" in sys.argv:
  71. sys.argv.remove("-r")
  72. q = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else input("问题: ").strip()
  73. print(a.reflect(q))
  74. else:
  75. q = " ".join(sys.argv[1:])
  76. print(a.react(q))
  77. return
  78. # ── 交互菜单模式 ──
  79. show_banner()
  80. print(MENU)
  81. print(EXAMPLES)
  82. fw = FrameworkStockAgent()
  83. mode = "react" # 当前模式
  84. while True:
  85. try:
  86. q = input("\nStock> ").strip()
  87. except (EOFError, KeyboardInterrupt):
  88. print("\n再见")
  89. break
  90. if not q:
  91. continue
  92. # 菜单选择
  93. if q in ("1", "2", "3", "4", "5", "6", "w", "h", "k", "m", "0"):
  94. if q == "1":
  95. mode = "react"
  96. print(" >> 切换到 [快速分析] 模式。输入你的问题:")
  97. elif q == "2":
  98. mode = "plan"
  99. print(" >> 切换到 [深度分析] 模式。输入你的问题:")
  100. elif q == "3":
  101. mode = "reflect"
  102. print(" >> 切换到 [批判分析] 模式。输入你的问题:")
  103. elif q == "4":
  104. mode = "raw-react"
  105. print(" >> 切换到 [教学版 ReAct] 模式。输入你的问题:")
  106. elif q == "5":
  107. mode = "raw-plan"
  108. print(" >> 切换到 [教学版 PlanSolve] 模式。输入你的问题:")
  109. elif q == "6":
  110. mode = "raw-reflect"
  111. print(" >> 切换到 [教学版 Reflection] 模式。输入你的问题:")
  112. elif q == "w":
  113. print(memory_get_watchlist())
  114. elif q == "h":
  115. code = input(" 股票代码 (回车看全部): ").strip()
  116. print(memory_get_history(code))
  117. elif q == "k":
  118. path = input(" 文档路径: ").strip()
  119. print(rag_import(path))
  120. print(rag_stats())
  121. elif q == "m":
  122. print(MENU)
  123. elif q == "0":
  124. print("再见")
  125. break
  126. continue
  127. # 执行分析
  128. print()
  129. if mode == "react":
  130. print(fw.react(q))
  131. elif mode == "plan":
  132. print(fw.plan_solve(q))
  133. elif mode == "reflect":
  134. print(fw.reflect(q))
  135. elif mode == "raw-react":
  136. StockInsightAgent(HelloAgentsLLM(), max_steps=6).run(q)
  137. elif mode == "raw-plan":
  138. PlanAndSolveStockAgent(HelloAgentsLLM()).run(q)
  139. elif mode == "raw-reflect":
  140. ReflectionStockAgent(HelloAgentsLLM(), max_iterations=2).run(q)
  141. if __name__ == "__main__":
  142. main()