迁移:69a855d(feat:保存关键词到message数据库)
This commit is contained in:
@@ -159,18 +159,11 @@ class CycleProcessor:
|
|||||||
self.context.last_action = action_type
|
self.context.last_action = action_type
|
||||||
|
|
||||||
# 处理no_reply相关的逻辑
|
# 处理no_reply相关的逻辑
|
||||||
if action_type != "no_reply" and action_type != "no_action":
|
if action_type != "no_reply":
|
||||||
# 导入NoReplyAction并重置计数器
|
|
||||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
|
||||||
NoReplyAction.reset_consecutive_count()
|
|
||||||
self.context.no_reply_consecutive = 0
|
self.context.no_reply_consecutive = 0
|
||||||
logger.info(f"{self.context.log_prefix} 执行了{action_type}动作,重置no_reply计数器")
|
if hasattr(self.context, 'chat_instance') and self.context.chat_instance:
|
||||||
elif action_type == "no_action":
|
self.context.chat_instance.recent_interest_records.clear()
|
||||||
# 当执行回复动作时,也重置no_reply计数
|
logger.info(f"{self.context.log_prefix} 执行了{action_type}动作,重置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 action_type == "no_reply":
|
if action_type == "no_reply":
|
||||||
self.context.no_reply_consecutive += 1
|
self.context.no_reply_consecutive += 1
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import time
|
|||||||
import traceback
|
import traceback
|
||||||
import random
|
import random
|
||||||
from typing import Optional, List, Dict, Any, Tuple
|
from typing import Optional, List, Dict, Any, Tuple
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.config.config import global_config
|
from src.config.config import global_config
|
||||||
@@ -56,6 +57,9 @@ class HeartFChatting:
|
|||||||
|
|
||||||
self._loop_task: Optional[asyncio.Task] = None
|
self._loop_task: Optional[asyncio.Task] = None
|
||||||
|
|
||||||
|
# 记录最近3次的兴趣度
|
||||||
|
self.recent_interest_records: deque = deque(maxlen=3)
|
||||||
|
|
||||||
self._initialize_chat_mode()
|
self._initialize_chat_mode()
|
||||||
logger.info(f"{self.context.log_prefix} HeartFChatting 初始化完成")
|
logger.info(f"{self.context.log_prefix} HeartFChatting 初始化完成")
|
||||||
|
|
||||||
@@ -242,14 +246,11 @@ class HeartFChatting:
|
|||||||
logger.info(f"{self.context.log_prefix} 从睡眠中被唤醒,将处理积压的消息。")
|
logger.info(f"{self.context.log_prefix} 从睡眠中被唤醒,将处理积压的消息。")
|
||||||
|
|
||||||
# 根据聊天模式处理新消息
|
# 根据聊天模式处理新消息
|
||||||
if self.context.loop_mode == ChatMode.FOCUS:
|
# 统一使用 _should_process_messages 判断是否应该处理
|
||||||
# 如果上一个动作是no_reply,则执行no_reply逻辑
|
if not self._should_process_messages(recent_messages if has_new_messages else None):
|
||||||
if self.context.last_action == "no_reply":
|
return has_new_messages
|
||||||
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
|
|
||||||
|
|
||||||
|
if self.context.loop_mode == ChatMode.FOCUS:
|
||||||
# 处理新消息
|
# 处理新消息
|
||||||
for message in recent_messages:
|
for message in recent_messages:
|
||||||
await self.cycle_processor.observe(message)
|
await self.cycle_processor.observe(message)
|
||||||
@@ -399,8 +400,7 @@ class HeartFChatting:
|
|||||||
self.context.focus_energy = 1
|
self.context.focus_energy = 1
|
||||||
else:
|
else:
|
||||||
# 计算最近三次记录的兴趣度总和
|
# 计算最近三次记录的兴趣度总和
|
||||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
total_recent_interest = sum(self.recent_interest_records)
|
||||||
total_recent_interest = sum(NoReplyAction._recent_interest_records)
|
|
||||||
|
|
||||||
# 获取当前聊天频率和意愿系数
|
# 获取当前聊天频率和意愿系数
|
||||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.context.stream_id)
|
talk_frequency = global_config.chat.get_current_talk_frequency(self.context.stream_id)
|
||||||
@@ -418,6 +418,22 @@ class HeartFChatting:
|
|||||||
logger.info(f"{self.context.log_prefix} 兴趣度充足")
|
logger.info(f"{self.context.log_prefix} 兴趣度充足")
|
||||||
self.context.focus_energy = 1
|
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:
|
async def _execute_no_reply(self, new_message: List[Dict[str, Any]]) -> bool:
|
||||||
"""执行breaking形式的no_reply(原有逻辑)"""
|
"""执行breaking形式的no_reply(原有逻辑)"""
|
||||||
new_message_count = len(new_message)
|
new_message_count = len(new_message)
|
||||||
@@ -427,14 +443,13 @@ class HeartFChatting:
|
|||||||
|
|
||||||
if new_message_count >= modified_exit_count_threshold:
|
if new_message_count >= modified_exit_count_threshold:
|
||||||
# 记录兴趣度到列表
|
# 记录兴趣度到列表
|
||||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
|
||||||
total_interest = 0.0
|
total_interest = 0.0
|
||||||
for msg_dict in new_message:
|
for msg_dict in new_message:
|
||||||
interest_value = msg_dict.get("interest_value", 0.0)
|
interest_value = msg_dict.get("interest_value", 0.0)
|
||||||
if msg_dict.get("processed_plain_text", ""):
|
if msg_dict.get("processed_plain_text", ""):
|
||||||
total_interest += interest_value
|
total_interest += interest_value
|
||||||
|
|
||||||
NoReplyAction._recent_interest_records.append(total_interest)
|
self.recent_interest_records.append(total_interest)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"{self.context.log_prefix} 累计消息数量达到{new_message_count}条(>{modified_exit_count_threshold}),结束等待"
|
f"{self.context.log_prefix} 累计消息数量达到{new_message_count}条(>{modified_exit_count_threshold}),结束等待"
|
||||||
@@ -458,11 +473,10 @@ class HeartFChatting:
|
|||||||
|
|
||||||
if accumulated_interest >= 3 / talk_frequency:
|
if accumulated_interest >= 3 / talk_frequency:
|
||||||
# 记录兴趣度到列表
|
# 记录兴趣度到列表
|
||||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
self.recent_interest_records.append(accumulated_interest)
|
||||||
NoReplyAction._recent_interest_records.append(accumulated_interest)
|
|
||||||
|
|
||||||
logger.info(
|
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
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -57,9 +57,11 @@ async def _calculate_interest(message: MessageRecv) -> Tuple[float, bool, list[s
|
|||||||
with Timer("记忆激活"):
|
with Timer("记忆激活"):
|
||||||
interested_rate, keywords = await hippocampus_manager.get_activate_from_text(
|
interested_rate, keywords = await hippocampus_manager.get_activate_from_text(
|
||||||
message.processed_plain_text,
|
message.processed_plain_text,
|
||||||
max_depth=5,
|
max_depth=4,
|
||||||
fast_retrieval=False,
|
fast_retrieval=False,
|
||||||
)
|
)
|
||||||
|
message.key_words = keywords
|
||||||
|
message.key_words_lite = keywords
|
||||||
logger.debug(f"记忆激活率: {interested_rate:.2f}, 关键词: {keywords}")
|
logger.debug(f"记忆激活率: {interested_rate:.2f}, 关键词: {keywords}")
|
||||||
|
|
||||||
text_len = len(message.processed_plain_text)
|
text_len = len(message.processed_plain_text)
|
||||||
|
|||||||
@@ -324,14 +324,14 @@ class Hippocampus:
|
|||||||
# 使用LLM提取关键词 - 根据详细文本长度分布优化topic_num计算
|
# 使用LLM提取关键词 - 根据详细文本长度分布优化topic_num计算
|
||||||
text_length = len(text)
|
text_length = len(text)
|
||||||
topic_num: int | list[int] = 0
|
topic_num: int | list[int] = 0
|
||||||
if text_length <= 5:
|
if text_length <= 6:
|
||||||
words = jieba.cut(text)
|
words = jieba.cut(text)
|
||||||
keywords = [word for word in words if len(word) > 1]
|
keywords = [word for word in words if len(word) > 1]
|
||||||
keywords = list(set(keywords))[:3] # 限制最多3个关键词
|
keywords = list(set(keywords))[:3] # 限制最多3个关键词
|
||||||
if keywords:
|
if keywords:
|
||||||
logger.debug(f"提取关键词: {keywords}")
|
logger.debug(f"提取关键词: {keywords}")
|
||||||
return keywords
|
return keywords
|
||||||
elif text_length <= 10:
|
elif text_length <= 12:
|
||||||
topic_num = [1, 3] # 6-10字符: 1个关键词 (27.18%的文本)
|
topic_num = [1, 3] # 6-10字符: 1个关键词 (27.18%的文本)
|
||||||
elif text_length <= 20:
|
elif text_length <= 20:
|
||||||
topic_num = [2, 4] # 11-20字符: 2个关键词 (22.76%的文本)
|
topic_num = [2, 4] # 11-20字符: 2个关键词 (22.76%的文本)
|
||||||
@@ -777,7 +777,7 @@ class Hippocampus:
|
|||||||
total_nodes = len(self.memory_graph.G.nodes())
|
total_nodes = len(self.memory_graph.G.nodes())
|
||||||
# activated_nodes = len(activate_map)
|
# activated_nodes = len(activate_map)
|
||||||
activation_ratio = total_activation / total_nodes if total_nodes > 0 else 0
|
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}")
|
logger.debug(f"总激活值: {total_activation:.2f}, 总节点数: {total_nodes}, 激活: {activation_ratio}")
|
||||||
|
|
||||||
return activation_ratio, keywords
|
return activation_ratio, keywords
|
||||||
|
|||||||
@@ -121,6 +121,9 @@ class MessageRecv(Message):
|
|||||||
self.priority_info = None
|
self.priority_info = None
|
||||||
self.interest_value: float = 0.0
|
self.interest_value: float = 0.0
|
||||||
|
|
||||||
|
self.key_words = []
|
||||||
|
self.key_words_lite = []
|
||||||
|
|
||||||
def update_chat_stream(self, chat_stream: "ChatStream"):
|
def update_chat_stream(self, chat_stream: "ChatStream"):
|
||||||
self.chat_stream = chat_stream
|
self.chat_stream = chat_stream
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,23 @@ logger = get_logger("message_storage")
|
|||||||
|
|
||||||
|
|
||||||
class MessageStorage:
|
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
|
@staticmethod
|
||||||
async def store_message(message: Union[MessageSending, MessageRecv], chat_stream: ChatStream) -> None:
|
async def store_message(message: Union[MessageSending, MessageRecv], chat_stream: ChatStream) -> None:
|
||||||
"""存储消息到数据库"""
|
"""存储消息到数据库"""
|
||||||
@@ -44,6 +61,8 @@ class MessageStorage:
|
|||||||
is_picid = False
|
is_picid = False
|
||||||
is_notify = False
|
is_notify = False
|
||||||
is_command = False
|
is_command = False
|
||||||
|
key_words = ""
|
||||||
|
key_words_lite = ""
|
||||||
else:
|
else:
|
||||||
filtered_display_message = ""
|
filtered_display_message = ""
|
||||||
interest_value = message.interest_value
|
interest_value = message.interest_value
|
||||||
@@ -55,6 +74,9 @@ class MessageStorage:
|
|||||||
is_picid = message.is_picid
|
is_picid = message.is_picid
|
||||||
is_notify = message.is_notify
|
is_notify = message.is_notify
|
||||||
is_command = message.is_command
|
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()
|
chat_info_dict = chat_stream.to_dict()
|
||||||
user_info_dict = message.message_info.user_info.to_dict() # type: ignore
|
user_info_dict = message.message_info.user_info.to_dict() # type: ignore
|
||||||
@@ -103,6 +125,8 @@ class MessageStorage:
|
|||||||
is_picid=is_picid,
|
is_picid=is_picid,
|
||||||
is_notify=is_notify,
|
is_notify=is_notify,
|
||||||
is_command=is_command,
|
is_command=is_command,
|
||||||
|
key_words=key_words,
|
||||||
|
key_words_lite=key_words_lite,
|
||||||
)
|
)
|
||||||
with get_db_session() as session:
|
with get_db_session() as session:
|
||||||
session.add(new_message)
|
session.add(new_message)
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ class Messages(Base):
|
|||||||
chat_id = Column(get_string_field(64), nullable=False, index=True)
|
chat_id = Column(get_string_field(64), nullable=False, index=True)
|
||||||
reply_to = Column(Text, nullable=True)
|
reply_to = Column(Text, nullable=True)
|
||||||
interest_value = Column(Float, nullable=True)
|
interest_value = Column(Float, nullable=True)
|
||||||
|
key_words = Column(Text, nullable=True)
|
||||||
|
key_words_lite = Column(Text, nullable=True)
|
||||||
is_mentioned = Column(Boolean, nullable=True)
|
is_mentioned = Column(Boolean, nullable=True)
|
||||||
|
|
||||||
# 从 chat_info 扁平化而来的字段
|
# 从 chat_info 扁平化而来的字段
|
||||||
|
|||||||
@@ -114,11 +114,7 @@ class ChatConfig(ValidatedConfigBase):
|
|||||||
|
|
||||||
# 检查全局时段配置(第一个元素为空字符串的配置)
|
# 检查全局时段配置(第一个元素为空字符串的配置)
|
||||||
global_frequency = self._get_global_frequency()
|
global_frequency = self._get_global_frequency()
|
||||||
if global_frequency is not None:
|
return self.talk_frequency if global_frequency is None else global_frequency
|
||||||
return global_frequency
|
|
||||||
|
|
||||||
# 如果都没有匹配,返回默认值
|
|
||||||
return self.talk_frequency
|
|
||||||
|
|
||||||
def _get_time_based_frequency(self, time_freq_list: list[str]) -> Optional[float]:
|
def _get_time_based_frequency(self, time_freq_list: list[str]) -> Optional[float]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
from typing import Tuple, List
|
from typing import Tuple
|
||||||
from collections import deque
|
|
||||||
|
|
||||||
# 导入新插件系统
|
# 导入新插件系统
|
||||||
from src.plugin_system import BaseAction, ActionActivationType, ChatMode
|
from src.plugin_system import BaseAction, ActionActivationType, ChatMode
|
||||||
@@ -23,11 +22,10 @@ class NoReplyAction(BaseAction):
|
|||||||
action_name = "no_reply"
|
action_name = "no_reply"
|
||||||
action_description = "暂时不回复消息"
|
action_description = "暂时不回复消息"
|
||||||
|
|
||||||
# 最近三次no_reply的新消息兴趣度记录
|
|
||||||
_recent_interest_records: deque = deque(maxlen=3)
|
|
||||||
|
|
||||||
# 动作参数定义
|
# 动作参数定义
|
||||||
action_parameters = {}
|
action_parameters = {
|
||||||
|
"reason": "不回复的原因",
|
||||||
|
}
|
||||||
|
|
||||||
# 动作使用场景
|
# 动作使用场景
|
||||||
action_require = [""]
|
action_require = [""]
|
||||||
@@ -60,14 +58,3 @@ class NoReplyAction(BaseAction):
|
|||||||
action_done=True,
|
action_done=True,
|
||||||
)
|
)
|
||||||
return False, f"不回复动作执行失败: {e}"
|
return False, f"不回复动作执行失败: {e}"
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def reset_consecutive_count(cls):
|
|
||||||
"""重置连续计数器和兴趣度记录"""
|
|
||||||
cls._recent_interest_records.clear()
|
|
||||||
logger.debug("NoReplyAction连续计数器和兴趣度记录已重置")
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_recent_interest_records(cls) -> List[float]:
|
|
||||||
"""获取最近的兴趣度记录"""
|
|
||||||
return list(cls._recent_interest_records)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user