Merge branch 'SengokuCola:debug' into debug
This commit is contained in:
@@ -26,7 +26,25 @@
|
|||||||
|
|
||||||
## 如果准备好了,就可以开始部署了
|
## 如果准备好了,就可以开始部署了
|
||||||
|
|
||||||
### 1️⃣ **我们需要创建一个Python环境来运行程序**
|
### 1️⃣ **首先,我们需要安装正确版本的Python**
|
||||||
|
|
||||||
|
在创建虚拟环境之前,请确保你的电脑上安装了Python 3.9及以上版本。如果没有,可以按以下步骤安装:
|
||||||
|
|
||||||
|
1. 访问Python官网下载页面:https://www.python.org/downloads/release/python-3913/
|
||||||
|
2. 下载Windows安装程序 (64-bit): `python-3.9.13-amd64.exe`
|
||||||
|
3. 运行安装程序,并确保勾选"Add Python 3.9 to PATH"选项
|
||||||
|
4. 点击"Install Now"开始安装
|
||||||
|
|
||||||
|
或者使用PowerShell自动下载安装(需要管理员权限):
|
||||||
|
```powershell
|
||||||
|
# 下载并安装Python 3.9.13
|
||||||
|
$pythonUrl = "https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe"
|
||||||
|
$pythonInstaller = "$env:TEMP\python-3.9.13-amd64.exe"
|
||||||
|
Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonInstaller
|
||||||
|
Start-Process -Wait -FilePath $pythonInstaller -ArgumentList "/quiet", "InstallAllUsers=0", "PrependPath=1" -Verb RunAs
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2️⃣ **创建Python虚拟环境来运行程序**
|
||||||
|
|
||||||
你可以选择使用以下两种方法之一来创建Python环境:
|
你可以选择使用以下两种方法之一来创建Python环境:
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
chcp 65001
|
chcp 65001
|
||||||
call conda activate niuniu
|
call conda activate maimbot
|
||||||
cd .
|
cd .
|
||||||
|
|
||||||
REM 执行nb run命令
|
REM 执行nb run命令
|
||||||
|
|||||||
@@ -148,15 +148,6 @@ class BotConfig:
|
|||||||
if "rerank" in model_config:
|
if "rerank" in model_config:
|
||||||
config.rerank = model_config["rerank"]
|
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:
|
if "message" in toml_dict:
|
||||||
msg_config = toml_dict["message"]
|
msg_config = toml_dict["message"]
|
||||||
@@ -190,13 +181,13 @@ class BotConfig:
|
|||||||
|
|
||||||
bot_config_floder_path = BotConfig.get_config_dir()
|
bot_config_floder_path = BotConfig.get_config_dir()
|
||||||
print(f"正在品鉴配置文件目录: {bot_config_floder_path}")
|
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")
|
bot_config_path = os.path.join(bot_config_floder_path, "bot_config.toml")
|
||||||
|
if os.path.exists(bot_config_path):
|
||||||
|
# 如果开发环境配置文件不存在,则使用默认配置文件
|
||||||
|
print(f"异常的新鲜,异常的美味: {bot_config_path}")
|
||||||
logger.info("使用bot配置文件")
|
logger.info("使用bot配置文件")
|
||||||
else:
|
else:
|
||||||
logger.info("已找到开发bot配置文件")
|
logger.info("没有找到美味")
|
||||||
|
|
||||||
global_config = BotConfig.load_config(config_path=bot_config_path)
|
global_config = BotConfig.load_config(config_path=bot_config_path)
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ config = driver.config
|
|||||||
class TopicIdentifier:
|
class TopicIdentifier:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.llm_client = LLM_request(model=global_config.llm_topic_extract)
|
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]]:
|
async def identify_topic_llm(self, text: str) -> Optional[List[str]]:
|
||||||
"""识别消息主题,返回主题列表"""
|
"""识别消息主题,返回主题列表"""
|
||||||
|
|||||||
@@ -578,14 +578,11 @@ class Hippocampus:
|
|||||||
|
|
||||||
async def memory_activate_value(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.3) -> int:
|
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)
|
identified_topics = await self._identify_topics(text)
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 识别出的主题: {identified_topics}")
|
|
||||||
|
|
||||||
if not identified_topics:
|
if not identified_topics:
|
||||||
# print(f"\033[1;32m[记忆激活]\033[0m 未识别出主题,返回0")
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# 查找相似主题
|
# 查找相似主题
|
||||||
@@ -596,7 +593,6 @@ class Hippocampus:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not all_similar_topics:
|
if not all_similar_topics:
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 未找到相似主题,返回0")
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# 获取最相关的主题
|
# 获取最相关的主题
|
||||||
@@ -605,19 +601,29 @@ class Hippocampus:
|
|||||||
# 如果只找到一个主题,进行惩罚
|
# 如果只找到一个主题,进行惩罚
|
||||||
if len(top_topics) == 1:
|
if len(top_topics) == 1:
|
||||||
topic, score = top_topics[0]
|
topic, score = top_topics[0]
|
||||||
activation = int(score * 50) # 单主题情况下,直接用相似度*50作为激活值
|
# 获取主题内容数量并计算惩罚系数
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 只找到一个主题,进行惩罚:")
|
memory_items = self.memory_graph.G.nodes[topic].get('memory_items', [])
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m - 主题: {topic}")
|
if not isinstance(memory_items, list):
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m - 相似度: {score:.3f}")
|
memory_items = [memory_items] if memory_items else []
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m - 最终激活值: {activation}")
|
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
|
return activation
|
||||||
|
|
||||||
# 计算关键词匹配率
|
# 计算关键词匹配率,同时考虑内容数量
|
||||||
matched_topics = set()
|
matched_topics = set()
|
||||||
topic_similarities = {}
|
topic_similarities = {}
|
||||||
|
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 计算关键词匹配情况:")
|
|
||||||
for memory_topic, similarity in top_topics:
|
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:
|
for input_topic in identified_topics:
|
||||||
topic_vector = text_to_vector(input_topic)
|
topic_vector = text_to_vector(input_topic)
|
||||||
@@ -628,27 +634,19 @@ class Hippocampus:
|
|||||||
sim = cosine_similarity(v1, v2)
|
sim = cosine_similarity(v1, v2)
|
||||||
if sim >= similarity_threshold:
|
if sim >= similarity_threshold:
|
||||||
matched_topics.add(input_topic)
|
matched_topics.add(input_topic)
|
||||||
topic_similarities[input_topic] = max(topic_similarities.get(input_topic, 0), sim)
|
adjusted_sim = sim * penalty
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m - 输入主题「{input_topic}」匹配到记忆「{memory_topic}」, 相似度: {sim:.3f}")
|
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)
|
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
|
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
|
activation = int((topic_match + average_similarities) / 2 * 100)
|
||||||
print(f"\033[1;32m[记忆激活]\033[0m 最终激活值: {int(activation)}")
|
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:
|
async def get_relevant_memories(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.4, max_memory_num: int = 5) -> list:
|
||||||
"""根据输入文本获取相关的记忆内容"""
|
"""根据输入文本获取相关的记忆内容"""
|
||||||
|
|||||||
Reference in New Issue
Block a user