优化一下dify

This commit is contained in:
liuwei
2025-05-28 10:10:20 +08:00
parent d9d41a29ba
commit f7e3420212

View File

@@ -140,7 +140,9 @@ class DifyPlugin(MessagePluginInterface):
try:
# 调用Dify API获取回复
response = self._chat_with_dify((roomid if roomid else sender), sender, query)
success, response = self._chat_with_dify((roomid if roomid else sender), sender, query)
if not success:
return False, response
# 去除广告内容
response = remove_trailing_content(response)
@@ -184,7 +186,8 @@ class DifyPlugin(MessagePluginInterface):
return False, "命令格式错误"
# 检查权限
if roomid and gbm.get_group_permission((roomid if roomid else sender), Feature.AI_CAPABILITY) == PermissionStatus.DISABLED:
if roomid and gbm.get_group_permission((roomid if roomid else sender),
Feature.AI_CAPABILITY) == PermissionStatus.DISABLED:
return False, "没有权限"
client_msg_id, create_time, new_msg_id = await bot.send_text_message((roomid if roomid else sender),
@@ -203,7 +206,9 @@ class DifyPlugin(MessagePluginInterface):
try:
# 调用Dify API获取回复
response = self._chat_with_dify(session_id, user_id, query)
success, response = self._chat_with_dify(session_id, user_id, query)
if not success:
return False, response
# 去除广告内容
self.LOG.debug(f"_chat_with_dify response:{response}")
response = remove_trailing_content(response)
@@ -242,7 +247,7 @@ class DifyPlugin(MessagePluginInterface):
revoke.add_message_to_revoke((roomid if roomid else sender), client_msg_id, create_time, new_msg_id, 5)
return False, f"处理出错: {e}"
def _chat_with_dify(self, session_id: str, user_id: str, query: str) -> Optional[str]:
def _chat_with_dify(self, session_id: str, user_id: str, query: str) -> Tuple[bool, Optional[str]]:
"""
与Dify API交互获取回复
@@ -322,7 +327,7 @@ class DifyPlugin(MessagePluginInterface):
if response.status_code != 200:
self.LOG.error(f"Dify API请求失败: {response.status_code} {response.text}")
return f"请求失败,状态码: {response.status_code}"
return False, f"请求失败,状态码: {response.status_code}"
# 解析响应
response_data = response.json()
@@ -399,11 +404,11 @@ class DifyPlugin(MessagePluginInterface):
self.LOG.info(
f"用户 {user_id} 本次消耗 {total_tokens} tokens累计 {self.token_usage[user_id]} tokens")
return answer
return True, answer
except Exception as e:
self.LOG.error(f"处理Dify响应时出错: {str(e)}")
return f"处理响应时出错: {str(e)}"
return False, f"处理响应时出错"
def _cleanup_expired_conversations(self) -> None:
"""清理过期的会话"""