迁移:69a855d(feat:保存关键词到message数据库)
This commit is contained in:
@@ -159,19 +159,12 @@ class CycleProcessor:
|
||||
self.context.last_action = action_type
|
||||
|
||||
# 处理no_reply相关的逻辑
|
||||
if action_type != "no_reply" and action_type != "no_action":
|
||||
# 导入NoReplyAction并重置计数器
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
NoReplyAction.reset_consecutive_count()
|
||||
if action_type != "no_reply":
|
||||
self.context.no_reply_consecutive = 0
|
||||
logger.info(f"{self.context.log_prefix} 执行了{action_type}动作,重置no_reply计数器")
|
||||
elif action_type == "no_action":
|
||||
# 当执行回复动作时,也重置no_reply计数
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
NoReplyAction.reset_consecutive_count()
|
||||
self.context.no_reply_consecutive = 0
|
||||
logger.info(f"{self.context.log_prefix} 执行了回复动作,重置no_reply计数器")
|
||||
|
||||
if hasattr(self.context, 'chat_instance') and self.context.chat_instance:
|
||||
self.context.chat_instance.recent_interest_records.clear()
|
||||
logger.info(f"{self.context.log_prefix} 执行了{action_type}动作,重置no_reply计数器和兴趣度记录")
|
||||
|
||||
if action_type == "no_reply":
|
||||
self.context.no_reply_consecutive += 1
|
||||
# 调用HeartFChatting中的_determine_form_type方法
|
||||
|
||||
@@ -3,6 +3,7 @@ import time
|
||||
import traceback
|
||||
import random
|
||||
from typing import Optional, List, Dict, Any, Tuple
|
||||
from collections import deque
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
@@ -55,6 +56,9 @@ class HeartFChatting:
|
||||
self.context.chat_instance = self
|
||||
|
||||
self._loop_task: Optional[asyncio.Task] = None
|
||||
|
||||
# 记录最近3次的兴趣度
|
||||
self.recent_interest_records: deque = deque(maxlen=3)
|
||||
|
||||
self._initialize_chat_mode()
|
||||
logger.info(f"{self.context.log_prefix} HeartFChatting 初始化完成")
|
||||
@@ -242,23 +246,20 @@ class HeartFChatting:
|
||||
logger.info(f"{self.context.log_prefix} 从睡眠中被唤醒,将处理积压的消息。")
|
||||
|
||||
# 根据聊天模式处理新消息
|
||||
if self.context.loop_mode == ChatMode.FOCUS:
|
||||
# 如果上一个动作是no_reply,则执行no_reply逻辑
|
||||
if self.context.last_action == "no_reply":
|
||||
if not await self._execute_no_reply(recent_messages):
|
||||
self.context.energy_value -= 0.3 / global_config.chat.focus_value
|
||||
logger.info(f"{self.context.log_prefix} 能量值减少,当前能量值:{self.context.energy_value:.1f}")
|
||||
return has_new_messages
|
||||
# 统一使用 _should_process_messages 判断是否应该处理
|
||||
if not self._should_process_messages(recent_messages if has_new_messages else None):
|
||||
return has_new_messages
|
||||
|
||||
if self.context.loop_mode == ChatMode.FOCUS:
|
||||
# 处理新消息
|
||||
for message in recent_messages:
|
||||
await self.cycle_processor.observe(message)
|
||||
|
||||
|
||||
# 如果成功观察,增加能量值
|
||||
if has_new_messages:
|
||||
self.context.energy_value += 1 / global_config.chat.focus_value
|
||||
logger.info(f"{self.context.log_prefix} 能量值增加,当前能量值:{self.context.energy_value:.1f}")
|
||||
|
||||
|
||||
self._check_focus_exit()
|
||||
elif self.context.loop_mode == ChatMode.NORMAL:
|
||||
self._check_focus_entry(len(recent_messages))
|
||||
@@ -399,9 +400,8 @@ class HeartFChatting:
|
||||
self.context.focus_energy = 1
|
||||
else:
|
||||
# 计算最近三次记录的兴趣度总和
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
total_recent_interest = sum(NoReplyAction._recent_interest_records)
|
||||
|
||||
total_recent_interest = sum(self.recent_interest_records)
|
||||
|
||||
# 获取当前聊天频率和意愿系数
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.context.stream_id)
|
||||
|
||||
@@ -417,6 +417,22 @@ class HeartFChatting:
|
||||
else:
|
||||
logger.info(f"{self.context.log_prefix} 兴趣度充足")
|
||||
self.context.focus_energy = 1
|
||||
|
||||
def _should_process_messages(self, messages: List[Dict[str, Any]] = None) -> bool:
|
||||
"""
|
||||
统一判断是否应该处理消息的函数
|
||||
根据当前循环模式和消息内容决定是否继续处理
|
||||
"""
|
||||
from src.chat.utils.utils_image import is_image_message
|
||||
|
||||
if self.context.loop_mode == ChatMode.FOCUS:
|
||||
if self.context.last_action == "no_reply":
|
||||
if messages:
|
||||
return self._execute_no_reply(messages)
|
||||
return False
|
||||
return True
|
||||
|
||||
return True
|
||||
|
||||
async def _execute_no_reply(self, new_message: List[Dict[str, Any]]) -> bool:
|
||||
"""执行breaking形式的no_reply(原有逻辑)"""
|
||||
@@ -427,14 +443,13 @@ class HeartFChatting:
|
||||
|
||||
if new_message_count >= modified_exit_count_threshold:
|
||||
# 记录兴趣度到列表
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
total_interest = 0.0
|
||||
for msg_dict in new_message:
|
||||
interest_value = msg_dict.get("interest_value", 0.0)
|
||||
if msg_dict.get("processed_plain_text", ""):
|
||||
total_interest += interest_value
|
||||
|
||||
NoReplyAction._recent_interest_records.append(total_interest)
|
||||
self.recent_interest_records.append(total_interest)
|
||||
|
||||
logger.info(
|
||||
f"{self.context.log_prefix} 累计消息数量达到{new_message_count}条(>{modified_exit_count_threshold}),结束等待"
|
||||
@@ -458,11 +473,10 @@ class HeartFChatting:
|
||||
|
||||
if accumulated_interest >= 3 / talk_frequency:
|
||||
# 记录兴趣度到列表
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
NoReplyAction._recent_interest_records.append(accumulated_interest)
|
||||
self.recent_interest_records.append(accumulated_interest)
|
||||
|
||||
logger.info(
|
||||
f"{self.context.log_prefix} 累计兴趣值达到{accumulated_interest:.2f}(>{5 / talk_frequency}),结束等待"
|
||||
f"{self.context.log_prefix} 累计兴趣值达到{accumulated_interest:.2f}(>{3 / talk_frequency}),结束等待"
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
@@ -57,9 +57,11 @@ async def _calculate_interest(message: MessageRecv) -> Tuple[float, bool, list[s
|
||||
with Timer("记忆激活"):
|
||||
interested_rate, keywords = await hippocampus_manager.get_activate_from_text(
|
||||
message.processed_plain_text,
|
||||
max_depth=5,
|
||||
max_depth=4,
|
||||
fast_retrieval=False,
|
||||
)
|
||||
message.key_words = keywords
|
||||
message.key_words_lite = keywords
|
||||
logger.debug(f"记忆激活率: {interested_rate:.2f}, 关键词: {keywords}")
|
||||
|
||||
text_len = len(message.processed_plain_text)
|
||||
|
||||
@@ -324,14 +324,14 @@ class Hippocampus:
|
||||
# 使用LLM提取关键词 - 根据详细文本长度分布优化topic_num计算
|
||||
text_length = len(text)
|
||||
topic_num: int | list[int] = 0
|
||||
if text_length <= 5:
|
||||
if text_length <= 6:
|
||||
words = jieba.cut(text)
|
||||
keywords = [word for word in words if len(word) > 1]
|
||||
keywords = list(set(keywords))[:3] # 限制最多3个关键词
|
||||
if keywords:
|
||||
logger.debug(f"提取关键词: {keywords}")
|
||||
return keywords
|
||||
elif text_length <= 10:
|
||||
elif text_length <= 12:
|
||||
topic_num = [1, 3] # 6-10字符: 1个关键词 (27.18%的文本)
|
||||
elif text_length <= 20:
|
||||
topic_num = [2, 4] # 11-20字符: 2个关键词 (22.76%的文本)
|
||||
@@ -777,7 +777,7 @@ class Hippocampus:
|
||||
total_nodes = len(self.memory_graph.G.nodes())
|
||||
# activated_nodes = len(activate_map)
|
||||
activation_ratio = total_activation / total_nodes if total_nodes > 0 else 0
|
||||
activation_ratio = activation_ratio * 60
|
||||
activation_ratio = activation_ratio * 50
|
||||
logger.debug(f"总激活值: {total_activation:.2f}, 总节点数: {total_nodes}, 激活: {activation_ratio}")
|
||||
|
||||
return activation_ratio, keywords
|
||||
|
||||
@@ -120,6 +120,9 @@ class MessageRecv(Message):
|
||||
self.priority_mode = "interest"
|
||||
self.priority_info = None
|
||||
self.interest_value: float = 0.0
|
||||
|
||||
self.key_words = []
|
||||
self.key_words_lite = []
|
||||
|
||||
def update_chat_stream(self, chat_stream: "ChatStream"):
|
||||
self.chat_stream = chat_stream
|
||||
|
||||
@@ -14,6 +14,23 @@ logger = get_logger("message_storage")
|
||||
|
||||
|
||||
class MessageStorage:
|
||||
@staticmethod
|
||||
def _serialize_keywords(keywords) -> str:
|
||||
"""将关键词列表序列化为JSON字符串"""
|
||||
if isinstance(keywords, list):
|
||||
return orjson.dumps(keywords).decode("utf-8")
|
||||
return "[]"
|
||||
|
||||
@staticmethod
|
||||
def _deserialize_keywords(keywords_str: str) -> list:
|
||||
"""将JSON字符串反序列化为关键词列表"""
|
||||
if not keywords_str:
|
||||
return []
|
||||
try:
|
||||
return orjson.loads(keywords_str)
|
||||
except (orjson.JSONDecodeError, TypeError):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
async def store_message(message: Union[MessageSending, MessageRecv], chat_stream: ChatStream) -> None:
|
||||
"""存储消息到数据库"""
|
||||
@@ -44,6 +61,8 @@ class MessageStorage:
|
||||
is_picid = False
|
||||
is_notify = False
|
||||
is_command = False
|
||||
key_words = ""
|
||||
key_words_lite = ""
|
||||
else:
|
||||
filtered_display_message = ""
|
||||
interest_value = message.interest_value
|
||||
@@ -55,6 +74,9 @@ class MessageStorage:
|
||||
is_picid = message.is_picid
|
||||
is_notify = message.is_notify
|
||||
is_command = message.is_command
|
||||
# 序列化关键词列表为JSON字符串
|
||||
key_words = MessageStorage._serialize_keywords(message.key_words)
|
||||
key_words_lite = MessageStorage._serialize_keywords(message.key_words_lite)
|
||||
|
||||
chat_info_dict = chat_stream.to_dict()
|
||||
user_info_dict = message.message_info.user_info.to_dict() # type: ignore
|
||||
@@ -103,6 +125,8 @@ class MessageStorage:
|
||||
is_picid=is_picid,
|
||||
is_notify=is_notify,
|
||||
is_command=is_command,
|
||||
key_words=key_words,
|
||||
key_words_lite=key_words_lite,
|
||||
)
|
||||
with get_db_session() as session:
|
||||
session.add(new_message)
|
||||
|
||||
Reference in New Issue
Block a user