test_sequential_god.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import sys
  2. import os
  3. import json
  4. from typing import List, Dict, Any
  5. # Ensure project root is in python path
  6. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from app.agent.real_god import RealGodAgent
  8. def test_sequential_generation():
  9. print("=== RealGodAgent 顺序生成模式测试 ===")
  10. agent = RealGodAgent(max_steps=5)
  11. prompt = "请生成两位历史上的物理学家:爱因斯坦和牛顿。"
  12. n = 2
  13. print(f"测试提示词: {prompt}")
  14. print(f"计划生成数量: {n} 位 (预期将分 2 次独立执行)\n")
  15. print("-" * 50)
  16. # Simulate the loop in the endpoint
  17. generated_names = []
  18. generated_count = 0
  19. for i in range(n):
  20. print(f"\n🚀 [第 {i+1}/{n} 次循环] 开始生成第 {i+1} 位角色...")
  21. step_count = 0
  22. search_count = 0
  23. current_persona = None
  24. # Each call to agent.run now only generates 1 persona
  25. generator = agent.run(prompt, n=1, generated_names=generated_names)
  26. try:
  27. for event in generator:
  28. e_type = event.get("type")
  29. content = event.get("content")
  30. if e_type == "thought":
  31. step_count += 1
  32. print(f" [思考] {content[:60]}...")
  33. elif e_type == "action":
  34. if "Search" in content or "搜索" in content:
  35. search_count += 1
  36. print(f" [行动] 🔍 触发搜索: {content}")
  37. elif e_type == "observation":
  38. print(f" [观察/搜索结果] 👀: {content}")
  39. elif e_type == "result":
  40. current_persona = content
  41. if isinstance(current_persona, list) and len(current_persona) == 1:
  42. p = current_persona[0]
  43. print(f" [结果] ✅ 成功生成角色: {p.get('name')} ({p.get('title')})")
  44. print(f" Bio长度: {len(p.get('bio', ''))} 字")
  45. print(f" Stance长度: {len(p.get('stance', ''))} 字")
  46. print(f" 完整JSON:\n{json.dumps(p, ensure_ascii=False, indent=2)}")
  47. # Add name to list for next iteration
  48. if p.get('name'):
  49. generated_names.append(p.get('name'))
  50. else:
  51. print(f" [警告] ⚠️ 预期生成 1 位,实际生成 {len(current_persona)} 位")
  52. print(f" 完整JSON:\n{json.dumps(current_persona, ensure_ascii=False, indent=2)}")
  53. elif e_type == "error":
  54. print(f" [错误] ❌: {content}")
  55. except Exception as e:
  56. print(f" [异常] ❌ 执行异常: {e}")
  57. if current_persona:
  58. generated_count += 1
  59. else:
  60. print(" [失败] ❌ 本次循环未生成有效角色")
  61. print(f" [统计] 本次消耗思考步数: {step_count}, 搜索次数: {search_count}")
  62. print(f"\n✅ 已生成名单: {generated_names}")
  63. print("\n" + "-" * 50)
  64. print("=== 测试总结 ===")
  65. if generated_count == n:
  66. print(f"✅ 测试通过: 成功按顺序独立生成了 {generated_count} 位角色。")
  67. else:
  68. print(f"❌ 测试失败: 预期生成 {n} 位,实际成功 {generated_count} 位。")
  69. if __name__ == "__main__":
  70. test_sequential_generation()