agentic_huge_data_base / wiki
页面 Cognee · 3.6 Memify 与 Graph Enrichment·DeepWiki 中文全文译文

3.6 · Memify 与 Graph Enrichment(Memify and Graph Enrichment)

记忆管道与知识图谱构建 · 聚焦本章的模块关系、源码依据与实现要点。

项目Cognee 章节3.6 状态全文译文 模块工作流与编排、图谱与关系、测试、发布与运维、存储与持久化
源码线索
  • cognee/api/v1/datasets/datasets.py
  • cognee/api/v1/delete/__init__.py
  • cognee/api/v1/delete/routers/__init__.py
  • cognee/api/v1/recall/recall.py
  • cognee/api/v1/remember/remember.py
  • cognee/api/v1/remember/routers/get_remember_router.py
  • cognee/api/v1/serve/cloud_client.py
  • cognee/cli/commands/recall_command.py
  • cognee/cli/config.py
  • cognee/infrastructure/databases/dataset_queue/__init__.py
模块标签
  • 工作流与编排
  • 图谱与关系
  • 测试、发布与运维
  • 存储与持久化
  • 入库与解析

章节正文

Memify 与 Graph Enrichment

记忆化与图谱增强

相关源文件

本章引用的主要源码文件:

  • cognee/api/v1/datasets/datasets.py
  • cognee/api/v1/delete/__init__.py
  • cognee/api/v1/delete/routers/__init__.py
  • cognee/api/v1/recall/recall.py
  • cognee/api/v1/remember/remember.py
  • cognee/api/v1/remember/routers/get_remember_router.py
  • cognee/api/v1/serve/cloud_client.py
  • cognee/cli/commands/recall_command.py
  • cognee/cli/config.py
  • cognee/infrastructure/databases/dataset_queue/__init__.py
  • cognee/infrastructure/databases/dataset_queue/queue.py
  • cognee/infrastructure/databases/hybrid/postgres/adapter.py
  • cognee/infrastructure/databases/unified/get_unified_engine.py
  • cognee/infrastructure/llm/prompts/chunk_association_system.txt
  • cognee/infrastructure/llm/prompts/chunk_association_user.txt
  • cognee/infrastructure/llm/prompts/consolidate_entity_details.txt
  • cognee/memify_pipelines/consolidate_entity_descriptions.py
  • cognee/memify_pipelines/memify_default_tasks.py
  • cognee/modules/graph/methods/delete_dataset_nodes_and_edges.py
  • cognee/modules/memify/memify.py
  • cognee/shared/logging_utils.py
  • cognee/tasks/chunks/__init__.py
  • cognee/tasks/chunks/create_chunk_associations.py
  • cognee/tasks/codingagents/coding_rule_associations.py
  • cognee/tasks/graph/__init__.py
  • cognee/tasks/memify/__init__.py
  • cognee/tasks/memify/extract_subgraph.py
  • cognee/tasks/memify/extract_subgraph_chunks.py
  • cognee/tasks/memify/sync_graph_to_session.py
  • cognee/tests/e2e/postgres/test_pghybrid.py
  • cognee/tests/e2e/postgres/test_postgres_hybrid_adapter.py
  • cognee/tests/test_chunk_associations.py
  • cognee/tests/unit/api/v1/recall/__init__.py
  • cognee/tests/unit/api/v1/recall/test_recall_api.py
  • cognee/tests/unit/api/v1/test_public_memory_api.py
  • cognee/tests/unit/infrastructure/databases/test_postgres_hybrid_batching.py
  • cognee/tests/unit/infrastructure/dataset_queue/test_dataset_queue.py
  • cognee/tests/unit/modules/memify_tasks/test_sync_graph_to_session.py
  • examples/demos/custom_pipeline_single_object_example.py
  • examples/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。该片段随后会通过一个包含两个主要阶段的管线:

  1. 提取任务:从输入中识别特定的模式、规则或实体 cognee/modules/memify/memify.py:90-91
  2. 增强任务:创建语义连接、应用反馈或合并信息 cognee/modules/memify/memify.py:92-93

管线通过 run_pipeline 执行,并借助 get_pipeline_executor 工具支持阻塞和后台两种执行模式 cognee/modules/memify/memify.py:114-128

自然语言到代码实体的映射:Memify 编排

下图展示了高层级的 memify 请求如何映射到具体的代码组件和任务。

Cognee · 自然语言到代码实体的映射:Memify 编排 · 图 1
Cognee · 自然语言到代码实体的映射:Memify 编排 · 图 1

来源: cognee/modules/memify/memify.py:25-130cognee/memify_pipelines/memify_default_tasks.py:1-20

编码规则提取

增强管线中的主要专用任务之一是 add_rule_associations。该任务允许 Cognee 从对话或文档中提取开发者指南或编码最佳实践,并将其链接到现有的代码实体。

规则提取工作流
  1. 上下文检索:使用 get_existing_rules 从图谱中获取现有规则,以避免重复并为大语言模型(LLM)提供上下文 cognee/tasks/codingagents/coding_rule_associations.py:36-51
  2. 结构化提取:使用 LLMGateway.acreate_structured_output 配合 RuleSet Pydantic 模型,从文本中解析新规则 cognee/tasks/codingagents/coding_rule_associations.py:113-115
  3. 图谱关联:新规则通过 rule_associated_from 边链接回其源文档片段 cognee/tasks/codingagents/coding_rule_associations.py:54-90
规则的数据结构
描述关键字段
Rule单个提取的指南。textbelongs_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-35cognee/tasks/codingagents/coding_rule_associations.py:93-139

实体合并

为维护图谱健康并减少冗余,Cognee 提供了合并实体的任务,这些实体代表相同的现实世界概念但具有不同的描述或属性。

consolidate_entity_descriptions

该任务专注于合并 Entity 类型节点的信息。

  1. 邻域获取:使用 get_entity_neighborhood 检索实体节点及其直接邻居(边和相邻节点)cognee/memify_pipelines/consolidate_entity_descriptions.py:33-48
  2. 大语言模型(LLM)精炼:对于每个实体,大语言模型(LLM)会分析现有的名称、描述和邻居上下文,以生成一个单一、高质量的合并描述 cognee/memify_pipelines/consolidate_entity_descriptions.py:190-196
  3. 图谱更新:通过 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.txt cognee/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

rememberimprove API 端点提供了对这些增强能力的高层访问。

  • remember:接收数据并自动将其处理到知识图谱中。它还可以将类型化的记忆条目(如 QAEntryTraceEntry)直接存储到会话缓存中 cognee/api/v1/remember/remember.py:165-172
  • improve:专门为给定数据集触发图谱增强管线,允许进行有针对性的优化和合并 cognee/api/v1/serve/cloud_client.py:166-180
自然语言到代码实体的映射:记忆持久化
Cognee · 自然语言到代码实体的映射:记忆持久化 · 图 2
Cognee · 自然语言到代码实体的映射:记忆持久化 · 图 2

来源: cognee/api/v1/remember/remember.py:125-162cognee/api/v1/recall/recall.py:155-190cognee/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_scoreassociation_type(例如,主题相关、因果相关)的 ChunkSimilarity 对象 cognee/tasks/chunks/create_chunk_associations.py:22-30
  • 边创建:如果分数超过阈值,则在图谱中创建一条 associated_withcognee/tasks/chunks/create_chunk_associations.py:71-96

来源: cognee/tasks/chunks/create_chunk_associations.py:1-180cognee/tasks/chunks/__init__.py:1-17