feat(affinity-flow): 重构兴趣度评分系统为智能embedding匹配
- 移除传统关键词匹配方式,改用embedding计算智能兴趣匹配度 - 添加异步方法支持机器人兴趣管理器的智能匹配计算 - 增加详细的日志记录和错误处理机制 - 添加数据库关键词提取和降级处理逻辑 - 集成智能兴趣系统初始化到人设构建流程 - 防止回复自身消息的死循环保护机制 BREAKING CHANGE: 兴趣匹配评分机制完全重构,从基于关键词的硬编码匹配改为基于embedding的智能匹配,需要重新初始化兴趣系统
This commit is contained in:
@@ -6,6 +6,7 @@ import asyncio
|
||||
import time
|
||||
from typing import Dict, List
|
||||
|
||||
from src.config.config import global_config
|
||||
from src.chat.planner_actions.action_manager import ActionManager
|
||||
from src.common.data_models.info_data_model import Plan, ActionPlannerInfo
|
||||
from src.common.logger import get_logger
|
||||
@@ -122,6 +123,16 @@ class PlanExecutor:
|
||||
try:
|
||||
logger.info(f"执行回复动作: {action_info.action_type}, 原因: {action_info.reasoning}")
|
||||
|
||||
if action_info.action_message.get("user_id","") == str(global_config.bot.qq_account):
|
||||
logger.warning("尝试回复自己,跳过此动作以防止死循环。")
|
||||
return {
|
||||
"action_type": action_info.action_type,
|
||||
"success": False,
|
||||
"error_message": "尝试回复自己,跳过此动作以防止死循环。",
|
||||
"execution_time": 0,
|
||||
"reasoning": action_info.reasoning,
|
||||
"reply_content": "",
|
||||
}
|
||||
# 构建回复动作参数
|
||||
action_params = {
|
||||
"chat_id": plan.chat_id,
|
||||
|
||||
@@ -97,7 +97,7 @@ class ActionPlanner:
|
||||
# 2. 兴趣度评分 - 只对未读消息进行评分
|
||||
if unread_messages:
|
||||
bot_nickname = global_config.bot.nickname
|
||||
interest_scores = self.interest_scoring.calculate_interest_scores(
|
||||
interest_scores = await self.interest_scoring.calculate_interest_scores(
|
||||
unread_messages, bot_nickname
|
||||
)
|
||||
|
||||
@@ -175,33 +175,14 @@ class ActionPlanner:
|
||||
|
||||
return final_actions_dict, final_target_message_dict
|
||||
|
||||
def _build_return_result(self, plan: Plan) -> Tuple[List[Dict], Optional[Dict]]:
|
||||
"""构建返回结果"""
|
||||
final_actions = plan.decided_actions or []
|
||||
final_target_message = next(
|
||||
(act.action_message for act in final_actions if act.action_message), None
|
||||
)
|
||||
|
||||
final_actions_dict = [asdict(act) for act in final_actions]
|
||||
|
||||
if final_target_message:
|
||||
if hasattr(final_target_message, '__dataclass_fields__'):
|
||||
final_target_message_dict = asdict(final_target_message)
|
||||
else:
|
||||
final_target_message_dict = final_target_message
|
||||
else:
|
||||
final_target_message_dict = None
|
||||
|
||||
return final_actions_dict, final_target_message_dict
|
||||
|
||||
def get_user_relationship(self, user_id: str) -> float:
|
||||
"""获取用户关系分"""
|
||||
return self.interest_scoring.get_user_relationship(user_id)
|
||||
|
||||
def update_interest_keywords(self, new_keywords: Dict[str, List[str]]):
|
||||
"""更新兴趣关键词"""
|
||||
self.interest_scoring.interest_keywords.update(new_keywords)
|
||||
logger.info(f"已更新兴趣关键词: {list(new_keywords.keys())}")
|
||||
"""更新兴趣关键词(已弃用,仅保留用于兼容性)"""
|
||||
logger.info("传统关键词匹配已移除,此方法仅保留用于兼容性")
|
||||
# 此方法已弃用,因为现在完全使用embedding匹配
|
||||
|
||||
def get_planner_stats(self) -> Dict[str, any]:
|
||||
"""获取规划器统计"""
|
||||
@@ -226,5 +207,4 @@ class ActionPlanner:
|
||||
}
|
||||
|
||||
|
||||
# 全局兴趣度评分系统实例
|
||||
interest_scoring_system = InterestScoringSystem()
|
||||
# 全局兴趣度评分系统实例 - 在 individuality 模块中创建
|
||||
Reference in New Issue
Block a user