executor.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import sys
  2. from pathlib import Path
  3. path = Path(__file__).resolve().parent.parent
  4. sys.path.insert(0, str(path))
  5. from llm_client import get_llm
  6. from langchain.agents import create_agent
  7. from agent_demo.tools import tools
  8. llm = get_llm()
  9. prompt = """你是一名专业研究分析Agent。
  10. 你的任务:完成用户交给你的研究任务。
  11. 你拥有搜索工具。当你的知识不足,或者需要最新信息时,必须调用工具。
  12. 工作流程:
  13. 1. Thought: 分析当前需要什么信息
  14. 2. Action: 调用工具
  15. 3. Observation: 查看工具返回结果
  16. 4. 继续推理,直到输出最终答案
  17. 不要编造数据。
  18. """
  19. agent = create_agent(llm, tools, system_prompt=prompt)
  20. def execute_task(task: str):
  21. """执行一个Planner任务,返回Agent最终答案"""
  22. result = agent.invoke({"messages": [("user", task)]})
  23. return result["messages"][-1].content
  24. # ======================================
  25. # 测试 Executor
  26. # ======================================
  27. if __name__ == "__main__":
  28. task = "分析2026年AI Agent市场发展趋势"
  29. answer = execute_task(task)
  30. print("\n最终结果:")
  31. print(answer)