agentic_huge_data_base / wiki
页面 Cognee · 8 数据模型与模式定义·DeepWiki 中文全文译文

8 · 数据模型与模式定义(Data Models and Schemas)

记忆管道与知识图谱构建 · 本章是 Cognee DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Cognee 章节8 状态全文译文 模块图谱与关系、文档对象与元数据、测试、发布与运维、检索、召回与索引
源码线索
  • cognee/infrastructure/engine/models/DataPoint.py
  • cognee/modules/chunking/models/DocumentChunk.py
  • cognee/modules/engine/models/Entity.py
  • cognee/modules/engine/models/EntityType.py
  • cognee/modules/graph/utils/get_graph_from_model.py
  • cognee/modules/graph/utils/get_model_instance_from_graph.py
  • cognee/modules/pipelines/operations/run_tasks_base.py
  • cognee/modules/pipelines/operations/run_tasks_with_telemetry.py
  • cognee/modules/visualization/cognee_network_visualization.py
  • cognee/shared/CodeGraphEntities.py
模块标签
  • 图谱与关系
  • 文档对象与元数据
  • 测试、发布与运维
  • 检索、召回与索引
  • 入库与解析

中文译文

数据模型与模式定义(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/topoteretes/cognee/8-data-models-and-schemas
翻译时间:2026-05-27T08:45:14.900Z
翻译模型:deepseek-chat
原文字符数:10729
项目:Cognee (cognee)

---

数据模型与模式

相关源文件

以下文件用于生成此 Wiki 页面:

  • cognee/infrastructure/engine/models/DataPoint.py
  • cognee/modules/chunking/models/DocumentChunk.py
  • cognee/modules/engine/models/Entity.py
  • cognee/modules/engine/models/EntityType.py
  • cognee/modules/graph/utils/get_graph_from_model.py
  • cognee/modules/graph/utils/get_model_instance_from_graph.py
  • cognee/modules/pipelines/operations/run_tasks_base.py
  • cognee/modules/pipelines/operations/run_tasks_with_telemetry.py
  • cognee/modules/visualization/cognee_network_visualization.py
  • cognee/shared/CodeGraphEntities.py
  • cognee/tasks/graph/extract_graph_and_summarize.py
  • cognee/tasks/storage/add_data_points.py
  • cognee/tasks/summarization/models.py
  • cognee/tasks/summarization/summarize_text.py
  • cognee/tests/unit/api/v1/__init__.py
  • cognee/tests/unit/api/v1/config/__init__.py
  • cognee/tests/unit/api/v1/config/test_config_set_method.py
  • cognee/tests/unit/modules/visualization/visualization_test.py
  • examples/demos/simple_cognee_example.py

目的与范围

本文档记录了 Cognee 知识引擎中使用的核心数据模型和模式结构。它涵盖了基础的 DataPoint 模型、图实体表示、溯源追踪机制,以及模式在不同存储层(向量数据库、图数据库和关系型数据库)之间的转换方式。

有关配置数据库连接和选择提供者的信息,请参见数据库配置与选择。有关这些模型如何流经处理管线的详细信息,请参见管线任务与执行。有关自定义图模型定义,请参见自定义图模型

---

DataPoint 基础模型

DataPoint 是 Cognee 中所有数据实体继承的基础 Pydantic 模型。它提供了一个标准化结构,用于表示知识图谱中的节点,并内置了版本控制、溯源追踪和元数据管理支持。

核心字段
classDiagram
    class DataPoint {
        +UUID id
        +int created_at
        +int updated_at
        +bool ontology_valid
        +int version
        +int topological_rank
        +MetaData metadata
        +str type
        +List belongs_to_set
        +str source_pipeline
        +str source_task
        +str source_node_set
        +str source_user
        +str source_content_hash
        +float feedback_weight
        +get_embeddable_data()
        +get_embeddable_properties()
        +update_version()
        +to_dict()
        +from_dict()
    }

    class MetaData {
        +str type
        +list index_fields
        +list identity_fields
    }

    DataPoint --> MetaData

标识与确定性

  • id:默认为 uuid4()。如果在 metadata 中定义了 identity_fields,则会通过这些字段的值使用 _generate_identity_id() 生成一个确定性的 uuid5 cognee/infrastructure/engine/models/DataPoint.py:105-131
  • created_at / updated_at:创建和修改时设置的 Unix 时间戳(毫秒级)cognee/infrastructure/engine/models/DataPoint.py:45-50

字段注解 Cognee 使用 Annotated 标记来自动推导元数据:

  • _Embeddable():将字段添加到 index_fields 中,用于向量搜索 cognee/infrastructure/engine/models/DataPoint.py:153-154
  • _Dedup():将字段添加到 identity_fields 中,用于生成确定性 ID cognee/infrastructure/engine/models/DataPoint.py:155-156

来源:cognee/infrastructure/engine/models/DataPoint.py:27-162cognee/infrastructure/engine/models/FieldAnnotations.py:1-15

---

溯源与血缘追踪

Cognee 会在所有 DataPoint 实例流经管线时自动为其打上溯源元数据标记。这支持追踪数据血缘、归属关系以及按来源进行可视化。

溯源字段
graph LR
    subgraph "溯源链"
        Pipeline["source_pipeline<br/>(例如 'cognify_pipeline')"]
        Task["source_task<br/>(例如 'extract_graph_from_data')"]
        NodeSet["source_node_set<br/>(例如 'research_nodes')"]
        User["source_user<br/>(例如 'alice@example.com')"]
        Hash["source_content_hash<br/>(原始数据哈希值)"]
    end

    Pipeline --> Task
    Task --> NodeSet
    NodeSet --> User
    User --> Hash

    DataPoint["DataPoint 实例"] --> Pipeline
自动打标

溯源信息在管线执行期间通过 _stamp_provenance() 应用 cognee/modules/pipelines/operations/run_tasks_base.py:33-85。其过程如下:

  1. 递归遍历:对主要结果和嵌套的 DataPoint 字段进行打标 cognee/modules/pipelines/operations/run_tasks_base.py:73-84
  2. 循环保护:使用 visited 集合通过追踪对象 ID 来防止无限循环 cognee/modules/pipelines/operations/run_tasks_base.py:46-49
  3. 字段传播source_node_setsource_content_hash 从输入参数中提取,并传播到所有输出数据点 cognee/modules/pipelines/operations/run_tasks_base.py:159-180

来源:cognee/modules/pipelines/operations/run_tasks_base.py:33-180cognee/modules/pipelines/operations/run_pipeline.py:1-25

---

图实体模型

知识使用结构化的 Pydantic 模型表示,这些模型通过提取工具转换为图节点和边。

模型到图的提取

系统使用 get_graph_from_model.pyDataPoint 层次结构投影到图结构中。该工具遍历模型字段,识别嵌套的 DataPoint 实例或 Edge 元数据,以构建知识图谱。

graph TD
    subgraph "代码实体空间"
        DP["DataPoint 模型实例"]
        EXT["get_graph_from_model()"]
        SM["_simple_model_for()"]
        EM["Edge(模型)"]
    end

    subgraph "自然语言/图空间"
        N1["节点(源)"]
        N2["节点(目标)"]
        REL["关系(边)"]
    end

    DP --> EXT
    EXT --> SM
    EXT --> EM
    SM --> N1
    SM --> N2
    EM --> REL
    N1 --> REL
    REL --> N2

关键行为:

  • 动态子类化_simple_model_for 创建带记忆功能的 Pydantic 子类,以处理特定的节点形状,同时避免内存泄漏 cognee/modules/graph/utils/get_graph_from_model.py:28-46
  • 边元数据:关系可以通过 Edge 模型包含权重和自定义属性 cognee/modules/graph/utils/get_graph_from_model.py:95-116
  • 重构get_model_instance_from_graph 执行逆操作,从扁平化的图节点和边重新组装完整的 Pydantic 模型层次结构 cognee/modules/graph/utils/get_model_instance_from_graph.py:68-132

来源:cognee/modules/graph/utils/get_graph_from_model.py:1-183cognee/modules/graph/utils/get_model_instance_from_graph.py:9-132cognee/infrastructure/engine/models/Edge.py:1-25

---

三元组嵌入模型

系统使用基于三元组的方法将语义链接表示为可嵌入的单元。这弥合了结构化图数据与向量搜索空间之间的差距。

graph TD
    subgraph "代码实体空间:add_data_points.py"
        NODES["List[DataPoint]"]
        EDGES["List[tuple]"]
        TRIP_GEN["_create_triplets_from_graph()"]
        TRIP_MOD["Triplet(模型)"]
    end

    subgraph "向量空间"
        IDX["index_data_points()"]
        VEC["向量嵌入"]
    end

    NODES --> TRIP_GEN
    EDGES --> TRIP_GEN
    TRIP_GEN --> TRIP_MOD
    TRIP_MOD --> IDX
    IDX --> VEC

三元组通过 brute_force_triplet_search.py 进行处理以用于搜索。它将源节点、关系和目标节点格式化为人类可读的字符串,用于向量嵌入。这使得系统能够执行"关系感知"搜索,查询可以匹配整个图边的语义上下文 cognee/tasks/storage/add_data_points.py:184-205

来源:cognee/tasks/storage/add_data_points.py:143-205cognee/modules/engine/models/Triplet.py:1-15

---

关系型模式

Cognee 使用关系型数据库来管理系统状态、用户数据和文档元数据。

模型用途
User存储用户身份、电子邮件和用于多租户的 tenant_id cognee/modules/users/models.py:1-15
Data已入库文件的元数据(名称、扩展名、content_hashcognee/modules/data/models/Data.py:1-20
DocumentChunk表示文本片段,包含大小、索引和与父文档的关系 cognee/modules/chunking/models/DocumentChunk.py:10-38
TextSummary封装由大语言模型(LLM)生成的 DocumentChunk 实例摘要 cognee/tasks/summarization/models.py:9-25
Entity核心图节点模型,包含名称、类型和描述 cognee/modules/engine/models/Entity.py:7-13

来源:cognee/modules/data/models/Data.py:1-20cognee/modules/chunking/models/DocumentChunk.py:10-38cognee/tasks/summarization/models.py:9-54cognee/modules/engine/models/Entity.py:7-13

---

子页面

有关特定模型的更多技术细节,请参考以下内容:

  • 核心数据模型 — 详细的 DataPoint 生命周期、元数据索引和 Pydantic 配置。
  • 图实体与关系NodeEdge 实现、来自数据库适配器的图投影以及遍历逻辑。
  • 三元组嵌入模型 — 图边如何转换为可向量搜索的三元组,并在检索过程中进行评分。
  • 关系型模式 — 通过 SQLAlchemy 进行用户、租户和数据集管理的 SQL 模式。