fix:优化激活值,优化logger显示
This commit is contained in:
@@ -228,6 +228,26 @@ CHAT_STYLE_CONFIG = {
|
||||
},
|
||||
}
|
||||
|
||||
SUB_HEARTFLOW_STYLE_CONFIG = {
|
||||
"advanced": {
|
||||
"console_format": (
|
||||
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||||
"<level>{level: <8}</level> | "
|
||||
"<cyan>{extra[module]: <12}</cyan> | "
|
||||
"<light-blue>麦麦小脑袋</light-blue> | "
|
||||
"<level>{message}</level>"
|
||||
),
|
||||
"file_format": ("{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦小脑袋 | {message}"),
|
||||
},
|
||||
"simple": {
|
||||
"console_format": ("<green>{time:MM-DD HH:mm}</green> | <light-blue>麦麦小脑袋</light-blue> | <green>{message}</green>"), # noqa: E501
|
||||
"file_format": ("{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦小脑袋 | {message}"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
# 根据SIMPLE_OUTPUT选择配置
|
||||
MEMORY_STYLE_CONFIG = MEMORY_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else MEMORY_STYLE_CONFIG["advanced"]
|
||||
TOPIC_STYLE_CONFIG = TOPIC_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else TOPIC_STYLE_CONFIG["advanced"]
|
||||
@@ -238,6 +258,7 @@ MOOD_STYLE_CONFIG = MOOD_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else MOOD_STYLE
|
||||
RELATION_STYLE_CONFIG = RELATION_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else RELATION_STYLE_CONFIG["advanced"]
|
||||
SCHEDULE_STYLE_CONFIG = SCHEDULE_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SCHEDULE_STYLE_CONFIG["advanced"]
|
||||
HEARTFLOW_STYLE_CONFIG = HEARTFLOW_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else HEARTFLOW_STYLE_CONFIG["advanced"]
|
||||
SUB_HEARTFLOW_STYLE_CONFIG = SUB_HEARTFLOW_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SUB_HEARTFLOW_STYLE_CONFIG["advanced"] # noqa: E501
|
||||
|
||||
def is_registered_module(record: dict) -> bool:
|
||||
"""检查是否为已注册的模块"""
|
||||
|
||||
@@ -130,9 +130,9 @@ class ChatBot:
|
||||
# 根据话题计算激活度
|
||||
topic = ""
|
||||
interested_rate = await HippocampusManager.get_instance().get_activate_from_text(
|
||||
message.processed_plain_text)*300
|
||||
message.processed_plain_text,fast_retrieval=True)
|
||||
# interested_rate = 0.1
|
||||
logger.info(f"对{message.processed_plain_text}的激活度:{interested_rate}")
|
||||
# logger.info(f"对{message.processed_plain_text}的激活度:{interested_rate}")
|
||||
# logger.info(f"\033[1;32m[主题识别]\033[0m 使用{global_config.topic_extract}主题: {topic}")
|
||||
|
||||
await self.storage.store_message(message, chat, topic[0] if topic else None)
|
||||
|
||||
@@ -81,15 +81,15 @@ class PromptBuilder:
|
||||
# 调用 hippocampus 的 get_relevant_memories 方法
|
||||
relevant_memories = await HippocampusManager.get_instance().get_memory_from_text(
|
||||
text=message_txt,
|
||||
max_memory_num=4,
|
||||
max_memory_num=3,
|
||||
max_memory_length=2,
|
||||
max_depth=3,
|
||||
fast_retrieval=True
|
||||
fast_retrieval=False
|
||||
)
|
||||
memory_str = ""
|
||||
for _topic, memories in relevant_memories:
|
||||
memory_str += f"{memories}\n"
|
||||
print(f"memory_str: {memory_str}")
|
||||
# print(f"memory_str: {memory_str}")
|
||||
|
||||
if relevant_memories:
|
||||
# 格式化记忆内容
|
||||
|
||||
@@ -817,8 +817,8 @@ class Hippocampus:
|
||||
self.parahippocampal_gyrus = ParahippocampalGyrus(self)
|
||||
# 从数据库加载记忆图
|
||||
self.entorhinal_cortex.sync_memory_from_db()
|
||||
self.llm_topic_judge = LLM_request(self.config.llm_topic_judge)
|
||||
self.llm_summary_by_topic = LLM_request(self.config.llm_summary_by_topic)
|
||||
self.llm_topic_judge = LLM_request(self.config.llm_topic_judge,request_type="memory")
|
||||
self.llm_summary_by_topic = LLM_request(self.config.llm_summary_by_topic,request_type="memory")
|
||||
|
||||
def get_all_node_names(self) -> list:
|
||||
"""获取记忆图中所有节点的名字列表"""
|
||||
@@ -950,7 +950,7 @@ class Hippocampus:
|
||||
# 提取关键词
|
||||
keywords = re.findall(r'<([^>]+)>', topics_response[0])
|
||||
if not keywords:
|
||||
keywords = ['none']
|
||||
keywords = []
|
||||
else:
|
||||
keywords = [
|
||||
keyword.strip()
|
||||
@@ -1025,7 +1025,7 @@ class Hippocampus:
|
||||
|
||||
# 基于激活值平方的独立概率选择
|
||||
remember_map = {}
|
||||
logger.info("基于激活值平方的归一化选择:")
|
||||
# logger.info("基于激活值平方的归一化选择:")
|
||||
|
||||
# 计算所有激活值的平方和
|
||||
total_squared_activation = sum(activation ** 2 for activation in activate_map.values())
|
||||
@@ -1080,11 +1080,10 @@ class Hippocampus:
|
||||
# 获取最匹配的记忆
|
||||
top_memories = memory_similarities[:max_memory_length]
|
||||
|
||||
|
||||
# 添加到结果中
|
||||
for memory, similarity in top_memories:
|
||||
all_memories.append((node, [memory], similarity))
|
||||
logger.info(f"选中记忆: {memory} (相似度: {similarity:.2f})")
|
||||
# logger.info(f"选中记忆: {memory} (相似度: {similarity:.2f})")
|
||||
else:
|
||||
logger.info("节点没有记忆")
|
||||
|
||||
@@ -1148,7 +1147,7 @@ class Hippocampus:
|
||||
# 提取关键词
|
||||
keywords = re.findall(r'<([^>]+)>', topics_response[0])
|
||||
if not keywords:
|
||||
keywords = ['none']
|
||||
keywords = []
|
||||
else:
|
||||
keywords = [
|
||||
keyword.strip()
|
||||
@@ -1221,10 +1220,13 @@ class Hippocampus:
|
||||
# logger.info(f"节点 '{node}': 累计激活值 = {total_activation:.2f}")
|
||||
|
||||
# 计算激活节点数与总节点数的比值
|
||||
total_activation = sum(activate_map.values())
|
||||
logger.info(f"总激活值: {total_activation:.2f}")
|
||||
total_nodes = len(self.memory_graph.G.nodes())
|
||||
activated_nodes = len(activate_map)
|
||||
activation_ratio = activated_nodes / total_nodes if total_nodes > 0 else 0
|
||||
logger.info(f"激活节点数: {activated_nodes}, 总节点数: {total_nodes}, 激活比例: {activation_ratio}")
|
||||
# activated_nodes = len(activate_map)
|
||||
activation_ratio = total_activation / total_nodes if total_nodes > 0 else 0
|
||||
activation_ratio = activation_ratio*40
|
||||
logger.info(f"总激活值: {total_activation:.2f}, 总节点数: {total_nodes}, 激活: {activation_ratio}")
|
||||
|
||||
return activation_ratio
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from .current_mind import SubHeartflow
|
||||
from .sub_heartflow import SubHeartflow
|
||||
from src.plugins.moods.moods import MoodManager
|
||||
from src.plugins.models.utils_model import LLM_request
|
||||
from src.plugins.config.config import global_config, BotConfig
|
||||
@@ -46,7 +46,7 @@ class Heartflow:
|
||||
logger.info("麦麦大脑袋转起来了")
|
||||
self.current_state.update_current_state_info()
|
||||
|
||||
personality_info = " ".join(BotConfig.PROMPT_PERSONALITY)
|
||||
personality_info = " ".join(global_config.PROMPT_PERSONALITY)
|
||||
current_thinking_info = self.current_mind
|
||||
mood_info = self.current_state.mood
|
||||
related_memory_info = 'memory'
|
||||
|
||||
@@ -16,6 +16,10 @@ class Talking_info:
|
||||
self.observe_times = 0
|
||||
self.activate = 360
|
||||
|
||||
self.last_summary_time = int(datetime.now().timestamp()) # 上次更新summary的时间
|
||||
self.summary_count = 0 # 30秒内的更新次数
|
||||
self.max_update_in_30s = 2
|
||||
|
||||
self.oberve_interval = 3
|
||||
|
||||
self.llm_summary = LLM_request(
|
||||
@@ -60,16 +64,22 @@ class Talking_info:
|
||||
if len(self.talking_message) > 20:
|
||||
self.talking_message = self.talking_message[-20:] # 只保留最新的20条
|
||||
self.translate_message_list_to_str()
|
||||
# print(self.talking_message_str)
|
||||
self.observe_times += 1
|
||||
self.last_observe_time = new_messages[-1]["time"]
|
||||
|
||||
if self.observe_times > 3:
|
||||
# 检查是否需要更新summary
|
||||
current_time = int(datetime.now().timestamp())
|
||||
if current_time - self.last_summary_time >= 30: # 如果超过30秒,重置计数
|
||||
self.summary_count = 0
|
||||
self.last_summary_time = current_time
|
||||
|
||||
if self.summary_count < self.max_update_in_30s: # 如果30秒内更新次数小于2次
|
||||
await self.update_talking_summary()
|
||||
# print(f"更新了聊天总结:{self.talking_summary}")
|
||||
self.summary_count += 1
|
||||
|
||||
async def update_talking_summary(self):
|
||||
#基于已经有的talking_summary,和新的talking_message,生成一个summary
|
||||
# print(f"更新聊天总结:{self.talking_summary}")
|
||||
prompt = ""
|
||||
prompt = f"你正在参与一个qq群聊的讨论,这个群之前在聊的内容是:{self.talking_summary}\n"
|
||||
prompt += f"现在群里的群友们产生了新的讨论,有了新的发言,具体内容如下:{self.talking_message_str}\n"
|
||||
|
||||
@@ -6,6 +6,16 @@ from src.plugins.config.config import global_config, BotConfig
|
||||
import re
|
||||
import time
|
||||
from src.plugins.schedule.schedule_generator import bot_schedule
|
||||
from src.plugins.memory_system.Hippocampus import HippocampusManager
|
||||
from src.common.logger import get_module_logger, LogConfig, SUB_HEARTFLOW_STYLE_CONFIG # noqa: E402
|
||||
|
||||
subheartflow_config = LogConfig(
|
||||
# 使用海马体专用样式
|
||||
console_format=SUB_HEARTFLOW_STYLE_CONFIG["console_format"],
|
||||
file_format=SUB_HEARTFLOW_STYLE_CONFIG["file_format"],
|
||||
)
|
||||
logger = get_module_logger("subheartflow", config=subheartflow_config)
|
||||
|
||||
|
||||
class CuttentState:
|
||||
def __init__(self):
|
||||
@@ -37,7 +47,7 @@ class SubHeartflow:
|
||||
if not self.current_mind:
|
||||
self.current_mind = "你什么也没想"
|
||||
|
||||
self.personality_info = " ".join(BotConfig.PROMPT_PERSONALITY)
|
||||
self.personality_info = " ".join(global_config.PROMPT_PERSONALITY)
|
||||
|
||||
def assign_observe(self,stream_id):
|
||||
self.outer_world = outer_world.get_world_by_stream_id(stream_id)
|
||||
@@ -55,23 +65,42 @@ class SubHeartflow:
|
||||
await asyncio.sleep(60)
|
||||
|
||||
async def do_a_thinking(self):
|
||||
print("麦麦小脑袋转起来了")
|
||||
self.current_state.update_current_state_info()
|
||||
|
||||
current_thinking_info = self.current_mind
|
||||
mood_info = self.current_state.mood
|
||||
related_memory_info = ''
|
||||
|
||||
message_stream_info = self.outer_world.talking_summary
|
||||
schedule_info = bot_schedule.get_current_num_task(num = 2,time_info = False)
|
||||
print(f"message_stream_info:{message_stream_info}")
|
||||
|
||||
related_memory = await HippocampusManager.get_instance().get_memory_from_text(
|
||||
text=message_stream_info,
|
||||
max_memory_num=3,
|
||||
max_memory_length=2,
|
||||
max_depth=3,
|
||||
fast_retrieval=False
|
||||
)
|
||||
# print(f"相关记忆:{related_memory}")
|
||||
if related_memory:
|
||||
related_memory_info = ""
|
||||
for memory in related_memory:
|
||||
related_memory_info += memory[1]
|
||||
else:
|
||||
related_memory_info = ''
|
||||
|
||||
print(f"相关记忆:{related_memory_info}")
|
||||
|
||||
schedule_info = bot_schedule.get_current_num_task(num = 1,time_info = False)
|
||||
|
||||
prompt = ""
|
||||
prompt += f"你刚刚在做的事情是:{schedule_info}\n"
|
||||
# prompt += f"麦麦的总体想法是:{self.main_heartflow_info}\n\n"
|
||||
prompt += f"{self.personality_info}\n"
|
||||
prompt += f"你{self.personality_info}\n"
|
||||
prompt += f"现在你正在上网,和qq群里的网友们聊天,群里正在聊的话题是:{message_stream_info}\n"
|
||||
prompt += f"你想起来{related_memory_info}。"
|
||||
if related_memory_info:
|
||||
prompt += f"你想起来{related_memory_info}。"
|
||||
prompt += f"刚刚你的想法是{current_thinking_info}。"
|
||||
prompt += f"你现在{mood_info}。"
|
||||
prompt += f"你现在{mood_info}。\n"
|
||||
prompt += "现在你接下去继续思考,产生新的想法,不要分点输出,输出连贯的内心独白,不要太长,"
|
||||
prompt += "但是记得结合上述的消息,要记得维持住你的人设,关注聊天和新内容,不要思考太多:"
|
||||
reponse, reasoning_content = await self.llm_model.generate_response_async(prompt)
|
||||
@@ -79,7 +108,8 @@ class SubHeartflow:
|
||||
self.update_current_mind(reponse)
|
||||
|
||||
self.current_mind = reponse
|
||||
print(f"麦麦的脑内状态:{self.current_mind}")
|
||||
print(prompt)
|
||||
logger.info(f"麦麦的脑内状态:{self.current_mind}")
|
||||
|
||||
async def do_after_reply(self,reply_content,chat_talking_prompt):
|
||||
# print("麦麦脑袋转起来了")
|
||||
@@ -91,24 +121,29 @@ class SubHeartflow:
|
||||
message_stream_info = self.outer_world.talking_summary
|
||||
message_new_info = chat_talking_prompt
|
||||
reply_info = reply_content
|
||||
schedule_info = bot_schedule.get_current_num_task(num = 1,time_info = False)
|
||||
|
||||
|
||||
prompt = ""
|
||||
prompt += f"{self.personality_info}\n"
|
||||
prompt += f"你刚刚在做的事情是:{schedule_info}\n"
|
||||
prompt += f"你{self.personality_info}\n"
|
||||
|
||||
prompt += f"现在你正在上网,和qq群里的网友们聊天,群里正在聊的话题是:{message_stream_info}\n"
|
||||
prompt += f"你想起来{related_memory_info}。"
|
||||
if related_memory_info:
|
||||
prompt += f"你想起来{related_memory_info}。"
|
||||
prompt += f"刚刚你的想法是{current_thinking_info}。"
|
||||
prompt += f"你现在看到了网友们发的新消息:{message_new_info}\n"
|
||||
prompt += f"你刚刚回复了群友们:{reply_info}"
|
||||
prompt += f"你现在{mood_info}。"
|
||||
prompt += "现在你接下去继续思考,产生新的想法,记得保留你刚刚的想法,不要分点输出,输出连贯的内心独白"
|
||||
prompt += "不要太长,但是记得结合上述的消息,要记得你的人设,关注聊天和新内容,以及你回复的内容,不要思考太多:"
|
||||
prompt += "不要太长,但是记得结合上述的消息,要记得你的人设,关注聊天和新内容,关注你回复的内容,不要思考太多:"
|
||||
|
||||
reponse, reasoning_content = await self.llm_model.generate_response_async(prompt)
|
||||
|
||||
self.update_current_mind(reponse)
|
||||
|
||||
self.current_mind = reponse
|
||||
print(f"{self.observe_chat_id}麦麦的脑内状态:{self.current_mind}")
|
||||
logger.info(f"麦麦回复后的脑内状态:{self.current_mind}")
|
||||
|
||||
self.last_reply_time = time.time()
|
||||
|
||||
@@ -133,7 +168,7 @@ class SubHeartflow:
|
||||
else:
|
||||
self.current_state.willing = 0
|
||||
|
||||
print(f"{self.observe_chat_id}麦麦的回复意愿:{self.current_state.willing}")
|
||||
logger.info(f"{self.observe_chat_id}麦麦的回复意愿:{self.current_state.willing}")
|
||||
|
||||
return self.current_state.willing
|
||||
|
||||
@@ -3,7 +3,7 @@ version = "0.0.11"
|
||||
|
||||
[mai_version]
|
||||
version = "0.6.0"
|
||||
version-fix = "snapshot-1"
|
||||
version-fix = "snapshot-2"
|
||||
|
||||
#以下是给开发人员阅读的,一般用户不需要阅读
|
||||
#如果你想要修改配置文件,请在修改后将version的值进行变更
|
||||
|
||||
Reference in New Issue
Block a user