agentic_huge_data_base / wiki
页面 Cognee · 12 示例与教程·DeepWiki 中文全文译文

12 · 示例与教程

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

项目Cognee 章节12 状态全文译文 模块工作流与编排、界面与交互、模型调用与提供方适配、安装与启动
源码线索
  • cognee/tests/unit/infrastructure/engine/test_identity_fields.py
  • examples/custom_pipelines/organizational_hierarchy/organizational_hierarchy_pipeline_example.py
  • examples/guides/consolidate_entity_descriptions_example.py
  • examples/guides/custom_data_models.py
  • examples/guides/custom_graph_model.py
  • examples/guides/custom_prompts.py
  • examples/guides/custom_tasks_and_pipelines.py
  • examples/guides/graph_visualization.py
  • examples/guides/importance_weight.py
  • examples/guides/improve_quickstart.py
模块标签
  • 工作流与编排
  • 界面与交互
  • 模型调用与提供方适配
  • 安装与启动
  • 测试、发布与运维

章节正文

示例与教程

示例与教程

相关源文件

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

  • cognee/tests/unit/infrastructure/engine/test_identity_fields.py
  • examples/custom_pipelines/organizational_hierarchy/organizational_hierarchy_pipeline_example.py
  • examples/guides/consolidate_entity_descriptions_example.py
  • examples/guides/custom_data_models.py
  • examples/guides/custom_graph_model.py
  • examples/guides/custom_prompts.py
  • examples/guides/custom_tasks_and_pipelines.py
  • examples/guides/graph_visualization.py
  • examples/guides/importance_weight.py
  • examples/guides/improve_quickstart.py
  • examples/guides/ontology_quickstart.py
  • notebooks/cognee_demo.ipynb
  • notebooks/cognee_multimedia_demo.ipynb
  • notebooks/cognee_simple_demo.ipynb
  • notebooks/data/alice_in_wonderland.txt
  • notebooks/data/enriched_medical_ontology_with_classes.owl
  • notebooks/data/scientific_papers/TOJ-22-0073_152Mendoza.pdf
  • notebooks/data/scientific_papers/nutrients-13-01241.pdf
  • notebooks/ontology_demo.ipynb

本页面提供了一系列实用示例和分步教程,演示 Cognee 的核心功能。它涵盖了基础的 addcognifysearch 工作流以及常见用例,帮助您快速将 Cognee 集成到应用程序中。

范围:本页面侧重于高层使用模式和常见场景。有关特定功能的详细技术实现,请参考各章节链接的子页面。

核心工作流总览

Cognee 处理管线由三个主要阶段组成,这些阶段实现为从主模块 cognee/__init__.py:18-28 导出的 Python 异步函数。这些函数管理从原始数据到结构化知识的转换过程。

工作流图:自然语言到代码实体空间

下图将高层系统操作映射到处理这些操作的具体代码实体。

Cognee · 工作流图:自然语言到代码实体空间 · 图 1
Cognee · 工作流图:自然语言到代码实体空间 · 图 1

来源cognee/__init__.py:18-28cognee/api/v1/add/add.py:18-215cognee/api/v1/cognify/cognify.py:41-246cognee/api/v1/search/search.py:23-112

基本使用示例

这些简单示例演示了使用 Cognee 默认配置处理数据和执行搜索所需的最小设置。涵盖以下内容:

  • 直接文本/列表输入:使用 cognee.remember() 立即入库并处理文本字符串 examples/guides/importance_weight.py:10-26
  • 文件处理:加载本地文件,如 alice_in_wonderland.txt notebooks/cognee_simple_demo.ipynb:59-61
  • 多媒体入库:通过文件路径处理 .mp3.png 文件 notebooks/cognee_multimedia_demo.ipynb:39-48

详情请参见基本使用示例

来源notebooks/cognee_simple_demo.ipynb:59-61examples/guides/importance_weight.py:10-26notebooks/cognee_multimedia_demo.ipynb:39-48

多数据库设置教程

Cognee 的架构与数据库无关。本教程指导您通过环境变量配置和切换不同的提供商。

  • 图数据库:通过 GRAPH_DATABASE_PROVIDERkuzuneo4jnetworkx 之间切换 notebooks/cognee_multimedia_demo.ipynb:75
  • 向量数据库:通过 VECTOR_DB_PROVIDER 配置 lancedbpgvectorqdrantweaviate notebooks/cognee_multimedia_demo.ipynb:82
  • 关系数据库:通过 DB_PROVIDERsqlitepostgres 之间选择 notebooks/cognee_multimedia_demo.ipynb:88

详情请参见多数据库设置教程

来源notebooks/cognee_multimedia_demo.ipynb:71-97

高级搜索模式

探索复杂的检索策略和智能体记忆模式:

  • 重要性权重:通过为入库信息分配权重来影响检索优先级 examples/guides/importance_weight.py:18-25
  • 自定义提示词:在 remember 过程中使用领域特定的提取规则 examples/guides/custom_prompts.py:5-22
  • 时间与图检索:使用 SearchType.GRAPH_COMPLETION 进行关系感知查询 examples/guides/custom_prompts.py:24-27

详情请参见高级搜索模式

来源examples/guides/importance_weight.py:15-26examples/guides/custom_prompts.py:5-27

图可视化

Cognee 提供了内置的可视化工具 visualize_graph,用于检查生成的知识图谱。本节说明如何生成交互式 HTML 报告,以便在描述合并等增强处理前后查看实体关系 examples/guides/consolidate_entity_descriptions_example.py:37-41

详情请参见图可视化

来源examples/guides/graph_visualization.py:4-19examples/guides/consolidate_entity_descriptions_example.py:37-41

自定义管线教程

学习如何通过创建自定义 Task 实现并使用 run_custom_pipeline 编排它们来扩展 Cognee。这允许进行领域特定的处理和自定义 DataPoint 模式。

自定义管线架构
Cognee · 自定义管线架构 · 图 2
Cognee · 自定义管线架构 · 图 2

详情请参见自定义管线教程

来源examples/guides/custom_tasks_and_pipelines.py:76-83examples/custom_pipelines/organizational_hierarchy/organizational_hierarchy_pipeline_example.py:119-124

MCP 客户端集成示例

Cognee 支持模型上下文协议(MCP),用于将知识图谱与 AI 智能体桥接。

  • 集成:将 Cognee 作为 Claude Desktop 或 Cursor 的记忆层连接。
  • 工具:将 cognifysearchsave_interaction 暴露为工具,供智能体动态使用。

详情请参见MCP 客户端集成示例

评估框架

Cognee 包含用于评估知识引擎性能的工具。

  • 结构化输出:使用 LLMGateway.acreate_structured_output 根据 Pydantic 模型验证提取准确性 examples/guides/custom_tasks_and_pipelines.py:57-59
  • 对比分析:测试有无本体指导下的检索效果 notebooks/ontology_demo.ipynb:141-150

详情请参见评估框架

来源examples/guides/custom_tasks_and_pipelines.py:57-59notebooks/ontology_demo.ipynb:141-150

本体集成

Cognee 支持 OWL 本体,用于指导实体提取和关系映射。

  • 本体加载:使用 RDFLibOntologyResolver 加载 .owl 文件 examples/guides/ontology_quickstart.py:21
  • 配置管线:将本体配置传递给 cognee.remembercognee.cognify examples/guides/ontology_quickstart.py:19-25

详情请参见本体集成

来源examples/guides/ontology_quickstart.py:5-25notebooks/ontology_demo.ipynb:138-140