better:更好的心流结构,使用了观察取代外部世界
This commit is contained in:
@@ -10,7 +10,7 @@ from .message_sender import message_manager
|
||||
from ..moods.moods import MoodManager
|
||||
from .llm_generator import ResponseGenerator
|
||||
from src.common.logger import get_module_logger
|
||||
from src.think_flow_demo.heartflow import subheartflow_manager
|
||||
from src.think_flow_demo.heartflow import heartflow
|
||||
from ...common.database import db
|
||||
|
||||
logger = get_module_logger("auto_speak")
|
||||
@@ -42,7 +42,7 @@ class AutoSpeakManager:
|
||||
while True and global_config.enable_think_flow:
|
||||
# 获取所有活跃的子心流
|
||||
active_subheartflows = []
|
||||
for chat_id, subheartflow in subheartflow_manager._subheartflows.items():
|
||||
for chat_id, subheartflow in heartflow._subheartflows.items():
|
||||
if (
|
||||
subheartflow.is_active and subheartflow.current_state.willing > 0
|
||||
): # 只考虑活跃且意愿值大于0.5的子心流
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import re
|
||||
import time
|
||||
from random import random
|
||||
import json
|
||||
|
||||
from ..memory_system.Hippocampus import HippocampusManager
|
||||
from ..moods.moods import MoodManager # 导入情绪管理器
|
||||
@@ -18,10 +17,9 @@ from .storage import MessageStorage
|
||||
from .utils import is_mentioned_bot_in_message, get_recent_group_detailed_plain_text
|
||||
from .utils_image import image_path_to_base64
|
||||
from ..willing.willing_manager import willing_manager # 导入意愿管理器
|
||||
from ..message import UserInfo, GroupInfo, Seg
|
||||
from ..message import UserInfo, Seg
|
||||
|
||||
from src.think_flow_demo.heartflow import subheartflow_manager
|
||||
from src.think_flow_demo.outer_world import outer_world
|
||||
from src.think_flow_demo.heartflow import heartflow
|
||||
from src.common.logger import get_module_logger, CHAT_STYLE_CONFIG, LogConfig
|
||||
|
||||
# 定义日志配置
|
||||
@@ -58,7 +56,7 @@ class ChatBot:
|
||||
5. 更新关系
|
||||
6. 更新情绪
|
||||
"""
|
||||
|
||||
|
||||
message = MessageRecv(message_data)
|
||||
groupinfo = message.message_info.group_info
|
||||
userinfo = message.message_info.user_info
|
||||
@@ -74,18 +72,8 @@ class ChatBot:
|
||||
)
|
||||
message.update_chat_stream(chat)
|
||||
|
||||
# 创建 心流 观察
|
||||
|
||||
await outer_world.check_and_add_new_observe()
|
||||
subheartflow_manager.create_subheartflow(chat.stream_id)
|
||||
|
||||
timer1 = time.time()
|
||||
await relationship_manager.update_relationship(
|
||||
chat_stream=chat,
|
||||
)
|
||||
await relationship_manager.update_relationship_value(chat_stream=chat, relationship_value=0)
|
||||
timer2 = time.time()
|
||||
logger.info(f"1关系更新时间: {timer2 - timer1}秒")
|
||||
# 创建 心流与chat的观察
|
||||
heartflow.create_subheartflow(chat.stream_id)
|
||||
|
||||
timer1 = time.time()
|
||||
await message.process()
|
||||
@@ -99,10 +87,9 @@ class ChatBot:
|
||||
):
|
||||
return
|
||||
|
||||
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time))
|
||||
|
||||
# 根据话题计算激活度
|
||||
await self.storage.store_message(message, chat)
|
||||
|
||||
|
||||
timer1 = time.time()
|
||||
interested_rate = 0
|
||||
@@ -117,8 +104,8 @@ class ChatBot:
|
||||
|
||||
if global_config.enable_think_flow:
|
||||
current_willing_old = willing_manager.get_willing(chat_stream=chat)
|
||||
current_willing_new = (subheartflow_manager.get_subheartflow(chat.stream_id).current_state.willing - 5) / 4
|
||||
print(f"4旧回复意愿:{current_willing_old},新回复意愿:{current_willing_new}")
|
||||
current_willing_new = (heartflow.get_subheartflow(chat.stream_id).current_state.willing - 5) / 4
|
||||
print(f"旧回复意愿:{current_willing_old},新回复意愿:{current_willing_new}")
|
||||
current_willing = (current_willing_old + current_willing_new) / 2
|
||||
else:
|
||||
current_willing = willing_manager.get_willing(chat_stream=chat)
|
||||
@@ -147,7 +134,8 @@ class ChatBot:
|
||||
else:
|
||||
mes_name = '私聊'
|
||||
|
||||
# print(f"mes_name: {mes_name}")
|
||||
#打印收到的信息的信息
|
||||
current_time = time.strftime("%H:%M:%S", time.localtime(messageinfo.time))
|
||||
logger.info(
|
||||
f"[{current_time}][{mes_name}]"
|
||||
f"{chat.user_info.user_nickname}:"
|
||||
@@ -225,7 +213,7 @@ class ChatBot:
|
||||
|
||||
return response_set, thinking_id
|
||||
|
||||
async def _update_using_response(self, message, chat, response_set):
|
||||
async def _update_using_response(self, message, response_set):
|
||||
# 更新心流状态
|
||||
stream_id = message.chat_stream.stream_id
|
||||
chat_talking_prompt = ""
|
||||
@@ -234,10 +222,10 @@ class ChatBot:
|
||||
stream_id, limit=global_config.MAX_CONTEXT_SIZE, combine=True
|
||||
)
|
||||
|
||||
if subheartflow_manager.get_subheartflow(stream_id):
|
||||
await subheartflow_manager.get_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
if heartflow.get_subheartflow(stream_id):
|
||||
await heartflow.get_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
else:
|
||||
await subheartflow_manager.create_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
await heartflow.create_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
|
||||
|
||||
async def _send_response_messages(self, message, chat, response_set, thinking_id):
|
||||
|
||||
@@ -97,9 +97,7 @@ class ResponseGenerator:
|
||||
logger.info(f"构建prompt时间: {timer2 - timer1}秒")
|
||||
|
||||
try:
|
||||
print(111111111111111111111111111111111111111111111111111111111)
|
||||
content, reasoning_content, self.current_model_name = await model.generate_response(prompt)
|
||||
print(222222222222222222222222222222222222222222222222222222222)
|
||||
except Exception:
|
||||
logger.exception("生成回复时出错")
|
||||
return None
|
||||
|
||||
@@ -12,7 +12,7 @@ from .chat_stream import chat_manager
|
||||
from .relationship_manager import relationship_manager
|
||||
from src.common.logger import get_module_logger
|
||||
|
||||
from src.think_flow_demo.heartflow import subheartflow_manager
|
||||
from src.think_flow_demo.heartflow import heartflow
|
||||
|
||||
logger = get_module_logger("prompt")
|
||||
|
||||
@@ -34,12 +34,11 @@ class PromptBuilder:
|
||||
# )
|
||||
|
||||
# outer_world_info = outer_world.outer_world_info
|
||||
if global_config.enable_think_flow:
|
||||
current_mind_info = subheartflow_manager.get_subheartflow(stream_id).current_mind
|
||||
else:
|
||||
current_mind_info = ""
|
||||
|
||||
relation_prompt = ""
|
||||
current_mind_info = heartflow.get_subheartflow(stream_id).current_mind
|
||||
|
||||
|
||||
# relation_prompt = ""
|
||||
# for person in who_chat_in_group:
|
||||
# relation_prompt += relationship_manager.build_relationship_info(person)
|
||||
|
||||
@@ -74,23 +73,22 @@ class PromptBuilder:
|
||||
chat_talking_prompt = chat_talking_prompt
|
||||
# print(f"\033[1;34m[调试]\033[0m 已从数据库获取群 {group_id} 的消息记录:{chat_talking_prompt}")
|
||||
|
||||
logger.info(f"聊天上下文prompt: {chat_talking_prompt}")
|
||||
|
||||
# 使用新的记忆获取方法
|
||||
memory_prompt = ""
|
||||
start_time = time.time()
|
||||
|
||||
# 调用 hippocampus 的 get_relevant_memories 方法
|
||||
# relevant_memories = await HippocampusManager.get_instance().get_memory_from_text(
|
||||
# text=message_txt, max_memory_num=3, max_memory_length=2, max_depth=2, fast_retrieval=True
|
||||
# )
|
||||
# memory_str = ""
|
||||
# for _topic, memories in relevant_memories:
|
||||
# memory_str += f"{memories}\n"
|
||||
#调用 hippocampus 的 get_relevant_memories 方法
|
||||
relevant_memories = await HippocampusManager.get_instance().get_memory_from_text(
|
||||
text=message_txt, max_memory_num=3, max_memory_length=2, max_depth=2, fast_retrieval=False
|
||||
)
|
||||
memory_str = ""
|
||||
for _topic, memories in relevant_memories:
|
||||
memory_str += f"{memories}\n"
|
||||
|
||||
# if relevant_memories:
|
||||
# # 格式化记忆内容
|
||||
# memory_prompt = f"你回忆起:\n{memory_str}\n"
|
||||
if relevant_memories:
|
||||
# 格式化记忆内容
|
||||
memory_prompt = f"你回忆起:\n{memory_str}\n"
|
||||
|
||||
end_time = time.time()
|
||||
logger.info(f"回忆耗时: {(end_time - start_time):.3f}秒")
|
||||
|
||||
@@ -29,7 +29,7 @@ class EnvConfig:
|
||||
if env_type == 'dev':
|
||||
env_file = self.ROOT_DIR / '.env.dev'
|
||||
elif env_type == 'prod':
|
||||
env_file = self.ROOT_DIR / '.env.prod'
|
||||
env_file = self.ROOT_DIR / '.env'
|
||||
|
||||
if env_file.exists():
|
||||
load_dotenv(env_file, override=True)
|
||||
|
||||
@@ -1266,13 +1266,13 @@ class HippocampusManager:
|
||||
node_count = len(memory_graph.nodes())
|
||||
edge_count = len(memory_graph.edges())
|
||||
|
||||
logger.success("--------------------------------")
|
||||
logger.success("记忆系统参数配置:")
|
||||
logger.success(f"构建间隔: {global_config.build_memory_interval}秒|样本数: {config.build_memory_sample_num},长度: {config.build_memory_sample_length}|压缩率: {config.memory_compress_rate}") # noqa: E501
|
||||
logger.success(f"记忆构建分布: {config.memory_build_distribution}")
|
||||
logger.success(f"遗忘间隔: {global_config.forget_memory_interval}秒|遗忘比例: {global_config.memory_forget_percentage}|遗忘: {config.memory_forget_time}小时之后") # noqa: E501
|
||||
logger.success(f"记忆图统计信息: 节点数量: {node_count}, 连接数量: {edge_count}")
|
||||
logger.success("--------------------------------")
|
||||
logger.success(f'''--------------------------------
|
||||
记忆系统参数配置:
|
||||
构建间隔: {global_config.build_memory_interval}秒|样本数: {config.build_memory_sample_num},长度: {config.build_memory_sample_length}|压缩率: {config.memory_compress_rate}
|
||||
记忆构建分布: {config.memory_build_distribution}
|
||||
遗忘间隔: {global_config.forget_memory_interval}秒|遗忘比例: {global_config.memory_forget_percentage}|遗忘: {config.memory_forget_time}小时之后
|
||||
记忆图统计信息: 节点数量: {node_count}, 连接数量: {edge_count}
|
||||
--------------------------------''') #noqa: E501
|
||||
|
||||
|
||||
return self._hippocampus
|
||||
|
||||
@@ -164,7 +164,7 @@ class LLM_request:
|
||||
# 常见Error Code Mapping
|
||||
error_code_mapping = {
|
||||
400: "参数不正确",
|
||||
401: "API key 错误,认证失败,请检查/config/bot_config.toml和.env.prod中的配置是否正确哦~",
|
||||
401: "API key 错误,认证失败,请检查/config/bot_config.toml和.env中的配置是否正确哦~",
|
||||
402: "账号余额不足",
|
||||
403: "需要实名,或余额不足",
|
||||
404: "Not Found",
|
||||
|
||||
@@ -10,7 +10,7 @@ import random
|
||||
|
||||
current_dir = Path(__file__).resolve().parent
|
||||
project_root = current_dir.parent.parent.parent
|
||||
env_path = project_root / ".env.prod"
|
||||
env_path = project_root / ".env"
|
||||
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.append(root_path)
|
||||
|
||||
@@ -17,7 +17,7 @@ import matplotlib.font_manager as fm
|
||||
|
||||
current_dir = Path(__file__).resolve().parent
|
||||
project_root = current_dir.parent.parent.parent
|
||||
env_path = project_root / ".env.prod"
|
||||
env_path = project_root / ".env"
|
||||
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.append(root_path)
|
||||
|
||||
@@ -9,7 +9,7 @@ from scipy import stats # 添加scipy导入用于t检验
|
||||
|
||||
current_dir = Path(__file__).resolve().parent
|
||||
project_root = current_dir.parent.parent.parent
|
||||
env_path = project_root / ".env.prod"
|
||||
env_path = project_root / ".env"
|
||||
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.append(root_path)
|
||||
|
||||
@@ -20,7 +20,7 @@ import sys
|
||||
"""
|
||||
current_dir = Path(__file__).resolve().parent
|
||||
project_root = current_dir.parent.parent.parent
|
||||
env_path = project_root / ".env.prod"
|
||||
env_path = project_root / ".env"
|
||||
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.append(root_path)
|
||||
|
||||
@@ -20,7 +20,7 @@ import sys
|
||||
"""
|
||||
current_dir = Path(__file__).resolve().parent
|
||||
project_root = current_dir.parent.parent.parent
|
||||
env_path = project_root / ".env.prod"
|
||||
env_path = project_root / ".env"
|
||||
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.append(root_path)
|
||||
|
||||
@@ -7,7 +7,7 @@ from typing import List, Dict, Optional
|
||||
|
||||
current_dir = Path(__file__).resolve().parent
|
||||
project_root = current_dir.parent.parent.parent
|
||||
env_path = project_root / ".env.prod"
|
||||
env_path = project_root / ".env"
|
||||
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.append(root_path)
|
||||
|
||||
@@ -84,7 +84,7 @@ class ScheduleGenerator:
|
||||
self.print_schedule()
|
||||
|
||||
# 执行当前活动
|
||||
# mind_thinking = subheartflow_manager.current_state.current_mind
|
||||
# mind_thinking = heartflow.current_state.current_mind
|
||||
|
||||
await self.move_doing()
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ sys.path.append(root_path)
|
||||
from src.common.database import db # noqa E402
|
||||
|
||||
# 加载根目录下的env.edv文件
|
||||
env_path = os.path.join(root_path, ".env.prod")
|
||||
env_path = os.path.join(root_path, ".env")
|
||||
if not os.path.exists(env_path):
|
||||
raise FileNotFoundError(f"配置文件不存在: {env_path}")
|
||||
load_dotenv(env_path)
|
||||
|
||||
Reference in New Issue
Block a user