Quellcode durchsuchen

Update agent_service: seperate response generating and sending

StrayWarrior vor 6 Tagen
Ursprung
Commit
a68b7dc0e3
1 geänderte Dateien mit 20 neuen und 15 gelöschten Zeilen
  1. 20 15
      agent_service.py

+ 20 - 15
agent_service.py

@@ -143,7 +143,9 @@ class AgentService:
             elif need_response:
                 # 先更新用户画像再处理回复
                 self._update_user_profile(user_id, user_profile, agent.dialogue_history[-10:])
-                self._get_chat_response(user_id, agent, message_text)
+                resp = self._get_chat_response(user_id, agent, message_text)
+                if resp:
+                    self._send_response(staff_id, user_id, resp)
             else:
                 logger.debug(f"staff[{staff_id}], user[{user_id}]: do not need response")
             # 当前消息处理成功,持久化agent状态
@@ -152,6 +154,22 @@ class AgentService:
             agent.rollback_state()
             raise e
 
+    def _send_response(self, staff_id, user_id, response):
+        logger.warning(f"staff[{staff_id}] user[{user_id}]: response: {response}")
+        current_ts = int(time.time() * 1000)
+        user_tags = self.user_relation_manager.get_user_tags(user_id)
+        # FIXME(zhoutian)
+        # 测试期间临时逻辑,只发送特定的账号或特定用户
+        user_black_lists = ['7881300148979455',]
+        if not (staff_id in set(['1688854492669990'])
+                or 'AgentTest1' in user_tags) or user_id in user_black_lists:
+            logger.warning(f"staff[{staff_id}] user[{user_id}]: skip reply")
+            return None
+        self.send_queue.produce(
+            Message.build(MessageType.TEXT, MessageChannel.CORP_WECHAT,
+                          staff_id, user_id, response, current_ts)
+        )
+
     def _route_to_human_intervention(self, user_id: str, origin_message: Message):
         """路由到人工干预"""
         self.human_queue.produce(Message.build(
@@ -175,6 +193,7 @@ class AgentService:
                 logger.warning("user: {}, initiate conversation".format(user_id))
                 resp = self._get_chat_response(user_id, agent, None)
                 if resp:
+                    self._send_response(staff_id, user_id, resp)
                     time.sleep(random.randint(10,20))
             else:
                 logger.debug("user: {}, do not initiate conversation".format(user_id))
@@ -188,20 +207,6 @@ class AgentService:
         chat_response = self.sanitize_response(chat_response)
 
         if response := agent.generate_response(chat_response):
-            logger.warning(f"staff[{agent.staff_id}] user[{user_id}]: response: {response}")
-            current_ts = int(time.time() * 1000)
-            user_tags = self.user_relation_manager.get_user_tags(user_id)
-            # FIXME(zhoutian)
-            # 测试期间临时逻辑,只发送特定的账号或特定用户
-            user_black_lists = ['7881300148979455',]
-            if not (agent.staff_id in set(['1688854492669990'])
-                    or 'AgentTest1' in user_tags) or agent.user_id in user_black_lists:
-                logger.warning(f"staff[{agent.staff_id}] user[{user_id}]: skip reply")
-                return None
-            self.send_queue.produce(
-                Message.build(MessageType.TEXT, MessageChannel.CORP_WECHAT,
-                              agent.staff_id, user_id, response, current_ts)
-            )
             return response
         else:
             logger.warning(f"staff[{agent.staff_id}] user[{user_id}]: no response generated")