改各种小问题

This commit is contained in:
春河晴
2025-04-16 17:37:28 +09:00
parent a0b1b1f8d8
commit dc2cf843e5
36 changed files with 114 additions and 107 deletions

View File

@@ -9,7 +9,7 @@ import networkx as nx
import numpy as np
from collections import Counter
from ...common.database import db
from ...plugins.models.utils_model import LLM_request
from ...plugins.models.utils_model import LLMRequest
from src.common.logger import get_module_logger, LogConfig, MEMORY_STYLE_CONFIG
from src.plugins.memory_system.sample_distribution import MemoryBuildScheduler # 分布生成器
from .memory_config import MemoryConfig
@@ -91,7 +91,7 @@ memory_config = LogConfig(
logger = get_module_logger("memory_system", config=memory_config)
class Memory_graph:
class MemoryGraph:
def __init__(self):
self.G = nx.Graph() # 使用 networkx 的图结构
@@ -229,7 +229,7 @@ class Memory_graph:
# 海马体
class Hippocampus:
def __init__(self):
self.memory_graph = Memory_graph()
self.memory_graph = MemoryGraph()
self.llm_topic_judge = None
self.llm_summary_by_topic = None
self.entorhinal_cortex = None
@@ -243,8 +243,8 @@ class Hippocampus:
self.parahippocampal_gyrus = ParahippocampalGyrus(self)
# 从数据库加载记忆图
self.entorhinal_cortex.sync_memory_from_db()
self.llm_topic_judge = LLM_request(self.config.llm_topic_judge, request_type="memory")
self.llm_summary_by_topic = LLM_request(self.config.llm_summary_by_topic, request_type="memory")
self.llm_topic_judge = LLMRequest(self.config.llm_topic_judge, request_type="memory")
self.llm_summary_by_topic = LLMRequest(self.config.llm_summary_by_topic, request_type="memory")
def get_all_node_names(self) -> list:
"""获取记忆图中所有节点的名字列表"""
@@ -346,7 +346,8 @@ class Hippocampus:
Args:
text (str): 输入文本
num (int, optional): 需要返回的记忆数量。默认为5
max_memory_num (int, optional): 记忆数量限制。默认为3
max_memory_length (int, optional): 记忆长度限制。默认为2。
max_depth (int, optional): 记忆检索深度。默认为2。
fast_retrieval (bool, optional): 是否使用快速检索。默认为False。
如果为True使用jieba分词和TF-IDF提取关键词速度更快但可能不够准确。
@@ -540,7 +541,6 @@ class Hippocampus:
Args:
text (str): 输入文本
num (int, optional): 需要返回的记忆数量。默认为5。
max_depth (int, optional): 记忆检索深度。默认为2。
fast_retrieval (bool, optional): 是否使用快速检索。默认为False。
如果为True使用jieba分词和TF-IDF提取关键词速度更快但可能不够准确。
@@ -937,7 +937,7 @@ class EntorhinalCortex:
# 海马体
class Hippocampus:
def __init__(self):
self.memory_graph = Memory_graph()
self.memory_graph = MemoryGraph()
self.llm_topic_judge = None
self.llm_summary_by_topic = None
self.entorhinal_cortex = None
@@ -951,8 +951,8 @@ class Hippocampus:
self.parahippocampal_gyrus = ParahippocampalGyrus(self)
# 从数据库加载记忆图
self.entorhinal_cortex.sync_memory_from_db()
self.llm_topic_judge = LLM_request(self.config.llm_topic_judge, request_type="memory")
self.llm_summary_by_topic = LLM_request(self.config.llm_summary_by_topic, request_type="memory")
self.llm_topic_judge = LLMRequest(self.config.llm_topic_judge, request_type="memory")
self.llm_summary_by_topic = LLMRequest(self.config.llm_summary_by_topic, request_type="memory")
def get_all_node_names(self) -> list:
"""获取记忆图中所有节点的名字列表"""
@@ -1054,8 +1054,9 @@ class Hippocampus:
Args:
text (str): 输入文本
num (int, optional): 需要返回的记忆数量。默认为5
max_depth (int, optional): 记忆检索深度。默认为2
max_memory_num (int, optional): 返回的记忆条目数量上限。默认为3表示最多返回3条与输入文本相关度最高的记忆
max_memory_length (int, optional): 每个主题最多返回的记忆条目数量。默认为2表示每个主题最多返回2条相似度最高的记忆。
max_depth (int, optional): 记忆检索深度。默认为3。值越大检索范围越广可以获取更多间接相关的记忆但速度会变慢。
fast_retrieval (bool, optional): 是否使用快速检索。默认为False。
如果为True使用jieba分词和TF-IDF提取关键词速度更快但可能不够准确。
如果为False使用LLM提取关键词速度较慢但更准确。
@@ -1248,7 +1249,6 @@ class Hippocampus:
Args:
text (str): 输入文本
num (int, optional): 需要返回的记忆数量。默认为5。
max_depth (int, optional): 记忆检索深度。默认为2。
fast_retrieval (bool, optional): 是否使用快速检索。默认为False。
如果为True使用jieba分词和TF-IDF提取关键词速度更快但可能不够准确。

View File

@@ -177,7 +177,7 @@ def remove_mem_edge(hippocampus: Hippocampus):
# 修改节点信息
def alter_mem_node(hippocampus: Hippocampus):
batchEnviroment = dict()
batch_environment = dict()
while True:
concept = input("请输入节点概念名(输入'终止'以结束):\n")
if concept.lower() == "终止":
@@ -229,7 +229,7 @@ def alter_mem_node(hippocampus: Hippocampus):
break
try:
user_exec(command, node_environment, batchEnviroment)
user_exec(command, node_environment, batch_environment)
except Exception as e:
console.print(e)
console.print(
@@ -239,7 +239,7 @@ def alter_mem_node(hippocampus: Hippocampus):
# 修改边信息
def alter_mem_edge(hippocampus: Hippocampus):
batchEnviroment = dict()
batch_enviroment = dict()
while True:
source = input("请输入 **第一个节点** 名称(输入'终止'以结束):\n")
if source.lower() == "终止":
@@ -262,21 +262,21 @@ def alter_mem_edge(hippocampus: Hippocampus):
console.print("[yellow]你将获得一个执行任意代码的环境[/yellow]")
console.print("[red]你已经被警告过了。[/red]\n")
edgeEnviroment = {"source": "<节点名>", "target": "<节点名>", "strength": "<强度值,装在一个list里>"}
edge_environment = {"source": "<节点名>", "target": "<节点名>", "strength": "<强度值,装在一个list里>"}
console.print(
"[green]环境变量中会有env与batchEnv两个dict, env在切换节点时会清空, batchEnv在操作终止时才会清空[/green]"
)
console.print(
f"[green] env 会被初始化为[/green]\n{edgeEnviroment}\n[green]且会在用户代码执行完毕后被提交 [/green]"
f"[green] env 会被初始化为[/green]\n{edge_environment}\n[green]且会在用户代码执行完毕后被提交 [/green]"
)
console.print(
"[yellow]为便于书写临时脚本请手动在输入代码通过Ctrl+C等方式触发KeyboardInterrupt来结束代码执行[/yellow]"
)
# 拷贝数据以防操作炸了
edgeEnviroment["strength"] = [edge["strength"]]
edgeEnviroment["source"] = source
edgeEnviroment["target"] = target
edge_environment["strength"] = [edge["strength"]]
edge_environment["source"] = source
edge_environment["target"] = target
while True:
@@ -288,8 +288,8 @@ def alter_mem_edge(hippocampus: Hippocampus):
except KeyboardInterrupt:
# 稍微防一下小天才
try:
if isinstance(edgeEnviroment["strength"][0], int):
edge["strength"] = edgeEnviroment["strength"][0]
if isinstance(edge_environment["strength"][0], int):
edge["strength"] = edge_environment["strength"][0]
else:
raise Exception
@@ -301,7 +301,7 @@ def alter_mem_edge(hippocampus: Hippocampus):
break
try:
user_exec(command, edgeEnviroment, batchEnviroment)
user_exec(command, edge_environment, batch_enviroment)
except Exception as e:
console.print(e)
console.print(

View File

@@ -10,7 +10,7 @@ from src.common.logger import get_module_logger
logger = get_module_logger("offline_llm")
class LLM_request_off:
class LLMRequestOff:
def __init__(self, model_name="deepseek-ai/DeepSeek-V3", **kwargs):
self.model_name = model_name
self.params = kwargs