refactor: 将多个方法修改为静态方法以提高代码可读性和一致性
This commit is contained in:
@@ -24,7 +24,8 @@ class LLMStatistics:
|
||||
self._init_database()
|
||||
self.name_dict: Dict[List] = {}
|
||||
|
||||
def _init_database(self):
|
||||
@staticmethod
|
||||
def _init_database():
|
||||
"""初始化数据库集合"""
|
||||
if "online_time" not in db.list_collection_names():
|
||||
db.create_collection("online_time")
|
||||
@@ -51,7 +52,8 @@ class LLMStatistics:
|
||||
if self.console_thread:
|
||||
self.console_thread.join()
|
||||
|
||||
def _record_online_time(self):
|
||||
@staticmethod
|
||||
def _record_online_time():
|
||||
"""记录在线时间"""
|
||||
current_time = datetime.now()
|
||||
# 检查5分钟内是否已有记录
|
||||
@@ -187,7 +189,7 @@ class LLMStatistics:
|
||||
|
||||
# 按模型统计
|
||||
output.append("按模型统计:")
|
||||
output.append(("模型名称 调用次数 Token总量 累计花费"))
|
||||
output.append("模型名称 调用次数 Token总量 累计花费")
|
||||
for model_name, count in sorted(stats["requests_by_model"].items()):
|
||||
tokens = stats["tokens_by_model"][model_name]
|
||||
cost = stats["costs_by_model"][model_name]
|
||||
@@ -198,7 +200,7 @@ class LLMStatistics:
|
||||
|
||||
# 按请求类型统计
|
||||
output.append("按请求类型统计:")
|
||||
output.append(("模型名称 调用次数 Token总量 累计花费"))
|
||||
output.append("模型名称 调用次数 Token总量 累计花费")
|
||||
for req_type, count in sorted(stats["requests_by_type"].items()):
|
||||
tokens = stats["tokens_by_type"][req_type]
|
||||
cost = stats["costs_by_type"][req_type]
|
||||
@@ -209,7 +211,7 @@ class LLMStatistics:
|
||||
|
||||
# 修正用户统计列宽
|
||||
output.append("按用户统计:")
|
||||
output.append(("用户ID 调用次数 Token总量 累计花费"))
|
||||
output.append("用户ID 调用次数 Token总量 累计花费")
|
||||
for user_id, count in sorted(stats["requests_by_user"].items()):
|
||||
tokens = stats["tokens_by_user"][user_id]
|
||||
cost = stats["costs_by_user"][user_id]
|
||||
@@ -225,7 +227,7 @@ class LLMStatistics:
|
||||
|
||||
# 添加聊天统计
|
||||
output.append("群组统计:")
|
||||
output.append(("群组名称 消息数量"))
|
||||
output.append("群组名称 消息数量")
|
||||
for group_id, count in sorted(stats["messages_by_chat"].items()):
|
||||
output.append(f"{self.name_dict[group_id][0][:32]:<32} {count:>10}")
|
||||
|
||||
@@ -246,7 +248,7 @@ class LLMStatistics:
|
||||
|
||||
# 按模型统计
|
||||
output.append("按模型统计:")
|
||||
output.append(("模型名称 调用次数 Token总量 累计花费"))
|
||||
output.append("模型名称 调用次数 Token总量 累计花费")
|
||||
for model_name, count in sorted(stats["requests_by_model"].items()):
|
||||
tokens = stats["tokens_by_model"][model_name]
|
||||
cost = stats["costs_by_model"][model_name]
|
||||
@@ -284,7 +286,7 @@ class LLMStatistics:
|
||||
|
||||
# 添加聊天统计
|
||||
output.append("群组统计:")
|
||||
output.append(("群组名称 消息数量"))
|
||||
output.append("群组名称 消息数量")
|
||||
for group_id, count in sorted(stats["messages_by_chat"].items()):
|
||||
output.append(f"{self.name_dict[group_id][0][:32]:<32} {count:>10}")
|
||||
|
||||
|
||||
@@ -90,7 +90,8 @@ class Timer:
|
||||
self.auto_unit = auto_unit
|
||||
self.start = None
|
||||
|
||||
def _validate_types(self, name, storage):
|
||||
@staticmethod
|
||||
def _validate_types(name, storage):
|
||||
"""类型检查"""
|
||||
if name is not None and not isinstance(name, str):
|
||||
raise TimerTypeError("name", "Optional[str]", type(name))
|
||||
|
||||
@@ -77,7 +77,8 @@ class ChineseTypoGenerator:
|
||||
|
||||
return normalized_freq
|
||||
|
||||
def _create_pinyin_dict(self):
|
||||
@staticmethod
|
||||
def _create_pinyin_dict():
|
||||
"""
|
||||
创建拼音到汉字的映射字典
|
||||
"""
|
||||
@@ -95,7 +96,8 @@ class ChineseTypoGenerator:
|
||||
|
||||
return pinyin_dict
|
||||
|
||||
def _is_chinese_char(self, char):
|
||||
@staticmethod
|
||||
def _is_chinese_char(char):
|
||||
"""
|
||||
判断是否为汉字
|
||||
"""
|
||||
@@ -124,7 +126,8 @@ class ChineseTypoGenerator:
|
||||
|
||||
return result
|
||||
|
||||
def _get_similar_tone_pinyin(self, py):
|
||||
@staticmethod
|
||||
def _get_similar_tone_pinyin(py):
|
||||
"""
|
||||
获取相似声调的拼音
|
||||
"""
|
||||
@@ -211,13 +214,15 @@ class ChineseTypoGenerator:
|
||||
# 返回概率最高的几个字
|
||||
return [char for char, _ in candidates_with_prob[:num_candidates]]
|
||||
|
||||
def _get_word_pinyin(self, word):
|
||||
@staticmethod
|
||||
def _get_word_pinyin(word):
|
||||
"""
|
||||
获取词语的拼音列表
|
||||
"""
|
||||
return [py[0] for py in pinyin(word, style=Style.TONE3)]
|
||||
|
||||
def _segment_sentence(self, sentence):
|
||||
@staticmethod
|
||||
def _segment_sentence(sentence):
|
||||
"""
|
||||
使用jieba分词,返回词语列表
|
||||
"""
|
||||
@@ -392,7 +397,8 @@ class ChineseTypoGenerator:
|
||||
|
||||
return "".join(result), correction_suggestion
|
||||
|
||||
def format_typo_info(self, typo_info):
|
||||
@staticmethod
|
||||
def format_typo_info(typo_info):
|
||||
"""
|
||||
格式化错别字信息
|
||||
|
||||
|
||||
Reference in New Issue
Block a user