自定义 Graph 模型
自定义图模型
相关源文件
以下文件为本 Wiki 页面生成时使用的上下文:
cognee/eval_framework/corpus_builder/task_getters/get_cascade_graph_tasks.pycognee/eval_framework/corpus_builder/task_getters/get_default_tasks_by_indices.pycognee/modules/graph/utils/expand_with_nodes_and_edges.pycognee/modules/graph/utils/get_graph_from_model.pycognee/modules/graph/utils/get_model_instance_from_graph.pycognee/modules/ontology/base_ontology_resolver.pycognee/modules/ontology/get_default_ontology_resolver.pycognee/modules/ontology/matching_strategies.pycognee/modules/ontology/models.pycognee/modules/ontology/ontology_config.pycognee/modules/ontology/ontology_env_config.pycognee/modules/ontology/rdf_xml/RDFLibOntologyResolver.pycognee/tasks/graph/extract_graph_and_summarize.pycognee/tasks/graph/extract_graph_from_data.pycognee/tasks/graph/extract_graph_from_data_v2.pycognee/tasks/storage/add_data_points.pycognee/tasks/summarization/summarize_text.pycognee/tests/unit/api/v1/__init__.pycognee/tests/unit/api/v1/config/__init__.pycognee/tests/unit/api/v1/config/test_config_set_method.pycognee/tests/unit/eval_framework/test_get_default_tasks_by_indices.pycognee/tests/unit/infrastructure/engine/test_identity_fields.pycognee/tests/unit/modules/ontology/test_ontology_adapter.pycognee/tests/unit/tasks/graph/test_extract_graph_from_data_v2.pyexamples/custom_pipelines/organizational_hierarchy/organizational_hierarchy_pipeline_example.pyexamples/demos/simple_cognee_example.pyexamples/guides/consolidate_entity_descriptions_example.pyexamples/guides/custom_data_models.pyexamples/guides/custom_graph_model.pyexamples/guides/custom_prompts.pyexamples/guides/custom_tasks_and_pipelines.pyexamples/guides/graph_visualization.pyexamples/guides/importance_weight.pyexamples/guides/improve_quickstart.pyexamples/guides/ontology_quickstart.py
Cognee 中的自定义图模型是 Pydantic 类(继承自 DataPoint),通过 graph_model 参数传递给 cognify() 或 remember() 函数。这些模型定义了模式,用于指导大语言模型(LLM)从文档中提取实体和关系。通过指定自定义模型,你可以精确控制系统提取的实体和关系类型,从而生成特定领域的知识图谱。
范围:本页面涵盖 graph_model 参数、默认的 KnowledgeGraph 模型、如何定义自定义 DataPoint 提取模型,以及将这些模型转换为图结构的内部逻辑。
概述
cognify() 中的 graph_model 参数接受任何定义了 LLM 提取结构的 Pydantic BaseModel(具体来说是 DataPoint)。在 extract_graph_from_data 任务中,Cognee 会将这些模型与结构化输出框架结合使用,确保 LLM 返回的数据符合你指定的模式。
| 组件 | 用途 | 代码实体 |
|---|---|---|
| 提取模型 | 定义模式的 Pydantic 类 | graph_model: Type[BaseModel] |
| 提取任务 | 编排针对片段(chunk)的 LLM 调用 | extract_graph_from_data() |
| 集成逻辑 | 将提取的图合并到数据库中 | integrate_chunk_graphs() |
| 图提取 | 递归地将模型转换为节点和边 | get_graph_from_model() |
来源:cognee/tasks/graph/extract_graph_from_data.py:129-136、cognee/modules/graph/utils/get_graph_from_model.py:178-184、examples/guides/custom_graph_model.py:14-33
Cognify 管线中的图模型流程
下图连接了自然语言空间(用户的提取意图)和代码实体空间(处理模型的任务和函数)。
图:提取模型数据流
来源:cognee/tasks/graph/extract_graph_from_data.py:56-125、cognee/tasks/storage/add_data_points.py:30-72、cognee/infrastructure/llm/extraction.py:20-20
默认的 KnowledgeGraph 模型
当未指定 graph_model 时,Cognee 会默认使用 KnowledgeGraph 模型。如果提供了自定义模型,系统会跳过标准的本体扩展逻辑,将提供的模型视为严格的提取模式。
# integrate_chunk_graphs 中的逻辑检查
if graph_model is not KnowledgeGraph:
for chunk_index, chunk_graph in enumerate(chunk_graphs):
data_chunks[chunk_index].contains = chunk_graph
return data_chunks
来源:cognee/tasks/graph/extract_graph_from_data.py:99-103、cognee/shared/data_models.py:19-19
内部图提取逻辑
当 Cognee 存储数据点(无论是来自自定义模型还是默认模型)时,它会使用 get_graph_from_model 将 Pydantic 对象转换为由节点和边组成的图结构。这个过程会递归地将嵌套的 DataPoint 对象视为相关节点。
图:get_graph_from_model 逻辑
提取过程中的关键函数:
_extract_field_data:判断字段值是否包含DataPoint、DataPoint列表或Tuple[Edge, DataPoint]。这样可以附加特定的边元数据。cognee/modules/graph/utils/get_graph_from_model.py:49-92_create_edge_properties:构建图边的属性字典,包括source_node_id、target_node_id和relationship_name。cognee/modules/graph/utils/get_graph_from_model.py:95-117get_graph_from_model:递归遍历模型的主入口。它使用added_nodes和visited_properties来防止循环图中的无限循环。cognee/modules/graph/utils/get_graph_from_model.py:178-184- 记忆化:Cognee 使用
_simple_model_for来缓存 Pydantic 类,以防止在大型提取周期中因重复调用copy_model而导致内存泄漏。cognee/modules/graph/utils/get_graph_from_model.py:28-47
来源:cognee/modules/graph/utils/get_graph_from_model.py:28-184
本体集成与扩展
对于默认的 KnowledgeGraph 模型,Cognee 会使用 ontology_resolver 执行本体验证和扩展。
- 本体解析器:通常是加载 OWL/RDF 文件的
RDFLibOntologyResolver。cognee/modules/ontology/rdf_xml/RDFLibOntologyResolver.py:14-20 - 扩展:
expand_with_nodes_and_edges函数会检查提取的实体在本体中是否存在父类或相关个体。cognee/modules/graph/utils/expand_with_nodes_and_edges.py:110-112 - 标准化:它使用
generate_node_id和generate_node_name对节点名称和 ID 进行标准化,以确保一致性。cognee/modules/graph/utils/expand_with_nodes_and_edges.py:39-40
来源:cognee/tasks/graph/extract_graph_from_data.py:110-112、cognee/modules/graph/utils/expand_with_nodes_and_edges.py:31-65
存储与索引
从自定义模型中提取图后,会由 add_data_points 任务进行处理。
- 去重:
deduplicate_nodes_and_edges确保如果多个片段提取到相同的实体,数据库中只会创建一个节点。cognee/tasks/storage/add_data_points.py:78-78 - 混合写入:如果数据库支持,
add_nodes_with_vectors会一步完成节点及其嵌入向量的保存。cognee/tasks/storage/add_data_points.py:87-88 - 向量索引:
index_data_points根据模型metadata字典中定义的index_fields为节点生成嵌入向量。cognee/tasks/storage/add_data_points.py:91-95、examples/guides/custom_graph_model.py:21-21 - 三元组嵌入:如果启用了
embed_triplets,系统会根据图结构创建Triplet对象,用于关系感知搜索。cognee/tasks/storage/add_data_points.py:143-147
来源:cognee/tasks/storage/add_data_points.py:78-147、cognee/modules/engine/models.py:16-16
示例:自定义编程语言模型
自定义模型通过类型提示定义关系。在下面的示例中,used_in 成为一条边,因为它指向 Field 对象列表(这些对象是 DataPoint)。
from typing import List
from cognee.low_level import DataPoint
class Field(DataPoint):
name: str
metadata: dict = {"index_fields": ["name"]}
class ProgrammingLanguage(DataPoint):
name: str
used_in: List[Field] = None
metadata: dict = {"index_fields": ["name"]}
# 使用方式
await cognee.remember(text, graph_model = ProgrammingLanguage)
extract_graph_from_data使用 LLM 填充ProgrammingLanguage实例。cognee/tasks/graph/extract_graph_from_data.py:161-173get_graph_from_model将used_in识别为关系,因为Field继承自DataPoint。cognee/modules/graph/utils/get_graph_from_model.py:49-53add_data_points将结果存储在图数据库和向量数据库中。cognee/tasks/storage/add_data_points.py:30-45
来源:examples/guides/custom_graph_model.py:14-33、cognee/tasks/graph/extract_graph_from_data.py:129-173