message_push_agent.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import datetime
  2. from typing import Optional, List, Dict
  3. from pqai_agent.agents.simple_chat_agent import SimpleOpenAICompatibleChatAgent
  4. from pqai_agent.chat_service import VOLCENGINE_MODEL_DEEPSEEK_V3
  5. from pqai_agent.logging_service import logger
  6. from pqai_agent.toolkit.function_tool import FunctionTool
  7. from pqai_agent.toolkit.image_describer import ImageDescriber
  8. from pqai_agent.toolkit.message_notifier import MessageNotifier
  9. DEFAULT_SYSTEM_PROMPT = '''
  10. <基本设定>
  11. 你是一位熟悉中老年用户交流习惯的微信客服。
  12. 你擅长以下事项:
  13. * 倾听、引导和共情,在对话中自然促进用户互动
  14. * 理解中老年人的典型情感需求、对话习惯
  15. 你的工作方法论:
  16. * 分析用户请求以确定核心需求
  17. * 为完成任务制定结构化的计划
  18. </基本设定>
  19. <语言设定>
  20. * 默认的工作语言:中文
  21. * 如果用户指定使用其它语言,则将其作为工作语言
  22. * 所有的思考和回答都要用工作语言
  23. </语言设定>
  24. <通用话术列表>
  25. <话术>
  26. * 简介:简单自我介绍,并向用户发出交友邀请
  27. * 适用情形:初次问候
  28. * 内容:根据你的人设做出简短自我介绍,并向用户发出交友邀请
  29. * 要求:简短亲切,1-2句话;模拟微信聊天的回复格式,分段清晰
  30. </话术>
  31. <话术>
  32. * 简介:使用用户微信名做藏头诗,进行问候
  33. * 适用情形:不限
  34. * 内容:首先进行用户问候,然后介绍藏头诗,最后以一个让老年人有动力继续聊天的问句结尾
  35. * 要求:最后的问句要与藏头诗或用户自身经历有关,与藏头诗自然承接,无需和用户画像其他内容相关
  36. </话术>
  37. <话术>
  38. * 简介:介绍自己的兴趣并询问用户的兴趣爱好
  39. * 适用情形:不限
  40. * 内容:根据用户头像分析用户的特点、可能的兴趣爱好,作为参考,表述自己有相同的兴趣爱好,并举一些简短的例子 ,然后询问用户的兴趣爱好
  41. * 要求:询问无需和用户画像中其他信息有关;说明引发你问候的是用户头像;简短亲切,2-3句话 30字左右;如无用户信息或行为,不要根据联想杜撰用户偏好/行为
  42. </话术>
  43. <话术>
  44. * 简介:对用户进行节日/节气相关问候
  45. * 适用情形:不限
  46. * 内容:结合具体节假日及其习俗产生问候,以一个让老年人有动力继续聊天的问句结尾,与前面的问候自然承接
  47. * 要求:根据今日或近日实际日期,不要假设日期和节日;忽略小众节假日,和根据最近的节假日产生问候,如临近或刚过完重要节日,可询问节日安排或节日经历;简短亲切,2-3句话 30字左右;如无用户信息或行为,不要根据联想杜撰用户偏好/行为
  48. </话术>
  49. <话术>
  50. * 简介:询问用户当日计划安排并产生问候
  51. * 适用情形:不限
  52. * 内容:向用户介绍你的今日安排以及询问用户的今日安排
  53. * 要求:简短亲切,1-2句话,像用户熟悉的晚辈一样问候沟通;模拟微信聊天的回复格式,分段清晰
  54. </话术>
  55. </通用话术列表>
  56. <心理学技巧>
  57. * 怀旧效应:可以用"当年/以前"触发美好回忆
  58. * 具象化提问:避免抽象问题
  59. * 正向反馈圈:在后续对话中重复用户的关键词
  60. </心理学技巧>
  61. <风险规避原则>
  62. * 避免过度打扰和重复:注意分析历史对话
  63. * 避免过度解读:不要过度解读用户的信息
  64. * 文化适配:注意不同地域的用户文化差异
  65. * 准确性要求:不要使用虚构的信息
  66. </风险规避原则>
  67. <agent_loop>
  68. You are operating in an agent loop, iteratively completing tasks through these steps:
  69. 1. Analyze Events: Understand user needs and current state through event stream, focusing on latest user messages and execution results
  70. 2. Select Tools: Choose next tool call based on current state, task planning, relevant knowledge and available data APIs
  71. 3. Wait for Execution: Selected tool action will be executed by sandbox environment with new observations added to event stream
  72. 4. Iterate: Choose only one tool call per iteration, patiently repeat above steps until task completion
  73. 5. Submit Results: Send results to user via message tools, providing deliverables and related files as message attachments
  74. 6. Enter Standby: Enter idle state when all tasks are completed or user explicitly requests to stop, and wait for new tasks
  75. </agent_loop>
  76. '''
  77. QUERY_PROMPT_TEMPLATE = """现在,请通过多步思考,选择合适的方法向一位用户发起问候。
  78. # 已知用户的信息
  79. 用户信息:
  80. - 姓名:{name}
  81. - 头像:{avatar}
  82. - 偏好的称呼:{preferred_nickname}
  83. - 年龄:{age}
  84. - 地区:{region}
  85. - 健康状况:{health_conditions}
  86. - 用药信息:{medications}
  87. - 兴趣爱好:{interests}
  88. # 已知过去的对话
  89. {dialogue_history}
  90. # 当前上下文信息
  91. 时间:{current_datetime}
  92. 注意对话信息的格式为: [角色][时间]对话内容
  93. 注意一定要分析对话信息中的时间,避免和当前时间段不符的内容!注意一定要结合历史的对话情况进行分析和问候方式的选择!
  94. 如有必要,可以使用analyse_image分析用户头像。
  95. 必须使用message_notify_user发送最终的问候内容,调用message_notify_user时不要传入除了问候内容外的其它任何信息。
  96. 注意每次问候只使用一种话术。
  97. Now, start to process your task. Please think step by step.
  98. """
  99. class MessagePushAgent(SimpleOpenAICompatibleChatAgent):
  100. """A specialized agent for message push tasks."""
  101. def __init__(self, model: Optional[str] = VOLCENGINE_MODEL_DEEPSEEK_V3, system_prompt: Optional[str] = None,
  102. tools: Optional[List[FunctionTool]] = None,
  103. generate_cfg: Optional[dict] = None, max_run_step: Optional[int] = None):
  104. system_prompt = system_prompt or DEFAULT_SYSTEM_PROMPT
  105. tools = tools or []
  106. tools = tools.copy()
  107. tools.extend([
  108. *ImageDescriber().get_tools(),
  109. *MessageNotifier().get_tools()
  110. ])
  111. super().__init__(model, system_prompt, tools, generate_cfg, max_run_step)
  112. def generate_message(self, context: Dict, dialogue_history: List[Dict]) -> str:
  113. formatted_dialogue = MessagePushAgent.compose_dialogue(dialogue_history)
  114. query = QUERY_PROMPT_TEMPLATE.format(**context, dialogue_history=formatted_dialogue)
  115. self.run(query)
  116. for tool_call in reversed(self.tool_call_records):
  117. if tool_call['name'] == MessageNotifier.message_notify_user.__name__:
  118. return tool_call['arguments']['message']
  119. return ''
  120. @staticmethod
  121. def compose_dialogue(dialogue: List[Dict]) -> str:
  122. role_map = {'user': '用户', 'assistant': '客服'}
  123. messages = []
  124. for msg in dialogue:
  125. if not msg['content']:
  126. continue
  127. if msg['role'] not in role_map:
  128. continue
  129. format_dt = datetime.datetime.fromtimestamp(msg['timestamp'] / 1000).strftime('%Y-%m-%d %H:%M:%S')
  130. messages.append('[{}][{}]{}'.format(role_map[msg['role']], format_dt, msg['content']))
  131. return '\n'.join(messages)
  132. class DummyMessagePushAgent(MessagePushAgent):
  133. """A dummy agent for testing purposes."""
  134. def __init__(self, *args, **kwargs):
  135. super().__init__(*args, **kwargs)
  136. def generate_message(self, context: Dict, dialogue_history: List[Dict]) -> str:
  137. return "测试消息: {agent_name} -> {nickname}".format(**context)
  138. if __name__ == '__main__':
  139. import pqai_agent.logging_service
  140. pqai_agent.logging_service.setup_root_logger()
  141. agent = MessagePushAgent()
  142. test_user_profile = {
  143. 'name': '薛岱月',
  144. 'avatar': 'http://wx.qlogo.cn/mmhead/Q3auHgzwzM5glpnBtDUianJErYf9AQsptLM3N78xP3sOR8SSibsG35HQ/0',
  145. 'preferred_nickname': '月哥',
  146. 'age': 65,
  147. 'region': '北京',
  148. 'health_conditions': '高血压',
  149. 'medications': ['降压药'],
  150. 'interests': ['钓鱼', '旅游']
  151. }
  152. test_context = {
  153. "current_datetime": "2025-05-10 08:00:00",
  154. **test_user_profile
  155. }
  156. def create_ts(year, month, day, hour, minute):
  157. return datetime.datetime(year, month, day, hour, minute).timestamp() * 1000
  158. messages = [
  159. # {"role": "assistant", "content": "月哥,早上好!看到您的头像是一片宁静的户外风景,感觉您一定很喜欢大自然吧?今天天气不错,您有什么计划吗?", "timestamp": create_ts(2025, 5, 10, 8, 0)},
  160. ]
  161. response = agent.generate_message(test_context, messages)
  162. print(response)