fix:修复表达权重爆炸的问题,修复数据库Bug

This commit is contained in:
SengokuCola
2025-06-25 20:39:20 +08:00
parent 2a44bb2db9
commit cdb0912b5a
3 changed files with 25 additions and 32 deletions

View File

@@ -108,13 +108,13 @@ class ExpressionSelector:
return selected_style, selected_grammar, selected_personality
def update_expression_count(self, chat_id: str, expression: Dict[str, str], base_multiplier: float = 1.5):
def update_expression_count(self, chat_id: str, expression: Dict[str, str], increment: float = 0.1):
"""更新表达方式的count值
Args:
chat_id: 聊天ID
expression: 表达方式字典
base_multiplier: 基础倍数当count=1时使用此倍数
increment: 增量值默认0.1
"""
if expression.get("type") == "style_personality":
# personality表达方式存储在全局文件中
@@ -142,27 +142,14 @@ class ExpressionSelector:
"style"
):
current_count = expr.get("count", 1)
# 计算动态倍数count值越高增加倍数越小
if current_count <= 1:
# count <= 1时使用基础倍数
dynamic_multiplier = base_multiplier
else:
# count > 1时倍数逐渐降低
# 使用公式base_multiplier / (1 + (count - 1) * 0.3)
# 这样count=2时倍数约为1.15count=5时倍数约为1.06
decay_factor = 0.3
dynamic_multiplier = base_multiplier / (1 + (current_count - 1) * decay_factor)
# 确保倍数不低于1.01(至少要有一点增长)
dynamic_multiplier = max(dynamic_multiplier, 1.01)
new_count = current_count * dynamic_multiplier
# 简单加0.1但限制最高为5
new_count = min(current_count + increment, 5.0)
expr["count"] = new_count
expr["last_active_time"] = time.time()
logger.debug(
f"表达方式激活: 原count={current_count:.2f}, 倍数={dynamic_multiplier:.3f}, 新count={new_count:.2f}"
logger.info(
f"表达方式激活: 原count={current_count:.2f}, 增量={increment}, 新count={new_count:.2f}"
)
break
@@ -252,8 +239,8 @@ class ExpressionSelector:
expression = all_expressions[idx - 1] # 索引从1开始
valid_expressions.append(expression)
# 对选中的表达方式count数*1.5
self.update_expression_count(chat_id, expression, 1.5)
# 对选中的表达方式count数+0.1
self.update_expression_count(chat_id, expression, 0.1)
# logger.info(f"LLM从{len(all_expressions)}个情境中选择了{len(valid_expressions)}个")
return valid_expressions