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表达方式存储在全局文件中
@@ -143,26 +143,13 @@ class ExpressionSelector:
):
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

View File

@@ -293,7 +293,7 @@ class PersonImpressionpProcessor(BaseProcessor):
}
segments.append(new_segment)
person_name = get_person_info_manager().get_value(person_id, "person_name")
person_name = get_person_info_manager().get_value(person_id, "person_name") or person_id
logger.info(
f"{self.log_prefix} 眼熟用户 {person_name}{time.strftime('%H:%M:%S', time.localtime(potential_start_time))} - {time.strftime('%H:%M:%S', time.localtime(message_time))} 之间有 {new_segment['message_count']} 条消息"
)
@@ -341,6 +341,7 @@ class PersonImpressionpProcessor(BaseProcessor):
"message_count": self._count_messages_in_timerange(potential_start_time, message_time),
}
segments.append(new_segment)
person_name = get_person_info_manager().get_value(person_id, "person_name") or person_id
logger.info(f"{self.log_prefix} 重新眼熟用户 {person_name} 创建新消息段超过10条消息间隔: {new_segment}")
self._save_cache()

View File

@@ -500,20 +500,25 @@ class RelationshipManager:
new_familiarity_value = int(relation_value_json.get("familiarity_value", 0))
new_liking_value = int(relation_value_json.get("liking_value", 50))
if new_familiarity_value > 25:
# 获取当前的关系值
old_familiarity_value = await person_info_manager.get_value(person_id, "familiarity_value") or 0
old_familiarity_value += new_familiarity_value - 25 / 75
liking_value = await person_info_manager.get_value(person_id, "liking_value") or 50
# 更新熟悉度
if new_familiarity_value > 25:
familiarity_value = old_familiarity_value + (new_familiarity_value - 25) / 75
else:
familiarity_value = old_familiarity_value
# 更新好感度
if new_liking_value > 50:
liking_value = await person_info_manager.get_value(person_id, "liking_value") or 50
liking_value += new_liking_value - 50 / 50
if new_liking_value < 50:
liking_value = await person_info_manager.get_value(person_id, "liking_value") or 50
liking_value -= (50 - new_liking_value / 50) * 1.5
liking_value += (new_liking_value - 50) / 50
elif new_liking_value < 50:
liking_value -= (50 - new_liking_value) / 50 * 1.5
await person_info_manager.update_one_field(person_id, "familiarity_value", liking_value)
await person_info_manager.update_one_field(person_id, "familiarity_value", familiarity_value)
await person_info_manager.update_one_field(person_id, "liking_value", liking_value)
logger.info(f"更新了与 {person_name} 的关系值: 熟悉度={liking_value}, 好感度={liking_value}")
logger.info(f"更新了与 {person_name} 的关系值: 熟悉度={familiarity_value}, 好感度={liking_value}")
except (json.JSONDecodeError, ValueError, TypeError) as e:
logger.error(f"解析relation_value JSON失败或值无效: {e}, 响应: {relation_value_response}")