|
@@ -409,55 +409,6 @@ class AgentService:
|
|
# 问题在于,如果每次创建出新的PushTaskWorkerPool,在上次任务有未处理完的消息即退出时,会有未处理的消息堆积
|
|
# 问题在于,如果每次创建出新的PushTaskWorkerPool,在上次任务有未处理完的消息即退出时,会有未处理的消息堆积
|
|
push_task_worker_pool.wait_to_finish()
|
|
push_task_worker_pool.wait_to_finish()
|
|
|
|
|
|
- def _check_initiative_conversations_v1(self):
|
|
|
|
- logger.info("start to check initiative conversations")
|
|
|
|
- if not DialogueManager.is_time_suitable_for_active_conversation():
|
|
|
|
- logger.info("time is not suitable for active conversation")
|
|
|
|
- return
|
|
|
|
- white_list_tags = set(apollo_config.get_json_value('agent_initiate_whitelist_tags', []))
|
|
|
|
- first_initiate_tags = set(apollo_config.get_json_value('agent_first_initiate_whitelist_tags', []))
|
|
|
|
- # 合并白名单,减少配置成本
|
|
|
|
- white_list_tags.update(first_initiate_tags)
|
|
|
|
- voice_tags = set(apollo_config.get_json_value('agent_initiate_by_voice_tags', []))
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- """定时检查主动发起对话"""
|
|
|
|
- for staff_user in self.user_relation_manager.list_staff_users():
|
|
|
|
- staff_id = staff_user['staff_id']
|
|
|
|
- user_id = staff_user['user_id']
|
|
|
|
- agent = self.get_agent_instance(staff_id, user_id)
|
|
|
|
- should_initiate = agent.should_initiate_conversation()
|
|
|
|
- user_tags = self.user_relation_manager.get_user_tags(user_id)
|
|
|
|
-
|
|
|
|
- if configs.get_env() != 'dev' and not white_list_tags.intersection(user_tags):
|
|
|
|
- should_initiate = False
|
|
|
|
-
|
|
|
|
- if should_initiate:
|
|
|
|
- logger.warning(f"user[{user_id}], tags{user_tags}: initiate conversation")
|
|
|
|
- # FIXME:虽然需要主动唤起的用户同时发来消息的概率很低,但仍可能会有并发冲突 需要并入事件驱动框架
|
|
|
|
- agent.do_state_change(DialogueState.GREETING)
|
|
|
|
- try:
|
|
|
|
- if agent.previous_state == DialogueState.INITIALIZED or first_initiate_tags.intersection(user_tags):
|
|
|
|
- # 完全无交互历史的用户才使用此策略,但新用户接入即会产生“我已添加了你”的消息将Agent初始化
|
|
|
|
- # 因此存量用户无法使用该状态做实验
|
|
|
|
- # TODO:增加基于对话历史的判断、策略去重;如果对话间隔过长需要使用长期记忆检索;在无长期记忆时,可采用用户添加时间来判断
|
|
|
|
- resp = self._generate_active_greeting_message(agent, user_tags)
|
|
|
|
- else:
|
|
|
|
- resp = self._get_chat_response(user_id, agent, None)
|
|
|
|
- if resp:
|
|
|
|
- if set(user_tags).intersection(voice_tags):
|
|
|
|
- message_type = MessageType.VOICE
|
|
|
|
- else:
|
|
|
|
- message_type = MessageType.TEXT
|
|
|
|
- self.send_response(staff_id, user_id, resp, message_type, skip_check=True)
|
|
|
|
- agent.persist_state()
|
|
|
|
- except Exception as e:
|
|
|
|
- # FIXME:虽然需要主动唤起的用户同时发来消息的概率很低,但仍可能会有并发冲突
|
|
|
|
- agent.rollback_state()
|
|
|
|
- logger.error("Error in active greeting: {}".format(e))
|
|
|
|
- else:
|
|
|
|
- logger.debug(f"user[{user_id}], do not initiate conversation")
|
|
|
|
-
|
|
|
|
def _generate_active_greeting_message(self, agent: DialogueManager, user_tags: List[str]=None):
|
|
def _generate_active_greeting_message(self, agent: DialogueManager, user_tags: List[str]=None):
|
|
chat_config = agent.build_active_greeting_config(user_tags)
|
|
chat_config = agent.build_active_greeting_config(user_tags)
|
|
chat_response = self._call_chat_api(chat_config, ChatServiceType.OPENAI_COMPATIBLE)
|
|
chat_response = self._call_chat_api(chat_config, ChatServiceType.OPENAI_COMPATIBLE)
|