10_CustomerService.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. 10.3.4 在智能体中使用A2A工具
  3. (2)实战案例:智能客服系统
  4. """
  5. from hello_agents import SimpleAgent, HelloAgentsLLM
  6. from hello_agents.tools import A2ATool
  7. from hello_agents.protocols import A2AServer
  8. import threading
  9. import time
  10. from dotenv import load_dotenv
  11. load_dotenv()
  12. llm = HelloAgentsLLM()
  13. # 1. 创建技术专家Agent服务
  14. tech_expert = A2AServer(
  15. name="tech_expert",
  16. description="技术专家,回答技术问题"
  17. )
  18. @tech_expert.skill("answer")
  19. def answer_tech_question(text: str) -> str:
  20. import re
  21. match = re.search(r'answer\s+(.+)', text, re.IGNORECASE)
  22. question = match.group(1).strip() if match else text
  23. # 实际应用中,这里会调用LLM或知识库
  24. return f"技术回答:关于'{question}',我建议您查看我们的技术文档..."
  25. # 2. 创建销售顾问Agent服务
  26. sales_advisor = A2AServer(
  27. name="sales_advisor",
  28. description="销售顾问,回答销售问题"
  29. )
  30. @sales_advisor.skill("answer")
  31. def answer_sales_question(text: str) -> str:
  32. import re
  33. match = re.search(r'answer\s+(.+)', text, re.IGNORECASE)
  34. question = match.group(1).strip() if match else text
  35. return f"销售回答:关于'{question}',我们有特别优惠..."
  36. # 3. 启动服务
  37. threading.Thread(target=lambda: tech_expert.run(port=6000), daemon=True).start()
  38. threading.Thread(target=lambda: sales_advisor.run(port=6001), daemon=True).start()
  39. time.sleep(2)
  40. # 4. 创建接待员Agent(使用HelloAgents的SimpleAgent)
  41. receptionist = SimpleAgent(
  42. name="接待员",
  43. llm=llm,
  44. system_prompt="""你是客服接待员,负责:
  45. 1. 分析客户问题类型(技术问题 or 销售问题)
  46. 2. 将问题转发给相应的专家
  47. 3. 整理专家的回答并返回给客户
  48. 请保持礼貌和专业。"""
  49. )
  50. # 添加技术专家工具
  51. tech_tool = A2ATool(
  52. agent_url="http://localhost:6000",
  53. name="tech_expert",
  54. description="技术专家,回答技术相关问题"
  55. )
  56. receptionist.add_tool(tech_tool)
  57. # 添加销售顾问工具
  58. sales_tool = A2ATool(
  59. agent_url="http://localhost:6001",
  60. name="sales_advisor",
  61. description="销售顾问,回答价格、购买相关问题"
  62. )
  63. receptionist.add_tool(sales_tool)
  64. # 5. 处理客户咨询
  65. def handle_customer_query(query):
  66. print(f"\n客户咨询:{query}")
  67. print("=" * 50)
  68. response = receptionist.run(query)
  69. print(f"\n客服回复:{response}")
  70. print("=" * 50)
  71. # 测试不同类型的问题
  72. if __name__ == "__main__":
  73. handle_customer_query("你们的API如何调用?")
  74. handle_customer_query("企业版的价格是多少?")
  75. handle_customer_query("如何集成到我的Python项目中?")