Complete memory system with causal graph, knowledge graph, auto-detection, and evolution features
完整记忆系统:双脑架构 + 因果图谱 + 知识图谱 + 自动检测 + 进化系统
完整的记忆管理系统,支持:
⚠️ 重要说明
此技能不包含任何预置的记忆数据。
安装后,用户将获得:
用户需要根据自己的需求添加记忆数据。
功能: 自动检测和存储记忆之间的因果关系
因果类型:
direct - 直接因果indirect - 间接因果conditional - 条件因果probabilistic - 概率因果核心功能:
使用示例:
from scripts.causal_knowledge_graphs import CausalGraph
causal_graph = CausalGraph("memory/database/xiaozhi_memory.db")
# 添加因果关系
causal_graph.add_causal_relation(
cause_id=1,
effect_id=2,
causal_type='direct',
strength=0.8,
confidence=0.9,
evidence='Test evidence'
)
# 获取原因
causes = causal_graph.get_causes(effect_id=2)
# 获取结果
effects = causal_graph.get_effects(cause_id=1)
# 获取因果链
chain = causal_graph.get_causal_chain(start_id=1, max_depth=5)
# 检测因果循环
cycles = causal_graph.detect_causal_cycles()
功能: 自动检测和存储知识点之间的各种关系
关系类型:
is_a - 是一种(继承)part_of - 是一部分(组成)related_to - 相关similar_to - 相似opposite_of - 相反depends_on - 依赖precedes - 先于follows - 跟随causes - 导致(因果关系)caused_by - 由...导致(因果关系)contains - 包含contained_in - 包含于exemplifies - 例证exemplified_by - 被例证context_for - 是...的上下文context_of - ...的上下文核心功能:
使用示例:
from scripts.causal_knowledge_graphs import KnowledgeGraph
knowledge_graph = KnowledgeGraph("memory/database/xiaozhi_memory.db")
# 添加关系
knowledge_graph.add_relation(
source_id=1,
target_id=2,
relation_type='related_to',
strength=0.7,
direction='bidirectional'
)
# 获取关系
relations = knowledge_graph.get_relations(memory_id=1)
# 获取相关记忆(多跳)
related = knowledge_graph.get_related_memories(memory_id=1, relation_type='related_to', max_depth=2)
# 查找最短路径
path = knowledge_graph.find_shortest_path(source_id=1, target_id=3)
# 检测社区
communities = knowledge_graph.detect_communities()
功能: 基于关键词、相似度、类别自动检测关系
检测方法:
使用示例:
from scripts.auto_relation_detector import AutoRelationManager
manager = AutoRelationManager("memory/database/xiaozhi_memory.db")
# 自动检测并添加关系
result = manager.auto_detect_and_add_relations(memory_id=1)
print(f"Found {len(result['causal_relations'])} causal relations")
print(f"Found {len(result['knowledge_relations'])} knowledge relations")
# 批量检测
results = manager.batch_detect_relations(limit=100)
功能: 基于llm-wiki最佳实践的记忆系统进化
核心改进:
使用示例:
from scripts.memory_system_v2 import MemorySystemV2
memory = MemorySystemV2()
memory.initialize()
# 保存记忆(自动触发两步摄入)
memory_id = memory.save(
type='learning',
title='New Learning',
content='This is a new learning',
category='knowledge'
)
# 四阶段检索
results = memory.search("python best practices")
# 获取统计信息
stats = memory.get_statistics()
安装后运行以下命令初始化数据库:
# 初始化数据库(v2.0)
python scripts/init_database_v2.py
# 或使用Python API
from scripts.memory_system_v2 import MemorySystemV2
memory = MemorySystemV2()
memory.initialize()
数据库文件将创建在:
memory/database/xiaozhi_memory.dbmemory/database/lancedb/安装后的目录结构:
memory-system-complete/
├── scripts/
│ ├── memory_system_v2.py # v2.0核心代码
│ ├── init_database_v2.py # v2.0数据库初始化
│ ├── verify_install_v2.py # v2.0安装验证
│ ├── causal_knowledge_graphs.py # 因果和知识图谱
│ └── auto_relation_detector.py # 自动关系检测
├── examples/
│ └── usage_demo_v2.py # v2.0使用示例
├── memory/
│ └── database/ # 数据库目录(空)
│ ├── xiaozhi_memory.db # 安装后创建
│ └── lancedb/ # 安装后创建
├── SKILL.md
└── README.md
python scripts/verify_install_v2.py
from scripts.memory_system_v2 import MemorySystemV2
# 初始化
memory = MemorySystemV2()
success = memory.initialize()
if success:
print("✅ Installation verified!")
# 保存测试记忆
test_id = memory.save(
type='test',
title='Installation Test v2.0',
content='Testing memory system v2.0 installation',
importance=5
)
# 查询测试
result = memory.get(test_id)
if result:
print("✅ Memory system v2.0 working!")
memory.delete(test_id) # 清理测试数据
else:
print("❌ Memory system v2.0 failed!")