Memify 与 Graph Enrichment
记忆化与图谱增强
相关源文件
本章引用的主要源码文件:
cognee/api/v1/datasets/datasets.pycognee/api/v1/delete/__init__.pycognee/api/v1/delete/routers/__init__.pycognee/api/v1/recall/recall.pycognee/api/v1/remember/remember.pycognee/api/v1/remember/routers/get_remember_router.pycognee/api/v1/serve/cloud_client.pycognee/cli/commands/recall_command.pycognee/cli/config.pycognee/infrastructure/databases/dataset_queue/__init__.pycognee/infrastructure/databases/dataset_queue/queue.pycognee/infrastructure/databases/hybrid/postgres/adapter.pycognee/infrastructure/databases/unified/get_unified_engine.pycognee/infrastructure/llm/prompts/chunk_association_system.txtcognee/infrastructure/llm/prompts/chunk_association_user.txtcognee/infrastructure/llm/prompts/consolidate_entity_details.txtcognee/memify_pipelines/consolidate_entity_descriptions.pycognee/memify_pipelines/memify_default_tasks.pycognee/modules/graph/methods/delete_dataset_nodes_and_edges.pycognee/modules/memify/memify.pycognee/shared/logging_utils.pycognee/tasks/chunks/__init__.pycognee/tasks/chunks/create_chunk_associations.pycognee/tasks/codingagents/coding_rule_associations.pycognee/tasks/graph/__init__.pycognee/tasks/memify/__init__.pycognee/tasks/memify/extract_subgraph.pycognee/tasks/memify/extract_subgraph_chunks.pycognee/tasks/memify/sync_graph_to_session.pycognee/tests/e2e/postgres/test_pghybrid.pycognee/tests/e2e/postgres/test_postgres_hybrid_adapter.pycognee/tests/test_chunk_associations.pycognee/tests/unit/api/v1/recall/__init__.pycognee/tests/unit/api/v1/recall/test_recall_api.pycognee/tests/unit/api/v1/test_public_memory_api.pycognee/tests/unit/infrastructure/databases/test_postgres_hybrid_batching.pycognee/tests/unit/infrastructure/dataset_queue/test_dataset_queue.pycognee/tests/unit/modules/memify_tasks/test_sync_graph_to_session.pyexamples/demos/custom_pipeline_single_object_example.pyexamples/demos/remember_recall_improve_example.py
memify 管线是 Cognee 的核心处理引擎,用于将现有的知识图谱或原始数据转换为增强后的智能记忆结构。与专注于初始图谱构建的标准 cognify 流程不同,memify 专为迭代增强、实体合并、编码规则提取以及基于会话的图谱更新而设计。
Memify 管线
memify 函数协调了一系列提取和增强任务。它可以对原始数据直接操作,也可以通过提取"记忆片段"(子图)作为进一步处理的输入,直接对现有知识图谱进行操作 cognee/modules/memify/memify.py:25-87。
数据流与执行
当 memify() 被调用且未指定具体数据时,它会使用 get_memory_fragment 检索一个子图 cognee/modules/memify/memify.py:100-106。该片段随后会通过一个包含两个主要阶段的管线:
- 提取任务:从输入中识别特定的模式、规则或实体
cognee/modules/memify/memify.py:90-91。 - 增强任务:创建语义连接、应用反馈或合并信息
cognee/modules/memify/memify.py:92-93。
管线通过 run_pipeline 执行,并借助 get_pipeline_executor 工具支持阻塞和后台两种执行模式 cognee/modules/memify/memify.py:114-128。
自然语言到代码实体的映射:Memify 编排
下图展示了高层级的 memify 请求如何映射到具体的代码组件和任务。
来源: cognee/modules/memify/memify.py:25-130,cognee/memify_pipelines/memify_default_tasks.py:1-20
编码规则提取
增强管线中的主要专用任务之一是 add_rule_associations。该任务允许 Cognee 从对话或文档中提取开发者指南或编码最佳实践,并将其链接到现有的代码实体。
规则提取工作流
- 上下文检索:使用
get_existing_rules从图谱中获取现有规则,以避免重复并为大语言模型(LLM)提供上下文cognee/tasks/codingagents/coding_rule_associations.py:36-51。 - 结构化提取:使用
LLMGateway.acreate_structured_output配合RuleSetPydantic 模型,从文本中解析新规则cognee/tasks/codingagents/coding_rule_associations.py:113-115。 - 图谱关联:新规则通过
rule_associated_from边链接回其源文档片段cognee/tasks/codingagents/coding_rule_associations.py:54-90。
规则的数据结构
| 类 | 描述 | 关键字段 |
|---|---|---|
Rule | 单个提取的指南。 | text,belongs_to_set cognee/tasks/codingagents/coding_rule_associations.py:19-25 |
RuleSet | 解析后的规则集合。 | rules: List[Rule] cognee/tasks/codingagents/coding_rule_associations.py:27-34 |
来源: cognee/tasks/codingagents/coding_rule_associations.py:19-35,cognee/tasks/codingagents/coding_rule_associations.py:93-139
实体合并
为维护图谱健康并减少冗余,Cognee 提供了合并实体的任务,这些实体代表相同的现实世界概念但具有不同的描述或属性。
consolidate_entity_descriptions
该任务专注于合并 Entity 类型节点的信息。
- 邻域获取:使用
get_entity_neighborhood检索实体节点及其直接邻居(边和相邻节点)cognee/memify_pipelines/consolidate_entity_descriptions.py:33-48。 - 大语言模型(LLM)精炼:对于每个实体,大语言模型(LLM)会分析现有的名称、描述和邻居上下文,以生成一个单一、高质量的合并描述
cognee/memify_pipelines/consolidate_entity_descriptions.py:190-196。 - 图谱更新:通过
add_data_points在图谱中更新精炼后的Entity数据点cognee/memify_pipelines/consolidate_entity_descriptions.py:15。
实现细节
- 文件:
cognee/memify_pipelines/consolidate_entity_descriptions.py - 关键函数:
get_entities_with_neighborhood(args)cognee/memify_pipelines/consolidate_entity_descriptions.py:116-125 - 提示模板:
consolidate_entity_details.txtcognee/memify_pipelines/consolidate_entity_descriptions.py:19
来源: cognee/memify_pipelines/consolidate_entity_descriptions.py:1-203
会话持久化与用户反馈
Cognee 在图谱增强过程中管理用户会话和反馈,使系统能够从交互中学习。
通过 sync_graph_to_session 实现会话持久化
Cognee 使用一个专用任务 sync_graph_to_session 来弥合持久化知识图谱与活跃用户会话之间的差距。该任务识别数据集中创建的新图谱边,并将其序列化到会话的上下文中 cognee/tasks/memify/sync_graph_to_session.py:17-18。
- 检查点:它使用基于缓存的检查点系统(
_checkpoint_key)来跟踪哪些边已经同步到会话中cognee/tasks/memify/sync_graph_to_session.py:14-15。 - 序列化:使用
_edge_to_text将边转换为文本表示,以便包含在会话上下文中cognee/tasks/memify/sync_graph_to_session.py:16。
Remember 和 Improve API
remember 和 improve API 端点提供了对这些增强能力的高层访问。
remember:接收数据并自动将其处理到知识图谱中。它还可以将类型化的记忆条目(如QAEntry或TraceEntry)直接存储到会话缓存中cognee/api/v1/remember/remember.py:165-172。improve:专门为给定数据集触发图谱增强管线,允许进行有针对性的优化和合并cognee/api/v1/serve/cloud_client.py:166-180。
自然语言到代码实体的映射:记忆持久化
来源: cognee/api/v1/remember/remember.py:125-162,cognee/api/v1/recall/recall.py:155-190,cognee/tasks/memify/sync_graph_to_session.py:17-103
语义片段关联
Cognee 对文档片段进行语义合并,以识别未明确陈述的关系。
create_chunk_associations
该任务识别语义相关的文档片段。
- 候选搜索:使用
vector_engine查找最相似的 $k$ 个片段cognee/tasks/chunks/create_chunk_associations.py:153-176。 - 大语言模型(LLM)验证:大语言模型(LLM)比较片段对,并生成一个包含
similarity_score和association_type(例如,主题相关、因果相关)的ChunkSimilarity对象cognee/tasks/chunks/create_chunk_associations.py:22-30。 - 边创建:如果分数超过阈值,则在图谱中创建一条
associated_with边cognee/tasks/chunks/create_chunk_associations.py:71-96。
来源: cognee/tasks/chunks/create_chunk_associations.py:1-180,cognee/tasks/chunks/__init__.py:1-17