fix: 修复正则表达式中的括号匹配,优化统计输出格式

This commit is contained in:
春河晴
2025-04-16 16:26:15 +09:00
parent a8d48fc6cf
commit 328d759ace
4 changed files with 15 additions and 25 deletions

1
.gitignore vendored
View File

@@ -29,7 +29,6 @@ config/bot_config_dev.toml
config/bot_config.toml
config/bot_config.toml.bak
src/plugins/remote/client_uuid.json
run_none.bat
(测试版)麦麦生成人格.bat
(临时版)麦麦开始学习.bat
src/plugins/utils/statistic.py

View File

@@ -46,12 +46,13 @@ def is_mentioned_bot_in_message(message: MessageRecv) -> bool:
is_at = False
is_mentioned = False
if "is_mentioned" in message.message_info.additional_config.keys():
if message.message_info.additional_config.get("is_mentioned") is not None:
try:
reply_probability = float(message.message_info.additional_config.get("is_mentioned"))
is_mentioned = True
return is_mentioned, reply_probability
except Exception as e:
logger.warning(e)
logger.warning(
f"消息中包含不合理的设置 is_mentioned: {message.message_info.additional_config.get('is_mentioned')}"
)
@@ -71,7 +72,7 @@ def is_mentioned_bot_in_message(message: MessageRecv) -> bool:
is_mentioned = True
# 判断内容中是否被提及
message_content = re.sub(r"\@[\s\S]*?(\d+)", "", message.processed_plain_text)
message_content = re.sub(r"@[\s\S]*?(\d+)", "", message.processed_plain_text)
message_content = re.sub(r"回复[\s\S]*?\((\d+)\)的消息,说: ", "", message_content)
for keyword in keywords:
if keyword in message_content:
@@ -335,7 +336,7 @@ def random_remove_punctuation(text: str) -> str:
def process_llm_response(text: str) -> List[str]:
# 提取被 () 或 [] 包裹的内容
pattern = re.compile(r"[\(\[].*?[\)\]]")
pattern = re.compile(r"[(\[].*?[\)\]")
_extracted_contents = pattern.findall(text)
# 去除 () 和 [] 及其包裹的内容
cleaned_text = pattern.sub("", text)
@@ -496,16 +497,16 @@ def protect_kaomoji(sentence):
"""
kaomoji_pattern = re.compile(
r"("
r"[\(\[(【]" # 左括号
r"[(\[(【]" # 左括号
r"[^()\[\]()【】]*?" # 非括号字符(惰性匹配)
r"[^\u4e00-\u9fa5a-zA-Z0-9\s]" # 非中文、非英文、非数字、非空格字符(必须包含至少一个)
r"[^一-龥a-zA-Z0-9\s]" # 非中文、非英文、非数字、非空格字符(必须包含至少一个)
r"[^()\[\]()【】]*?" # 非括号字符(惰性匹配)
r"[\)\])】]" # 右括号
r"[\)\])】" # 右括号
r"]"
r")"
r"|"
r"("
r"[▼▽・ᴥω・﹏^><≧≦ ̄`´∀ヮДд︿﹀へ。゚╥╯╰︶︹•⁄]{2,15}"
r")"
r"([▼▽・ᴥω・﹏^><≧≦ ̄`´∀ヮДд︿﹀へ。゚╥╯╰︶︹•⁄]{2,15"
r"}"
)
kaomoji_matches = kaomoji_pattern.findall(sentence)

View File

@@ -119,7 +119,7 @@ class Prompt(str):
# 解析模板
template_args = []
result = re.findall(r"\{(.*?)\}", processed_fstr)
result = re.findall(r"\{(.*?)}", processed_fstr)
for expr in result:
if expr and expr not in template_args:
template_args.append(expr)
@@ -164,7 +164,7 @@ class Prompt(str):
processed_template = cls._process_escaped_braces(template)
template_args = []
result = re.findall(r"\{(.*?)\}", processed_template)
result = re.findall(r"\{(.*?)}", processed_template)
for expr in result:
if expr and expr not in template_args:
template_args.append(expr)

View File

@@ -175,13 +175,8 @@ class LLMStatistics:
def _format_stats_section(self, stats: Dict[str, Any], title: str) -> str:
"""格式化统计部分的输出"""
output = []
output = ["\n" + "-" * 84, f"{title}", "-" * 84, f"总请求数: {stats['total_requests']}"]
output.append("\n" + "-" * 84)
output.append(f"{title}")
output.append("-" * 84)
output.append(f"总请求数: {stats['total_requests']}")
if stats["total_requests"] > 0:
output.append(f"总Token数: {stats['total_tokens']}")
output.append(f"总花费: {stats['total_cost']:.4f}¥")
@@ -238,11 +233,7 @@ class LLMStatistics:
def _format_stats_section_lite(self, stats: Dict[str, Any], title: str) -> str:
"""格式化统计部分的输出"""
output = []
output.append("\n" + "-" * 84)
output.append(f"{title}")
output.append("-" * 84)
output = ["\n" + "-" * 84, f"{title}", "-" * 84]
# output.append(f"总请求数: {stats['total_requests']}")
if stats["total_requests"] > 0:
@@ -303,8 +294,7 @@ class LLMStatistics:
"""将统计结果保存到文件"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output = []
output.append(f"LLM请求统计报告 (生成时间: {current_time})")
output = [f"LLM请求统计报告 (生成时间: {current_time})"]
# 添加各个时间段的统计
sections = [