evaluate_agent_v2.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import json
  2. from openai import OpenAI
  3. def fetch_deepseek_completion(prompt, output_type='text'):
  4. """
  5. deep_seek方法
  6. """
  7. client = OpenAI(
  8. api_key='sk-cfd2df92c8864ab999d66a615ee812c5',
  9. base_url="https://api.deepseek.com"
  10. )
  11. # get response format
  12. if output_type == "json":
  13. response_format = {"type": "json_object"}
  14. else:
  15. response_format = {"type": "text"}
  16. chat_completion = client.chat.completions.create(
  17. messages=[
  18. {
  19. "role": "user",
  20. "content": prompt,
  21. }
  22. ],
  23. model="deepseek-chat",
  24. response_format=response_format,
  25. )
  26. response = chat_completion.choices[0].message.content
  27. if output_type == "json":
  28. response_json = json.loads(response)
  29. return response_json
  30. return response
  31. class PushAgentEvaluator:
  32. def __init__(self, dialogue_history, push_message, push_time, user_profile, agent_profile):
  33. self.dialogue_history = dialogue_history
  34. self.push_message = push_message
  35. self.user_profile = user_profile
  36. self.agent_profile = agent_profile
  37. self.push_time = push_time
  38. def evaluate_user_match(self):
  39. """用户匹配度评估(24分)"""
  40. out_put_format = {
  41. "语言风格": {
  42. "score": 8,
  43. "reason": "语言风格符合用户特征,年龄、性别、地域等特征都匹配"
  44. },
  45. "信息边界": {
  46. "score": 10,
  47. "reason": "信息范围符合用户认知范围"
  48. },
  49. "特征匹配": {
  50. "score": 10,
  51. "reason": "特征匹配度高,user_profile中有兴趣爱好,健康状态,用药状态,居住地址,家庭成员等特征,在 push_message需要体现出来"
  52. }
  53. }
  54. analysis_prompt = f"""
  55. 通过分析用户的以下特征
  56. 年龄: {self.user_profile['age']},
  57. 性别: {self.user_profile['gender']},
  58. 兴趣: {self.user_profile['interests']},
  59. 健康状态: {self.user_profile['health_conditions']},
  60. 用药状态: {self.user_profile['medications']},
  61. 居住地址: {self.user_profile['region']},
  62. 家庭成员: {self.user_profile['family_members']},
  63. 和 agent 发送的消息: {self.push_message}, 判断该消息是否和该用户的特征匹配
  64. 包括一下三点,每一点的满分为 8 分:
  65. 语言风格:是否符合用户的年龄、性别、地域等特征;
  66. 信息范围:是否超出用户认知范围;
  67. 特征匹配:如果 user_profile中有兴趣爱好,健康状态,用药状态,居住地址,家庭成员等特征,在 push_message需要体现出来
  68. 输出的格式为 JSON,示例如:{out_put_format}
  69. """
  70. completion = fetch_deepseek_completion(prompt=analysis_prompt, output_type='json')
  71. return completion
  72. def evaluate_agent_consistency(self):
  73. """人设一致性评估(16分)"""
  74. out_put_format = {
  75. "语言风格": {
  76. "score": 8,
  77. "reason": "语言风格符合用户特征,年龄、性别、地域等特征都匹配"
  78. },
  79. "消息边界": {
  80. "score": 8,
  81. "reason": "信息范围符合用户认知范围"
  82. },
  83. }
  84. analysis_prompt = f"""
  85. 通过分析 agent 的一下特征:
  86. 职业:{self.agent_profile['occupation']},
  87. 年龄:{self.agent_profile['age']},
  88. 性别:{self.agent_profile['gender']},
  89. 地址:{self.agent_profile['region']},
  90. 教育背景:{self.agent_profile['education']},
  91. 工作经验:{self.agent_profile['work_experience']}
  92. 和 agent 发送的消息: {self.push_message}, 判断该消息是否和该 agent 的特征匹配
  93. 包括一下 2 点,每一点的满分为 8 分:
  94. 语言风格:是否符合 agent 的年龄、性别、地域等特征;需要从不同性别,不同职业,不同年龄, 不同地域的人的说话风格去分析
  95. 信息范围:是否超出 agent 认知范围;
  96. 输出的格式为 JSON,示例如:{out_put_format}
  97. """
  98. completion = fetch_deepseek_completion(prompt=analysis_prompt, output_type='json')
  99. return completion
  100. def evaluate_interest_arousal(self):
  101. """兴趣激发评估(30)"""
  102. out_put_format = {
  103. "好奇设计": {
  104. "score": 15,
  105. "reason": "包含开放式问题,包括对用户认知边界的探索,用户兴趣爱好的联想等方面"
  106. },
  107. "利益设计": {
  108. "score": 15,
  109. "reason": "如果用户在历史对话中提到了某种需求,新的唤起消息是否有为用户解决需求的趋势"
  110. }
  111. }
  112. analysis_prompt = f"""
  113. 通过分析用户的以下特征
  114. 年龄: {self.user_profile['age']},
  115. 性别: {self.user_profile['gender']},
  116. 兴趣: {self.user_profile['interests']},
  117. 健康状态: {self.user_profile['health_conditions']},
  118. 用药状态: {self.user_profile['medications']},
  119. 居住地址: {self.user_profile['region']},
  120. 家庭成员: {self.user_profile['family_members']},
  121. 和 agent 发送的消息: {self.push_message}, 以及用户和 agent 的历史对话: {self.dialogue_history},
  122. 判断该消息是否能唤起用户的兴趣,驱动用户的聊天激情,主要从以下几2个方面考虑,每一个要点满分 15分
  123. 好奇设计:是否包含开放式问题,包括对用户认知边界的探索,用户兴趣爱好的联想等方面
  124. 利益设计:如果用户在历史对话中提到了某种需求,新的唤起消息是否有为用户解决需求的趋势;
  125. 如果没提出,可以从用户特征中联想出用户的需求,比如用户的兴趣爱好,健康状态,用药状态,居住地址,家庭成员等特征,
  126. 在 push_message需要体现出来
  127. 输出的格式为 JSON,示例如:{out_put_format}
  128. """
  129. completion = fetch_deepseek_completion(prompt=analysis_prompt, output_type='json')
  130. return completion
  131. def evaluate_execution_quality(self):
  132. """执行质量评估(15分)"""
  133. output_format = {
  134. "信息保真": {
  135. "score": 5,
  136. "reason": "信息真实"
  137. },
  138. "政策合规": {
  139. "score": 5,
  140. "reason": "发送消息是否符合现有政策要求,是否违反相关规则"
  141. },
  142. "语言拟人": {
  143. "score": 5,
  144. "reason": "表现出一定的拟人化和情商"
  145. }
  146. }
  147. analysis_prompt = f"""
  148. 通过分析 agent 发送的消息:{self.push_message},
  149. 信息保真(满分 5 分):
  150. 拆分消息重点各个事件点,联网搜索,判断事件是否为真实事件,比如说节日祝福需要和发送时间{self.push_time}对应, 天气需要和地区相关
  151. 不能在早上发晚上好之类等等
  152. 政策合规(满分 5 分):
  153. 并且判断发送的消息是否存在违反规则的现象。
  154. 语言拟人:(满分 5 分):
  155. 判断发送的消息是否存在机械化,同质化的现象,比如重复发相同或者相似的消息,比如过于机械的回复等等
  156. 输出的格式为 JSON,示例如:{output_format}
  157. """
  158. completion = fetch_deepseek_completion(prompt=analysis_prompt, output_type='json')
  159. return completion
  160. def evaluate_emotional_intelligence(self):
  161. """情感智能评估(15分)"""
  162. output_format = {
  163. "共情深度": {
  164. "score": 10,
  165. "reason": "识别用户情感状态,提供相应的回应"
  166. },
  167. }
  168. analysis_prompt = f"""
  169. 通过分析用户的以下特征
  170. 年龄: {self.user_profile['age']},
  171. 性别: {self.user_profile['gender']},
  172. 兴趣: {self.user_profile['interests']},
  173. 健康状态: {self.user_profile['health_conditions']},
  174. 用药状态: {self.user_profile['medications']},
  175. 居住地址: {self.user_profile['region']},
  176. 家庭成员: {self.user_profile['family_members']},
  177. 和 agent 和用户的对话:{self.dialogue_history} 挖掘出用户的情感需求
  178. 并且 agent 发送的消息: {self.push_message}, 判断该message是否能和用户共情,提升用户的情感体验
  179. 满分 15分
  180. 输出格式为 JSON,示例如:{output_format}
  181. """
  182. completion = fetch_deepseek_completion(prompt=analysis_prompt, output_type='json')
  183. return completion
  184. def evaluate_push_agent(
  185. dialogue_history,
  186. push_message,
  187. push_time,
  188. user_profile,
  189. agent_profile
  190. ):
  191. # 创建评估引擎实例
  192. evaluator = PushAgentEvaluator(
  193. dialogue_history,
  194. push_message,
  195. push_time,
  196. user_profile,
  197. agent_profile,
  198. )
  199. # 执行模块化评估
  200. evaluation_report = {
  201. "基础能力": {
  202. "用户匹配": evaluator.evaluate_user_match(),
  203. "人设一致": evaluator.evaluate_agent_consistency()
  204. },
  205. "任务效能": {
  206. "兴趣激发": evaluator.evaluate_interest_arousal(),
  207. "执行质量": evaluator.evaluate_execution_quality(),
  208. "情感智能": evaluator.evaluate_emotional_intelligence()
  209. }
  210. }
  211. return evaluation_report
  212. if __name__ == '__main__':
  213. with open("dev.json") as f:
  214. data = json.load(f)
  215. evaluation_report = evaluate_push_agent(
  216. data['dialogue_history'],
  217. data['push_message'],
  218. data['push_time'],
  219. data['user_profile'],
  220. data['agent_profile']
  221. )
  222. print(json.dumps(evaluation_report, indent=4, ensure_ascii=False))