reformat: 格式化memory.py
This commit is contained in:
@@ -27,6 +27,7 @@ logger = log_module.setup_logger(LogClassification.MEMORY)
|
|||||||
|
|
||||||
logger.info("初始化记忆系统")
|
logger.info("初始化记忆系统")
|
||||||
|
|
||||||
|
|
||||||
class Memory_graph:
|
class Memory_graph:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.G = nx.Graph() # 使用 networkx 的图结构
|
self.G = nx.Graph() # 使用 networkx 的图结构
|
||||||
@@ -46,9 +47,9 @@ class Memory_graph:
|
|||||||
else:
|
else:
|
||||||
# 如果是新边,初始化 strength 为 1
|
# 如果是新边,初始化 strength 为 1
|
||||||
self.G.add_edge(concept1, concept2,
|
self.G.add_edge(concept1, concept2,
|
||||||
strength=1,
|
strength=1,
|
||||||
created_time=current_time, # 添加创建时间
|
created_time=current_time, # 添加创建时间
|
||||||
last_modified=current_time) # 添加最后修改时间
|
last_modified=current_time) # 添加最后修改时间
|
||||||
|
|
||||||
def add_dot(self, concept, memory):
|
def add_dot(self, concept, memory):
|
||||||
current_time = datetime.datetime.now().timestamp()
|
current_time = datetime.datetime.now().timestamp()
|
||||||
@@ -69,9 +70,9 @@ class Memory_graph:
|
|||||||
else:
|
else:
|
||||||
# 如果是新节点,创建新的记忆列表
|
# 如果是新节点,创建新的记忆列表
|
||||||
self.G.add_node(concept,
|
self.G.add_node(concept,
|
||||||
memory_items=[memory],
|
memory_items=[memory],
|
||||||
created_time=current_time, # 添加创建时间
|
created_time=current_time, # 添加创建时间
|
||||||
last_modified=current_time) # 添加最后修改时间
|
last_modified=current_time) # 添加最后修改时间
|
||||||
|
|
||||||
def get_dot(self, concept):
|
def get_dot(self, concept):
|
||||||
# 检查节点是否存在于图中
|
# 检查节点是否存在于图中
|
||||||
@@ -210,7 +211,8 @@ class Hippocampus:
|
|||||||
# 成功抽取短期消息样本
|
# 成功抽取短期消息样本
|
||||||
# 数据写回:增加记忆次数
|
# 数据写回:增加记忆次数
|
||||||
for message in messages:
|
for message in messages:
|
||||||
db.messages.update_one({"_id": message["_id"]}, {"$set": {"memorized_times": message["memorized_times"] + 1}})
|
db.messages.update_one({"_id": message["_id"]},
|
||||||
|
{"$set": {"memorized_times": message["memorized_times"] + 1}})
|
||||||
return messages
|
return messages
|
||||||
try_count += 1
|
try_count += 1
|
||||||
# 三次尝试均失败
|
# 三次尝试均失败
|
||||||
@@ -387,9 +389,9 @@ class Hippocampus:
|
|||||||
strength = int(similarity * 10)
|
strength = int(similarity * 10)
|
||||||
logger.info(f"连接相似节点: {topic} 和 {similar_topic} (强度: {strength})")
|
logger.info(f"连接相似节点: {topic} 和 {similar_topic} (强度: {strength})")
|
||||||
self.memory_graph.G.add_edge(topic, similar_topic,
|
self.memory_graph.G.add_edge(topic, similar_topic,
|
||||||
strength=strength,
|
strength=strength,
|
||||||
created_time=current_time,
|
created_time=current_time,
|
||||||
last_modified=current_time)
|
last_modified=current_time)
|
||||||
|
|
||||||
# 连接同批次的相关话题
|
# 连接同批次的相关话题
|
||||||
for i in range(len(all_topics)):
|
for i in range(len(all_topics)):
|
||||||
@@ -533,9 +535,9 @@ class Hippocampus:
|
|||||||
|
|
||||||
# 添加节点到图中
|
# 添加节点到图中
|
||||||
self.memory_graph.G.add_node(concept,
|
self.memory_graph.G.add_node(concept,
|
||||||
memory_items=memory_items,
|
memory_items=memory_items,
|
||||||
created_time=created_time,
|
created_time=created_time,
|
||||||
last_modified=last_modified)
|
last_modified=last_modified)
|
||||||
|
|
||||||
# 从数据库加载所有边
|
# 从数据库加载所有边
|
||||||
edges = list(db.graph_data.edges.find())
|
edges = list(db.graph_data.edges.find())
|
||||||
@@ -567,9 +569,9 @@ class Hippocampus:
|
|||||||
# 只有当源节点和目标节点都存在时才添加边
|
# 只有当源节点和目标节点都存在时才添加边
|
||||||
if source in self.memory_graph.G and target in self.memory_graph.G:
|
if source in self.memory_graph.G and target in self.memory_graph.G:
|
||||||
self.memory_graph.G.add_edge(source, target,
|
self.memory_graph.G.add_edge(source, target,
|
||||||
strength=strength,
|
strength=strength,
|
||||||
created_time=created_time,
|
created_time=created_time,
|
||||||
last_modified=last_modified)
|
last_modified=last_modified)
|
||||||
|
|
||||||
if need_update:
|
if need_update:
|
||||||
logger.success("[数据库] 已为缺失的时间字段进行补充")
|
logger.success("[数据库] 已为缺失的时间字段进行补充")
|
||||||
@@ -612,7 +614,7 @@ class Hippocampus:
|
|||||||
edge_data = self.memory_graph.G[source][target]
|
edge_data = self.memory_graph.G[source][target]
|
||||||
last_modified = edge_data.get('last_modified')
|
last_modified = edge_data.get('last_modified')
|
||||||
|
|
||||||
if current_time - last_modified > 3600*global_config.memory_forget_time:
|
if current_time - last_modified > 3600 * global_config.memory_forget_time:
|
||||||
current_strength = edge_data.get('strength', 1)
|
current_strength = edge_data.get('strength', 1)
|
||||||
new_strength = current_strength - 1
|
new_strength = current_strength - 1
|
||||||
|
|
||||||
@@ -632,7 +634,7 @@ class Hippocampus:
|
|||||||
node_data = self.memory_graph.G.nodes[node]
|
node_data = self.memory_graph.G.nodes[node]
|
||||||
last_modified = node_data.get('last_modified', current_time)
|
last_modified = node_data.get('last_modified', current_time)
|
||||||
|
|
||||||
if current_time - last_modified > 3600*24:
|
if current_time - last_modified > 3600 * 24:
|
||||||
memory_items = node_data.get('memory_items', [])
|
memory_items = node_data.get('memory_items', [])
|
||||||
if not isinstance(memory_items, list):
|
if not isinstance(memory_items, list):
|
||||||
memory_items = [memory_items] if memory_items else []
|
memory_items = [memory_items] if memory_items else []
|
||||||
@@ -943,6 +945,7 @@ def segment_text(text):
|
|||||||
seg_text = list(jieba.cut(text))
|
seg_text = list(jieba.cut(text))
|
||||||
return seg_text
|
return seg_text
|
||||||
|
|
||||||
|
|
||||||
driver = get_driver()
|
driver = get_driver()
|
||||||
config = driver.config
|
config = driver.config
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user