fix: logger初始化顺序
This commit is contained in:
1
bot.py
1
bot.py
@@ -149,6 +149,7 @@ if __name__ == "__main__":
|
|||||||
init_config()
|
init_config()
|
||||||
init_env()
|
init_env()
|
||||||
load_env()
|
load_env()
|
||||||
|
load_logger()
|
||||||
|
|
||||||
env_config = {key: os.getenv(key) for key in os.environ}
|
env_config = {key: os.getenv(key) for key in os.environ}
|
||||||
scan_provider(env_config)
|
scan_provider(env_config)
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ class BotConfig:
|
|||||||
personality_config = parent['personality']
|
personality_config = parent['personality']
|
||||||
personality = personality_config.get('prompt_personality')
|
personality = personality_config.get('prompt_personality')
|
||||||
if len(personality) >= 2:
|
if len(personality) >= 2:
|
||||||
logger.info(f"载入自定义人格:{personality}")
|
logger.debug(f"载入自定义人格:{personality}")
|
||||||
config.PROMPT_PERSONALITY = personality_config.get('prompt_personality', config.PROMPT_PERSONALITY)
|
config.PROMPT_PERSONALITY = personality_config.get('prompt_personality', config.PROMPT_PERSONALITY)
|
||||||
logger.info(f"载入自定义日程prompt:{personality_config.get('prompt_schedule', config.PROMPT_SCHEDULE_GEN)}")
|
logger.info(f"载入自定义日程prompt:{personality_config.get('prompt_schedule', config.PROMPT_SCHEDULE_GEN)}")
|
||||||
config.PROMPT_SCHEDULE_GEN = personality_config.get('prompt_schedule', config.PROMPT_SCHEDULE_GEN)
|
config.PROMPT_SCHEDULE_GEN = personality_config.get('prompt_schedule', config.PROMPT_SCHEDULE_GEN)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import time
|
|||||||
import jieba
|
import jieba
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
from ...common.database import Database # 使用正确的导入语法
|
from ...common.database import Database # 使用正确的导入语法
|
||||||
from ..chat.config import global_config
|
from ..chat.config import global_config
|
||||||
from ..chat.utils import (
|
from ..chat.utils import (
|
||||||
@@ -131,10 +132,10 @@ class Memory_graph:
|
|||||||
|
|
||||||
# 海马体
|
# 海马体
|
||||||
class Hippocampus:
|
class Hippocampus:
|
||||||
def __init__(self,memory_graph:Memory_graph):
|
def __init__(self, memory_graph: Memory_graph):
|
||||||
self.memory_graph = memory_graph
|
self.memory_graph = memory_graph
|
||||||
self.llm_topic_judge = LLM_request(model = global_config.llm_topic_judge,temperature=0.5)
|
self.llm_topic_judge = LLM_request(model=global_config.llm_topic_judge, temperature=0.5)
|
||||||
self.llm_summary_by_topic = LLM_request(model = global_config.llm_summary_by_topic,temperature=0.5)
|
self.llm_summary_by_topic = LLM_request(model=global_config.llm_summary_by_topic, temperature=0.5)
|
||||||
|
|
||||||
def get_all_node_names(self) -> list:
|
def get_all_node_names(self) -> list:
|
||||||
"""获取记忆图中所有节点的名字列表
|
"""获取记忆图中所有节点的名字列表
|
||||||
@@ -157,7 +158,7 @@ class Hippocampus:
|
|||||||
nodes = sorted([source, target])
|
nodes = sorted([source, target])
|
||||||
return hash(f"{nodes[0]}:{nodes[1]}")
|
return hash(f"{nodes[0]}:{nodes[1]}")
|
||||||
|
|
||||||
def get_memory_sample(self, chat_size=20, time_frequency:dict={'near':2,'mid':4,'far':3}):
|
def get_memory_sample(self, chat_size=20, time_frequency: dict = {'near': 2, 'mid': 4, 'far': 3}):
|
||||||
"""获取记忆样本
|
"""获取记忆样本
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -174,13 +175,13 @@ class Hippocampus:
|
|||||||
chat_samples.append(messages)
|
chat_samples.append(messages)
|
||||||
|
|
||||||
for _ in range(time_frequency.get('mid')):
|
for _ in range(time_frequency.get('mid')):
|
||||||
random_time = current_timestamp - random.randint(3600, 3600*4)
|
random_time = current_timestamp - random.randint(3600, 3600 * 4)
|
||||||
messages = get_cloest_chat_from_db(db=self.memory_graph.db, length=chat_size, timestamp=random_time)
|
messages = get_cloest_chat_from_db(db=self.memory_graph.db, length=chat_size, timestamp=random_time)
|
||||||
if messages:
|
if messages:
|
||||||
chat_samples.append(messages)
|
chat_samples.append(messages)
|
||||||
|
|
||||||
for _ in range(time_frequency.get('far')):
|
for _ in range(time_frequency.get('far')):
|
||||||
random_time = current_timestamp - random.randint(3600*4, 3600*24)
|
random_time = current_timestamp - random.randint(3600 * 4, 3600 * 24)
|
||||||
messages = get_cloest_chat_from_db(db=self.memory_graph.db, length=chat_size, timestamp=random_time)
|
messages = get_cloest_chat_from_db(db=self.memory_graph.db, length=chat_size, timestamp=random_time)
|
||||||
if messages:
|
if messages:
|
||||||
chat_samples.append(messages)
|
chat_samples.append(messages)
|
||||||
@@ -230,7 +231,8 @@ class Hippocampus:
|
|||||||
|
|
||||||
# 过滤topics
|
# 过滤topics
|
||||||
filter_keywords = global_config.memory_ban_words
|
filter_keywords = global_config.memory_ban_words
|
||||||
topics = [topic.strip() for topic in topics_response[0].replace(",", ",").replace("、", ",").replace(" ", ",").split(",") if topic.strip()]
|
topics = [topic.strip() for topic in
|
||||||
|
topics_response[0].replace(",", ",").replace("、", ",").replace(" ", ",").split(",") if topic.strip()]
|
||||||
filtered_topics = [topic for topic in topics if not any(keyword in topic for keyword in filter_keywords)]
|
filtered_topics = [topic for topic in topics if not any(keyword in topic for keyword in filter_keywords)]
|
||||||
|
|
||||||
print(f"过滤后话题: {filtered_topics}")
|
print(f"过滤后话题: {filtered_topics}")
|
||||||
@@ -251,19 +253,20 @@ class Hippocampus:
|
|||||||
|
|
||||||
return compressed_memory
|
return compressed_memory
|
||||||
|
|
||||||
def calculate_topic_num(self,text, compress_rate):
|
def calculate_topic_num(self, text, compress_rate):
|
||||||
"""计算文本的话题数量"""
|
"""计算文本的话题数量"""
|
||||||
information_content = calculate_information_content(text)
|
information_content = calculate_information_content(text)
|
||||||
topic_by_length = text.count('\n')*compress_rate
|
topic_by_length = text.count('\n') * compress_rate
|
||||||
topic_by_information_content = max(1, min(5, int((information_content-3) * 2)))
|
topic_by_information_content = max(1, min(5, int((information_content - 3) * 2)))
|
||||||
topic_num = int((topic_by_length + topic_by_information_content)/2)
|
topic_num = int((topic_by_length + topic_by_information_content) / 2)
|
||||||
print(f"topic_by_length: {topic_by_length}, topic_by_information_content: {topic_by_information_content}, topic_num: {topic_num}")
|
print(
|
||||||
|
f"topic_by_length: {topic_by_length}, topic_by_information_content: {topic_by_information_content}, topic_num: {topic_num}")
|
||||||
return topic_num
|
return topic_num
|
||||||
|
|
||||||
async def operation_build_memory(self,chat_size=20):
|
async def operation_build_memory(self, chat_size=20):
|
||||||
# 最近消息获取频率
|
# 最近消息获取频率
|
||||||
time_frequency = {'near':2,'mid':4,'far':2}
|
time_frequency = {'near': 2, 'mid': 4, 'far': 2}
|
||||||
memory_sample = self.get_memory_sample(chat_size,time_frequency)
|
memory_sample = self.get_memory_sample(chat_size, time_frequency)
|
||||||
|
|
||||||
for i, input_text in enumerate(memory_sample, 1):
|
for i, input_text in enumerate(memory_sample, 1):
|
||||||
# 加载进度可视化
|
# 加载进度可视化
|
||||||
@@ -532,11 +535,11 @@ class Hippocampus:
|
|||||||
else:
|
else:
|
||||||
print("\n本次检查没有需要合并的节点")
|
print("\n本次检查没有需要合并的节点")
|
||||||
|
|
||||||
def find_topic_llm(self,text, topic_num):
|
def find_topic_llm(self, text, topic_num):
|
||||||
prompt = f'这是一段文字:{text}。请你从这段话中总结出{topic_num}个关键的概念,可以是名词,动词,或者特定人物,帮我列出来,用逗号,隔开,尽可能精简。只需要列举{topic_num}个话题就好,不要有序号,不要告诉我其他内容。'
|
prompt = f'这是一段文字:{text}。请你从这段话中总结出{topic_num}个关键的概念,可以是名词,动词,或者特定人物,帮我列出来,用逗号,隔开,尽可能精简。只需要列举{topic_num}个话题就好,不要有序号,不要告诉我其他内容。'
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
def topic_what(self,text, topic, time_info):
|
def topic_what(self, text, topic, time_info):
|
||||||
prompt = f'这是一段文字,{time_info}:{text}。我想让你基于这段文字来概括"{topic}"这个概念,帮我总结成一句自然的话,可以包含时间和人物,以及具体的观点。只输出这句话就好'
|
prompt = f'这是一段文字,{time_info}:{text}。我想让你基于这段文字来概括"{topic}"这个概念,帮我总结成一句自然的话,可以包含时间和人物,以及具体的观点。只输出这句话就好'
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
@@ -551,7 +554,8 @@ class Hippocampus:
|
|||||||
"""
|
"""
|
||||||
topics_response = await self.llm_topic_judge.generate_response(self.find_topic_llm(text, 5))
|
topics_response = await self.llm_topic_judge.generate_response(self.find_topic_llm(text, 5))
|
||||||
# print(f"话题: {topics_response[0]}")
|
# print(f"话题: {topics_response[0]}")
|
||||||
topics = [topic.strip() for topic in topics_response[0].replace(",", ",").replace("、", ",").replace(" ", ",").split(",") if topic.strip()]
|
topics = [topic.strip() for topic in
|
||||||
|
topics_response[0].replace(",", ",").replace("、", ",").replace(" ", ",").split(",") if topic.strip()]
|
||||||
# print(f"话题: {topics}")
|
# print(f"话题: {topics}")
|
||||||
|
|
||||||
return topics
|
return topics
|
||||||
@@ -655,7 +659,8 @@ class Hippocampus:
|
|||||||
penalty = 1.0 / (1 + math.log(content_count + 1))
|
penalty = 1.0 / (1 + math.log(content_count + 1))
|
||||||
|
|
||||||
activation = int(score * 50 * penalty)
|
activation = int(score * 50 * penalty)
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 单主题「{topic}」- 相似度: {score:.3f}, 内容数: {content_count}, 激活值: {activation}")
|
print(
|
||||||
|
f"\033[1;32m[记忆激活]\033[0m 单主题「{topic}」- 相似度: {score:.3f}, 内容数: {content_count}, 激活值: {activation}")
|
||||||
return activation
|
return activation
|
||||||
|
|
||||||
# 计算关键词匹配率,同时考虑内容数量
|
# 计算关键词匹配率,同时考虑内容数量
|
||||||
@@ -682,7 +687,8 @@ class Hippocampus:
|
|||||||
matched_topics.add(input_topic)
|
matched_topics.add(input_topic)
|
||||||
adjusted_sim = sim * penalty
|
adjusted_sim = sim * penalty
|
||||||
topic_similarities[input_topic] = max(topic_similarities.get(input_topic, 0), adjusted_sim)
|
topic_similarities[input_topic] = max(topic_similarities.get(input_topic, 0), adjusted_sim)
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 主题「{input_topic}」-> 「{memory_topic}」(内容数: {content_count}, 相似度: {adjusted_sim:.3f})")
|
print(
|
||||||
|
f"\033[1;32m[记忆激活]\033[0m 主题「{input_topic}」-> 「{memory_topic}」(内容数: {content_count}, 相似度: {adjusted_sim:.3f})")
|
||||||
|
|
||||||
# 计算主题匹配率和平均相似度
|
# 计算主题匹配率和平均相似度
|
||||||
topic_match = len(matched_topics) / len(identified_topics)
|
topic_match = len(matched_topics) / len(identified_topics)
|
||||||
@@ -690,11 +696,13 @@ class Hippocampus:
|
|||||||
|
|
||||||
# 计算最终激活值
|
# 计算最终激活值
|
||||||
activation = int((topic_match + average_similarities) / 2 * 100)
|
activation = int((topic_match + average_similarities) / 2 * 100)
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 匹配率: {topic_match:.3f}, 平均相似度: {average_similarities:.3f}, 激活值: {activation}")
|
print(
|
||||||
|
f"\033[1;32m[记忆激活]\033[0m 匹配率: {topic_match:.3f}, 平均相似度: {average_similarities:.3f}, 激活值: {activation}")
|
||||||
|
|
||||||
return activation
|
return activation
|
||||||
|
|
||||||
async def get_relevant_memories(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.4, max_memory_num: int = 5) -> list:
|
async def get_relevant_memories(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.4,
|
||||||
|
max_memory_num: int = 5) -> list:
|
||||||
"""根据输入文本获取相关的记忆内容"""
|
"""根据输入文本获取相关的记忆内容"""
|
||||||
# 识别主题
|
# 识别主题
|
||||||
identified_topics = await self._identify_topics(text)
|
identified_topics = await self._identify_topics(text)
|
||||||
@@ -716,8 +724,8 @@ class Hippocampus:
|
|||||||
first_layer, _ = self.memory_graph.get_related_item(topic, depth=1)
|
first_layer, _ = self.memory_graph.get_related_item(topic, depth=1)
|
||||||
if first_layer:
|
if first_layer:
|
||||||
# 如果记忆条数超过限制,随机选择指定数量的记忆
|
# 如果记忆条数超过限制,随机选择指定数量的记忆
|
||||||
if len(first_layer) > max_memory_num/2:
|
if len(first_layer) > max_memory_num / 2:
|
||||||
first_layer = random.sample(first_layer, max_memory_num//2)
|
first_layer = random.sample(first_layer, max_memory_num // 2)
|
||||||
# 为每条记忆添加来源主题和相似度信息
|
# 为每条记忆添加来源主题和相似度信息
|
||||||
for memory in first_layer:
|
for memory in first_layer:
|
||||||
relevant_memories.append({
|
relevant_memories.append({
|
||||||
@@ -749,19 +757,19 @@ config = driver.config
|
|||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
Database.initialize(
|
Database.initialize(
|
||||||
host= config.MONGODB_HOST,
|
host=config.MONGODB_HOST,
|
||||||
port= config.MONGODB_PORT,
|
port=config.MONGODB_PORT,
|
||||||
db_name= config.DATABASE_NAME,
|
db_name=config.DATABASE_NAME,
|
||||||
username= config.MONGODB_USERNAME,
|
username=config.MONGODB_USERNAME,
|
||||||
password= config.MONGODB_PASSWORD,
|
password=config.MONGODB_PASSWORD,
|
||||||
auth_source=config.MONGODB_AUTH_SOURCE
|
auth_source=config.MONGODB_AUTH_SOURCE
|
||||||
)
|
)
|
||||||
#创建记忆图
|
# 创建记忆图
|
||||||
memory_graph = Memory_graph()
|
memory_graph = Memory_graph()
|
||||||
#创建海马体
|
# 创建海马体
|
||||||
hippocampus = Hippocampus(memory_graph)
|
hippocampus = Hippocampus(memory_graph)
|
||||||
#从数据库加载记忆图
|
# 从数据库加载记忆图
|
||||||
hippocampus.sync_memory_from_db()
|
hippocampus.sync_memory_from_db()
|
||||||
|
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
print(f"\033[32m[加载海马体耗时: {end_time - start_time:.2f} 秒]\033[0m")
|
logger.success(f"加载海马体耗时: {end_time - start_time:.2f} 秒")
|
||||||
|
|||||||
Reference in New Issue
Block a user