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 (
|
||||||
@@ -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}")
|
||||||
@@ -257,7 +259,8 @@ class Hippocampus:
|
|||||||
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):
|
||||||
@@ -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)
|
||||||
@@ -764,4 +772,4 @@ 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