generate_strategy.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import asyncio
  2. import json
  3. import sys
  4. import os
  5. # 将 Agent 根目录加入 sys.path
  6. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
  7. from agent.llm.qwen import qwen_llm_call
  8. async def main():
  9. case_path = os.path.join(os.path.dirname(__file__), 'output', '04_parallel', 'case.json')
  10. out_path = os.path.join(os.path.dirname(__file__), 'output', '04_parallel', 'strategy.json')
  11. if not os.path.exists(case_path):
  12. print(f"Error: {case_path} does not exist.")
  13. return
  14. print("Loading case.json...")
  15. with open(case_path, 'r', encoding='utf-8') as f:
  16. case_data = json.load(f)
  17. # 提取精简的提示词
  18. prompt = f"""
  19. 你是一个工序调研协调器。你面前已经有了 54 个子 Agent 刚刚从各大平台收集回来的调研案例(见下方 JSON)。
  20. 这里的核心需求是:生成带有明显颗粒感或纸张纹理的插画风格图片。
  21. 请分析这些案例并总结、提取、设计制作最终的打法策略,按照下方强制的 JSON 格式输出,不要包含 Markdown 代码块(如 ```json 等),纯粹返回合法的 JSON 字符串。
  22. 强制输出格式:
  23. {{
  24. "selected_strategy": {{
  25. "name": "端到端策略命名(如:分层生成 + 颗粒噪点叠加 + FLUX局部精修)",
  26. "source": "指明主要受到这批采集回来的哪些 case 的启发",
  27. "workflow_outline": [
  28. {{
  29. "phase": "阶段1:底层结构与材质生成",
  30. "description": "这个阶段要完成的子目标...",
  31. "capabilities": [
  32. {{
  33. "capability_id": null,
  34. "capability_name": "原子能力名称",
  35. "is_new": true,
  36. "suggested_tools": ["提取自Case的建议工具"],
  37. "case_references": ["来源于哪个case的反馈"]
  38. }}
  39. ]
  40. }}
  41. ]
  42. }}
  43. }}
  44. 调研案例 JSON 内容如下:
  45. {json.dumps(case_data, ensure_ascii=False)}
  46. """
  47. print("Submitting to Qwen-Plus for strategy generation...")
  48. messages = [{"role": "user", "content": prompt}]
  49. try:
  50. response = await qwen_llm_call(messages, model="qwen-plus", temperature=0.3)
  51. content = response.get("content", "").strip()
  52. # 清理多余的 Markdown 标记
  53. if content.startswith("```json"):
  54. content = content[7:]
  55. elif content.startswith("```"):
  56. content = content[3:]
  57. if content.endswith("```"):
  58. content = content[:-3]
  59. content = content.strip()
  60. with open(out_path, 'w', encoding='utf-8') as f:
  61. f.write(content)
  62. print(f"\\n✅ Strategy successfully saved to: {out_path}")
  63. except Exception as e:
  64. print(f"\\n❌ Generation failed: {str(e)}")
  65. if __name__ == "__main__":
  66. asyncio.run(main())