记忆模块
联想记忆
定义生成式智能体的核心长期记忆模块
源代码相对路径:reverie\backend_server\persona\memory_structures\associative_memory.py
ConceptNode 概念节点
class ConceptNode:
"""
概念节点类 - 记忆流中的基本单元
每个 ConceptNode 代表智能体记忆中的一个概念,可以是:
- 事件(event): 观察到的行为或状态变化,深度=0
- 思考(thought): 基于事件的反思和抽象,深度≥1
- 对话(chat): 与其他智能体的交流记录,深度=0
"""
def __init__(self,
node_id, node_count, type_count, node_type, depth,
created, expiration,
s, p, o,
description, embedding_key, poignancy, keywords, filling):
# === 节点标识信息 ===
self.node_id = node_id # 唯一ID: "node_1", "node_2"...
self.node_count = node_count # 全局节点计数器
self.type_count = type_count # 同类型节点计数器
self.type = node_type # 节点类型: "event"/"thought"/"chat"
self.depth = depth # 抽象层级: 0=原始观察, 1+=反思层级
# === 时间属性 ===
self.created = created # 创建时间(datetime对象)
self.expiration = expiration # 过期时间(可选,用于临时记忆)
self.last_accessed = self.created # 最后访问时间(用于时近性计算)
# === SPO三元组结构 (主语-谓语-宾语) ===
self.subject = s # 主语: 谁/什么
self.predicate = p # 谓语: 做什么/是什么状态
self.object = o # 宾语: 对什么/在哪里
# === 内容和元数据 ===
self.description = description # 人类可读的描述文本
self.embedding_key = embedding_key # 向量嵌入的键名
self.poignancy = poignancy # 重要性/情感强度评分(1-10)
self.keywords = keywords # 关键词集合(用于快速检索)
self.filling = filling # 填充信息(用于思考节点的支撑证据)
def spo_summary(self):
"""
返回SPO三元组摘要
返回:
tuple: (主语, 谓语, 宾语) 的元组形式
示例:
("Isabella Rodriguez", "is", "sleeping")
("咖啡馆", "位于", "小镇中心")
"""
return (self.subject, self.predicate, self.object)
节点是这样的数据结构 如nodes.json
{
"node_87": {
"node_count": 87,
"type_count": 81,
"type": "event",
"depth": 0,
"created": "2023-02-13 09:00:20",
"expiration": null,
"subject": "the Ville:Oak Hill College:library:library table",
"predicate": "is",
"object": "use",
"description": "library table is in use",
"embedding_key": "library table is in use",
"poignancy": 3,
"keywords": [
"library table",
"use"
],
"filling": []
},
"node_86": {
"node_count": 86,
"type_count": 80,
"type": "event",
"depth": 0,
"created": "2023-02-13 09:00:10",
"expiration": null,
"subject": "Klaus Mueller",
"predicate": "research",
"object": "topic",
"description": "Klaus Mueller is researching the topic",
"embedding_key": "researching the topic",
"poignancy": 8,
"keywords": [
"topic",
"Klaus Mueller"
],
"filling": []
},
}