优化群总结内容。若是群名太长则进行压缩

This commit is contained in:
liuwei
2025-03-21 12:01:33 +08:00
parent 419e55a299
commit 12e01c4e2a

View File

@@ -1,4 +1,5 @@
import json
import re
from typing import Dict, Any, Tuple, Optional, List
import requests
@@ -103,8 +104,13 @@ class MessageSummaryPlugin(MessagePluginInterface):
chat_content = self.message_storage.get_messages(group_id, all_contacts)
if len(chat_content) < 100:
return False, None
# 获取群名并处理
group_name = all_contacts.get(group_id, group_id)
group_name = self._sanitize_group_name(group_name)
# 生成总结
summary, image_path = self._generate_summary(chat_content, all_contacts.get(group_id, group_id))
summary, image_path = self._generate_summary(chat_content, group_name)
# 发送总结结果
wcf = message.get("wcf")
@@ -121,7 +127,19 @@ class MessageSummaryPlugin(MessagePluginInterface):
self.LOG.error(f"处理消息总结命令失败: {e}")
return False, None
def _generate_summary(self, chat_content: str, group_id: str) -> Tuple[str, Optional[str]]:
def _sanitize_group_name(self, group_name: str) -> str:
"""处理群名,去除特殊字符并限制长度"""
# 去除特殊字符,只保留字母、数字、中文和基本标点
sanitized_name = re.sub(r'[^\w\s\u4e00-\u9fff,.,。]', '', group_name)
# 限制长度为15个字符
if len(sanitized_name) > 15:
sanitized_name = sanitized_name[:15]
# 如果处理后为空,则使用默认名称
if not sanitized_name:
sanitized_name = "群聊"
return sanitized_name
def _generate_summary(self, chat_content: str, group_name: str) -> Tuple[str, Optional[str]]:
"""生成总结"""
# Dify API配置
content_compress = chat_content
@@ -134,10 +152,10 @@ class MessageSummaryPlugin(MessagePluginInterface):
# 准备请求数据
data = {
"inputs": {},
"query": f"请根据以下{group_id}群聊记录生成一份精华总结:\n\n{content_compress}",
"query": f"请根据以下{group_name}群聊记录生成一份精华总结:\n\n{content_compress}",
"response_mode": "blocking", # 使用阻塞模式,直接获取完整响应
"conversation_id": "",
"user": group_id if group_id is not None else "message_summary_bot",
"user": group_name if group_name is not None else "message_summary_bot",
"files": [] # 不包含文件
}