test_analysis_0415.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. 临时测试:基于20260415数据进行分析
  3. """
  4. import asyncio
  5. import os
  6. import sys
  7. from pathlib import Path
  8. # 代理设置
  9. os.environ.setdefault("HTTP_PROXY", "http://127.0.0.1:29758")
  10. os.environ.setdefault("HTTPS_PROXY", "http://127.0.0.1:29758")
  11. # 添加项目根目录到 Python 路径
  12. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  13. from dotenv import load_dotenv
  14. load_dotenv()
  15. from agent.core.runner import AgentRunner
  16. from agent.trace import FileSystemTraceStore
  17. from agent.llm import create_openrouter_llm_call
  18. from agent.utils import setup_logging
  19. from examples.auto_put_ad_mini.config import (
  20. MAIN_CONFIG, SKILLS_DIR, TRACE_STORE_PATH, LOG_LEVEL, LOG_FILE,
  21. )
  22. # 导入自定义工具
  23. from examples.auto_put_ad_mini.tools.data_query import fetch_creative_data, merge_creative_data
  24. from examples.auto_put_ad_mini.tools.roi_calculator import calculate_roi_metrics
  25. from examples.auto_put_ad_mini.tools.ad_decision import (
  26. analyze_ads, get_ads_for_review, apply_decisions,
  27. query_ad_detail, modify_decisions,
  28. )
  29. from examples.auto_put_ad_mini.tools.report_generator import generate_report, compare_decisions
  30. from examples.auto_put_ad_mini.tools.guardrails import validate_decisions
  31. from examples.auto_put_ad_mini.tools.execution_engine import execute_decisions, check_execution_feedback
  32. from examples.auto_put_ad_mini.tools.im_approval import send_approval_request, check_approval_status
  33. async def main():
  34. base_dir = Path(__file__).parent
  35. setup_logging(level=LOG_LEVEL, file=LOG_FILE)
  36. # 加载 system prompt
  37. prompt_path = base_dir / "prompts" / "system.prompt"
  38. system_prompt = ""
  39. if prompt_path.exists():
  40. system_prompt = prompt_path.read_text(encoding="utf-8")
  41. # 加载 presets
  42. presets_path = base_dir / "presets.json"
  43. if presets_path.exists():
  44. from agent.core.presets import load_presets_from_json
  45. load_presets_from_json(str(presets_path))
  46. store = FileSystemTraceStore(base_path=TRACE_STORE_PATH)
  47. runner = AgentRunner(
  48. trace_store=store,
  49. llm_call=create_openrouter_llm_call(model=MAIN_CONFIG.model),
  50. skills_dir=SKILLS_DIR if Path(SKILLS_DIR).exists() else None,
  51. logger_name="agents.auto_put_ad_mini",
  52. )
  53. config = MAIN_CONFIG
  54. if system_prompt:
  55. config.system_prompt = system_prompt
  56. print("=" * 70)
  57. print(" 广告智能调控助手 — 基于20260415数据分析")
  58. print("=" * 70)
  59. print()
  60. print("🚀 执行:使用20260415及之前的数据进行分析")
  61. print()
  62. # 修改用户指令,明确指定使用0415数据
  63. messages = [{"role": "user", "content": "分析广告,使用20260415及之前的数据,不要拉取20260416的数据"}]
  64. config.trace_id = None
  65. try:
  66. async for item in runner.run(messages=messages, config=config):
  67. from agent.trace import Trace, Message
  68. if isinstance(item, Trace):
  69. if item.status == "completed":
  70. print(f"\n✅ [Trace] 完成")
  71. elif item.status == "failed":
  72. print(f"\n❌ [Trace] 失败")
  73. elif isinstance(item, Message):
  74. if item.role == "assistant" and item.content:
  75. content = item.content
  76. text = content.get("text", "") if isinstance(content, dict) else content
  77. if text and text.strip():
  78. print(f"\n💭 {text}\n")
  79. print("\n" + "=" * 70)
  80. print("✅ 执行完成")
  81. print("=" * 70)
  82. except Exception as e:
  83. print(f"\n❌ 执行失败: {e}")
  84. import traceback
  85. traceback.print_exc()
  86. if __name__ == "__main__":
  87. asyncio.run(main())