fix:修复表达权重爆炸的问题,修复数据库Bug
This commit is contained in:
@@ -108,13 +108,13 @@ class ExpressionSelector:
|
|||||||
|
|
||||||
return selected_style, selected_grammar, selected_personality
|
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值
|
"""更新表达方式的count值
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
chat_id: 聊天ID
|
chat_id: 聊天ID
|
||||||
expression: 表达方式字典
|
expression: 表达方式字典
|
||||||
base_multiplier: 基础倍数,当count=1时使用此倍数
|
increment: 增量值,默认0.1
|
||||||
"""
|
"""
|
||||||
if expression.get("type") == "style_personality":
|
if expression.get("type") == "style_personality":
|
||||||
# personality表达方式存储在全局文件中
|
# personality表达方式存储在全局文件中
|
||||||
@@ -142,27 +142,14 @@ class ExpressionSelector:
|
|||||||
"style"
|
"style"
|
||||||
):
|
):
|
||||||
current_count = expr.get("count", 1)
|
current_count = expr.get("count", 1)
|
||||||
|
|
||||||
# 计算动态倍数:count值越高,增加倍数越小
|
# 简单加0.1,但限制最高为5
|
||||||
if current_count <= 1:
|
new_count = min(current_count + increment, 5.0)
|
||||||
# count <= 1时使用基础倍数
|
|
||||||
dynamic_multiplier = base_multiplier
|
|
||||||
else:
|
|
||||||
# count > 1时,倍数逐渐降低
|
|
||||||
# 使用公式:base_multiplier / (1 + (count - 1) * 0.3)
|
|
||||||
# 这样count=2时倍数约为1.15,count=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
|
|
||||||
expr["count"] = new_count
|
expr["count"] = new_count
|
||||||
expr["last_active_time"] = time.time()
|
expr["last_active_time"] = time.time()
|
||||||
|
|
||||||
logger.debug(
|
logger.info(
|
||||||
f"表达方式激活: 原count={current_count:.2f}, 倍数={dynamic_multiplier:.3f}, 新count={new_count:.2f}"
|
f"表达方式激活: 原count={current_count:.2f}, 增量={increment}, 新count={new_count:.2f}"
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -252,8 +239,8 @@ class ExpressionSelector:
|
|||||||
expression = all_expressions[idx - 1] # 索引从1开始
|
expression = all_expressions[idx - 1] # 索引从1开始
|
||||||
valid_expressions.append(expression)
|
valid_expressions.append(expression)
|
||||||
|
|
||||||
# 对选中的表达方式count数*1.5
|
# 对选中的表达方式count数+0.1
|
||||||
self.update_expression_count(chat_id, expression, 1.5)
|
self.update_expression_count(chat_id, expression, 0.1)
|
||||||
|
|
||||||
# logger.info(f"LLM从{len(all_expressions)}个情境中选择了{len(valid_expressions)}个")
|
# logger.info(f"LLM从{len(all_expressions)}个情境中选择了{len(valid_expressions)}个")
|
||||||
return valid_expressions
|
return valid_expressions
|
||||||
|
|||||||
@@ -293,7 +293,7 @@ class PersonImpressionpProcessor(BaseProcessor):
|
|||||||
}
|
}
|
||||||
segments.append(new_segment)
|
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(
|
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']} 条消息"
|
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),
|
"message_count": self._count_messages_in_timerange(potential_start_time, message_time),
|
||||||
}
|
}
|
||||||
segments.append(new_segment)
|
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}")
|
logger.info(f"{self.log_prefix} 重新眼熟用户 {person_name} 创建新消息段(超过10条消息间隔): {new_segment}")
|
||||||
|
|
||||||
self._save_cache()
|
self._save_cache()
|
||||||
|
|||||||
@@ -500,20 +500,25 @@ class RelationshipManager:
|
|||||||
new_familiarity_value = int(relation_value_json.get("familiarity_value", 0))
|
new_familiarity_value = int(relation_value_json.get("familiarity_value", 0))
|
||||||
new_liking_value = int(relation_value_json.get("liking_value", 50))
|
new_liking_value = int(relation_value_json.get("liking_value", 50))
|
||||||
|
|
||||||
|
# 获取当前的关系值
|
||||||
|
old_familiarity_value = await person_info_manager.get_value(person_id, "familiarity_value") or 0
|
||||||
|
liking_value = await person_info_manager.get_value(person_id, "liking_value") or 50
|
||||||
|
|
||||||
|
# 更新熟悉度
|
||||||
if new_familiarity_value > 25:
|
if new_familiarity_value > 25:
|
||||||
old_familiarity_value = await person_info_manager.get_value(person_id, "familiarity_value") or 0
|
familiarity_value = old_familiarity_value + (new_familiarity_value - 25) / 75
|
||||||
old_familiarity_value += new_familiarity_value - 25 / 75
|
else:
|
||||||
|
familiarity_value = old_familiarity_value
|
||||||
|
|
||||||
|
# 更新好感度
|
||||||
if new_liking_value > 50:
|
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
|
||||||
liking_value += new_liking_value - 50 / 50
|
elif new_liking_value < 50:
|
||||||
if new_liking_value < 50:
|
liking_value -= (50 - new_liking_value) / 50 * 1.5
|
||||||
liking_value = await person_info_manager.get_value(person_id, "liking_value") or 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)
|
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:
|
except (json.JSONDecodeError, ValueError, TypeError) as e:
|
||||||
logger.error(f"解析relation_value JSON失败或值无效: {e}, 响应: {relation_value_response}")
|
logger.error(f"解析relation_value JSON失败或值无效: {e}, 响应: {relation_value_response}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user