修复没移除干净

1
This commit is contained in:
SengokuCola
2025-03-07 13:30:50 +08:00
parent c11dd85790
commit 4a27446430
5 changed files with 49 additions and 44 deletions

View File

@@ -148,15 +148,6 @@ class BotConfig:
if "rerank" in model_config:
config.rerank = model_config["rerank"]
if 'topic' in toml_dict:
topic_config=toml_dict['topic']
if 'topic_extract' in topic_config:
config.topic_extract=topic_config.get('topic_extract',config.topic_extract)
logger.info(f"载入自定义主题提取为{config.topic_extract}")
if config.topic_extract=='llm' and 'llm_topic' in topic_config:
config.llm_topic_extract=topic_config['llm_topic']
logger.info(f"载入自定义主题提取模型为{config.llm_topic_extract['name']}")
# 消息配置
if "message" in toml_dict:
msg_config = toml_dict["message"]
@@ -190,13 +181,13 @@ class BotConfig:
bot_config_floder_path = BotConfig.get_config_dir()
print(f"正在品鉴配置文件目录: {bot_config_floder_path}")
bot_config_path = os.path.join(bot_config_floder_path, "bot_config_dev.toml")
if not os.path.exists(bot_config_path):
bot_config_path = os.path.join(bot_config_floder_path, "bot_config.toml")
if os.path.exists(bot_config_path):
# 如果开发环境配置文件不存在,则使用默认配置文件
bot_config_path = os.path.join(bot_config_floder_path, "bot_config.toml")
print(f"异常的新鲜,异常的美味: {bot_config_path}")
logger.info("使用bot配置文件")
else:
logger.info("已找到开发bot配置文件")
logger.info("没有找到美味")
global_config = BotConfig.load_config(config_path=bot_config_path)

View File

@@ -12,9 +12,7 @@ config = driver.config
class TopicIdentifier:
def __init__(self):
self.llm_client = LLM_request(model=global_config.llm_topic_extract)
self.select=global_config.topic_extract
async def identify_topic_llm(self, text: str) -> Optional[List[str]]:
"""识别消息主题,返回主题列表"""

View File

@@ -578,14 +578,11 @@ class Hippocampus:
async def memory_activate_value(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.3) -> int:
"""计算输入文本对记忆的激活程度"""
print(f"\033[1;32m[记忆激活]\033[0m 开始计算文本的记忆激活度: {text}")
print(f"\033[1;32m[记忆激活]\033[0m 识别主题: {await self._identify_topics(text)}")
# 识别主题
identified_topics = await self._identify_topics(text)
print(f"\033[1;32m[记忆激活]\033[0m 识别出的主题: {identified_topics}")
if not identified_topics:
# print(f"\033[1;32m[记忆激活]\033[0m 未识别出主题,返回0")
return 0
# 查找相似主题
@@ -596,7 +593,6 @@ class Hippocampus:
)
if not all_similar_topics:
print(f"\033[1;32m[记忆激活]\033[0m 未找到相似主题,返回0")
return 0
# 获取最相关的主题
@@ -605,19 +601,29 @@ class Hippocampus:
# 如果只找到一个主题,进行惩罚
if len(top_topics) == 1:
topic, score = top_topics[0]
activation = int(score * 50) # 单主题情况下,直接用相似度*50作为激活值
print(f"\033[1;32m[记忆激活]\033[0m 只找到一个主题,进行惩罚:")
print(f"\033[1;32m[记忆激活]\033[0m - 主题: {topic}")
print(f"\033[1;32m[记忆激活]\033[0m - 相似度: {score:.3f}")
print(f"\033[1;32m[记忆激活]\033[0m - 最终激活值: {activation}")
# 获取主题内容数量并计算惩罚系数
memory_items = self.memory_graph.G.nodes[topic].get('memory_items', [])
if not isinstance(memory_items, list):
memory_items = [memory_items] if memory_items else []
content_count = len(memory_items)
penalty = 1.0 / (1 + math.log(content_count + 1))
activation = int(score * 50 * penalty)
print(f"\033[1;32m[记忆激活]\033[0m 单主题「{topic}」- 相似度: {score:.3f}, 内容数: {content_count}, 激活值: {activation}")
return activation
# 计算关键词匹配率
# 计算关键词匹配率,同时考虑内容数量
matched_topics = set()
topic_similarities = {}
print(f"\033[1;32m[记忆激活]\033[0m 计算关键词匹配情况:")
for memory_topic, similarity in top_topics:
# 计算内容数量惩罚
memory_items = self.memory_graph.G.nodes[memory_topic].get('memory_items', [])
if not isinstance(memory_items, list):
memory_items = [memory_items] if memory_items else []
content_count = len(memory_items)
penalty = 1.0 / (1 + math.log(content_count + 1))
# 对每个记忆主题,检查它与哪些输入主题相似
for input_topic in identified_topics:
topic_vector = text_to_vector(input_topic)
@@ -628,27 +634,19 @@ class Hippocampus:
sim = cosine_similarity(v1, v2)
if sim >= similarity_threshold:
matched_topics.add(input_topic)
topic_similarities[input_topic] = max(topic_similarities.get(input_topic, 0), sim)
print(f"\033[1;32m[记忆激活]\033[0m - 输入主题「{input_topic}」匹配到记忆「{memory_topic}」, 相似度: {sim:.3f}")
adjusted_sim = sim * penalty
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})")
# 计算主题匹配率
# 计算主题匹配率和平均相似度
topic_match = len(matched_topics) / len(identified_topics)
print(f"\033[1;32m[记忆激活]\033[0m 主题匹配率:")
print(f"\033[1;32m[记忆激活]\033[0m - 匹配主题数: {len(matched_topics)}")
print(f"\033[1;32m[记忆激活]\033[0m - 总主题数: {len(identified_topics)}")
print(f"\033[1;32m[记忆激活]\033[0m - 匹配率: {topic_match:.3f}")
# 计算匹配主题的平均相似度
average_similarities = sum(topic_similarities.values()) / len(topic_similarities) if topic_similarities else 0
print(f"\033[1;32m[记忆激活]\033[0m 平均相似度:")
print(f"\033[1;32m[记忆激活]\033[0m - 各主题相似度: {[f'{k}:{v:.3f}' for k,v in topic_similarities.items()]}")
print(f"\033[1;32m[记忆激活]\033[0m - 平均相似度: {average_similarities:.3f}")
# 计算最终激活值
activation = (topic_match + average_similarities) / 2 * 100
print(f"\033[1;32m[记忆激活]\033[0m 最终激活值: {int(activation)}")
activation = int((topic_match + average_similarities) / 2 * 100)
print(f"\033[1;32m[记忆激活]\033[0m 匹配率: {topic_match:.3f}, 平均相似度: {average_similarities:.3f}, 激活值: {activation}")
return int(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:
"""根据输入文本获取相关的记忆内容"""