run.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """
  2. 自动化投放 Agent 系统 — 主入口
  3. 运行方式:
  4. cd /Users/liulidong/project/agent/Agent
  5. python examples/auto_put_ad/run.py
  6. """
  7. import asyncio
  8. import os
  9. import sys
  10. from pathlib import Path
  11. # 如需代理,取消注释:
  12. # os.environ.setdefault("HTTP_PROXY", "http://127.0.0.1:29758")
  13. # os.environ.setdefault("HTTPS_PROXY", "http://127.0.0.1:29758")
  14. # 添加项目根目录到 Python 路径
  15. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  16. from dotenv import load_dotenv
  17. load_dotenv()
  18. from agent.core.runner import AgentRunner
  19. from agent.trace import FileSystemTraceStore, Trace, Message
  20. from agent.llm import create_openrouter_llm_call
  21. from agent.utils import setup_logging
  22. # 导入配置(使用绝对路径导入)
  23. from examples.auto_put_ad.config import RUN_CONFIG, SKILLS_DIR, TRACE_STORE_PATH, LOG_LEVEL, LOG_FILE
  24. # 导入自定义工具(触发 @tool 注册)
  25. from examples.auto_put_ad.tools.ad_api import (
  26. account_get_info, ad_batch_update_status, ad_create, ad_get_list,
  27. ad_get_report, ad_update, asset_get_list, audience_get_list,
  28. creative_create, creative_get_report, creative_update,
  29. )
  30. from examples.auto_put_ad.tools.audience_tools import (
  31. audience_build_targeting, audience_recommend_targeting,
  32. )
  33. from examples.auto_put_ad.tools.budget_calc import (
  34. account_evaluate, bid_adjustment_execute, budget_calculate_from_data,
  35. )
  36. from examples.auto_put_ad.tools.data_query import (
  37. data_aggregate, data_query, get_ad_current_status,
  38. )
  39. from examples.auto_put_ad.tools.monitor_tools import (
  40. monitor_check_metrics, monitor_circuit_break,
  41. )
  42. async def init_project_env(messages=None):
  43. """供 api_server 可视化调用:返回 (runner, messages, config)"""
  44. base_dir = Path(__file__).parent
  45. # 读取 system prompt
  46. task_prompt_path = base_dir / "task.prompt"
  47. system_prompt = ""
  48. if task_prompt_path.exists():
  49. system_prompt = task_prompt_path.read_text(encoding="utf-8")
  50. store = FileSystemTraceStore(base_path=TRACE_STORE_PATH)
  51. runner = AgentRunner(
  52. trace_store=store,
  53. llm_call=create_openrouter_llm_call(model=RUN_CONFIG.model),
  54. skills_dir=SKILLS_DIR if Path(SKILLS_DIR).exists() else None,
  55. logger_name="agents.auto_put_ad",
  56. )
  57. config = RUN_CONFIG
  58. if system_prompt:
  59. config.system_prompt = system_prompt
  60. # 如果前端没传 messages,给个默认的
  61. if not messages:
  62. messages = [{"role": "user", "content": "今天小程序预算10w"}]
  63. return runner, messages, config
  64. async def main():
  65. """主函数"""
  66. base_dir = Path(__file__).parent
  67. # 初始化日志
  68. setup_logging(level=LOG_LEVEL, file=LOG_FILE)
  69. # 读取 system prompt
  70. task_prompt_path = base_dir / "task.prompt"
  71. system_prompt = ""
  72. if task_prompt_path.exists():
  73. system_prompt = task_prompt_path.read_text(encoding="utf-8")
  74. # 创建 Runner
  75. store = FileSystemTraceStore(base_path=TRACE_STORE_PATH)
  76. runner = AgentRunner(
  77. trace_store=store,
  78. llm_call=create_openrouter_llm_call(model=RUN_CONFIG.model),
  79. skills_dir=SKILLS_DIR if Path(SKILLS_DIR).exists() else None,
  80. logger_name="agents.auto_put_ad",
  81. )
  82. # 如果有 system_prompt,注入到 config
  83. config = RUN_CONFIG
  84. if system_prompt:
  85. config.system_prompt = system_prompt
  86. print("=" * 60)
  87. print(" 自动化投放 Agent 系统已启动")
  88. print("=" * 60)
  89. print("请输入投放任务(输入 'exit' 退出):")
  90. print("示例:")
  91. print(" - 今天小程序预算10w")
  92. print(" - 分析账户昨日投放效果,优化预算分配")
  93. print(" - 查询账户余额和今日消耗")
  94. print()
  95. while True:
  96. try:
  97. user_input = input("\n> ").strip()
  98. if not user_input:
  99. continue
  100. if user_input.lower() in ("exit", "quit", "q"):
  101. print("退出系统")
  102. break
  103. # 构建消息
  104. messages = [{"role": "user", "content": user_input}]
  105. # 每次对话用新的 trace
  106. config.trace_id = None
  107. print(f"\n🚀 执行任务: {user_input}\n")
  108. # 执行 Agent
  109. async for item in runner.run(messages=messages, config=config):
  110. if isinstance(item, Trace):
  111. print(f"[Trace] 状态: {item.status}")
  112. elif isinstance(item, Message):
  113. if item.role == "assistant" and item.content:
  114. content = item.content
  115. if isinstance(content, dict):
  116. text = content.get("text", "")
  117. else:
  118. text = content
  119. if text and text.strip():
  120. print(f"\n💭 {text}\n")
  121. elif item.role == "tool" and item.content:
  122. content = item.content
  123. if isinstance(content, str):
  124. text = content
  125. elif isinstance(content, dict):
  126. text = content.get("text", str(content))
  127. else:
  128. text = str(content)
  129. # 只打印前 500 字符避免刷屏
  130. if len(text) > 500:
  131. text = text[:500] + "..."
  132. print(f" [Tool] {text}")
  133. print("\n" + "=" * 60)
  134. print("✅ 任务完成")
  135. print("=" * 60)
  136. except KeyboardInterrupt:
  137. print("\n用户中断,退出系统")
  138. break
  139. except Exception as e:
  140. print(f"\n❌ 执行失败: {e}")
  141. import traceback
  142. traceback.print_exc()
  143. if __name__ == "__main__":
  144. asyncio.run(main())