agentic_huge_data_base / wiki
页面 Cognee · 5.2 图数据库适配器·DeepWiki 中文全文译文

5.2 · 图数据库适配器(Graph Database Adapters)

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

项目Cognee 章节5.2 状态全文译文 模块检索、召回与索引、图谱与关系、系统架构、测试、发布与运维
源码线索
  • CLAUDE.md
  • cognee/infrastructure/databases/graph/graph_db_interface.py
  • cognee/infrastructure/databases/graph/kuzu/adapter.py
  • cognee/infrastructure/databases/graph/ladybug/LadybugDatasetDatabaseHandler.py
  • cognee/infrastructure/databases/graph/ladybug/adapter.py
  • cognee/infrastructure/databases/graph/ladybug/ladybug_migrate.py
  • cognee/infrastructure/databases/graph/ladybug/remote_ladybug_adapter.py
  • cognee/infrastructure/databases/graph/neo4j_driver/adapter.py
  • cognee/infrastructure/databases/graph/neptune_driver/__init__.py
  • cognee/infrastructure/databases/graph/neptune_driver/adapter.py
模块标签
  • 检索、召回与索引
  • 图谱与关系
  • 系统架构
  • 测试、发布与运维
  • 存储与持久化

章节正文

图数据库适配器

图数据库适配器

相关源文件

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

  • CLAUDE.md
  • cognee/infrastructure/databases/graph/graph_db_interface.py
  • cognee/infrastructure/databases/graph/kuzu/adapter.py
  • cognee/infrastructure/databases/graph/ladybug/LadybugDatasetDatabaseHandler.py
  • cognee/infrastructure/databases/graph/ladybug/adapter.py
  • cognee/infrastructure/databases/graph/ladybug/ladybug_migrate.py
  • cognee/infrastructure/databases/graph/ladybug/remote_ladybug_adapter.py
  • cognee/infrastructure/databases/graph/neo4j_driver/adapter.py
  • cognee/infrastructure/databases/graph/neptune_driver/__init__.py
  • cognee/infrastructure/databases/graph/neptune_driver/adapter.py
  • cognee/infrastructure/databases/graph/neptune_driver/exceptions.py
  • cognee/infrastructure/databases/graph/neptune_driver/neptune_utils.py
  • cognee/infrastructure/databases/graph/postgres/__init__.py
  • cognee/infrastructure/databases/graph/postgres/adapter.py
  • cognee/infrastructure/databases/graph/postgres/tables.py
  • cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py
  • cognee/infrastructure/databases/hybrid/neptune_analytics/__init__.py
  • cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py
  • cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py
  • cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py
  • cognee/infrastructure/databases/vector/vector_db_interface.py
  • cognee/modules/retrieval/cypher_search_retriever.py
  • cognee/modules/retrieval/natural_language_retriever.py
  • cognee/tasks/temporal_awareness/index_graphiti_objects.py
  • cognee/tests/test_neptune_analytics_graph.py
  • cognee/tests/test_neptune_analytics_hybrid.py
  • cognee/tests/test_neptune_analytics_vector.py
  • cognee/tests/unit/infrastructure/databases/graph/test_ladybug_migrate.py
  • cognee/tests/unit/infrastructure/databases/test_get_unified_engine.py
  • cognee/tests/unit/infrastructure/databases/test_neptune_analytics_hybrid.py
  • cognee_db_workers/ladybug_migrate.py
  • examples/configurations/permissions_example/data_access_control_example.py
  • examples/configurations/permissions_example/tenant_role_constraints_example.py
  • examples/configurations/permissions_example/tenant_role_setup_example.py
  • examples/database_examples/neptune_analytics_example.py
  • notebooks/neptune-analytics-example.ipynb

目的与范围

本文档描述了 Cognee 中的图数据库适配器层,该层为与不同图数据库后端的交互提供了统一接口。适配器模式使 Cognee 能够通过通用的 GraphDBInterface 支持多种图数据库提供商(Ladybug/Kuzu、Neo4j、Neptune、Postgres、NetworkX)。该层对于 Cognee 的 "Cognify" 流程至关重要,在该流程中,结构化知识会被提取并以节点和边的形式持久化存储。

关于向量数据库适配器的信息,请参见向量数据库适配器。关于关系型数据库配置,请参见关系型数据库适配器。关于数据库提供商选择与工厂模式,请参见数据库配置与选择

适配器架构总览

Cognee 使用适配器模式实现了可插拔的图数据库层。所有图数据库操作都通过 GraphDBInterface 进行,具体的适配器会为特定的数据库提供商实现该接口。这种抽象使得更高层的检索逻辑(如 GraphCompletionRetriever)无需关心底层存储是嵌入式数据库还是远程集群。

代码实体空间映射

下图将自然语言搜索意图与负责图持久化和检索的具体代码实体进行了关联。

Cognee · 代码实体空间映射 · 图 1
Cognee · 代码实体空间映射 · 图 1

来源: cognee/infrastructure/databases/graph/graph_db_interface.py:17-40cognee/infrastructure/databases/graph/ladybug/adapter.py:47-56cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:52-57cognee/infrastructure/databases/graph/postgres/adapter.py:24-25cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py:45-52

GraphDBInterface

GraphDBInterface 抽象基类定义了所有图数据库适配器必须实现的契约。它为节点和边的操作、图遍历以及指标统计提供了统一的 API。

核心接口方法
方法类别方法用途
节点操作add_node()add_nodes()delete_node()delete_nodes()get_node()get_nodes()节点的创建、读取、更新、删除操作 cognee/infrastructure/databases/graph/graph_db_interface.py:62-156
边操作add_edge()add_edges()has_edge()has_edges()get_edges()管理节点间的关系 cognee/infrastructure/databases/graph/graph_db_interface.py:159-230
图遍历get_neighbors()get_predecessors()get_successors()get_connections()导航图结构
图查询query()is_empty()get_graph_data()get_graph_metrics()执行查询并检索图信息 cognee/infrastructure/databases/graph/graph_db_interface.py:43-58

来源: cognee/infrastructure/databases/graph/graph_db_interface.py:42-230

支持的提供商

Ladybug(Kuzu)适配器

LadybugAdapter(原名 KuzuAdapter)使用 Kuzu 提供了基于文件的嵌入式图数据库。它是 Cognee 的默认提供商。

  • 实现方式:支持本地模式和子进程代理模式,以实现资源隔离 cognee/infrastructure/databases/graph/ladybug/adapter.py:58-64
  • JSON 支持:自动处理 JSON 扩展,以存储复杂属性 cognee/infrastructure/databases/graph/ladybug/adapter.py:79-80
  • 持久化:管理 DatabaseConnection 句柄,支持可配置的缓冲池和数据库大小 cognee/infrastructure/databases/graph/ladybug/adapter.py:119-145
  • 迁移:保留 KuzuAdapter 的历史导入路径,以实现向后兼容 cognee/infrastructure/databases/graph/kuzu/adapter.py:1-17

来源: cognee/infrastructure/databases/graph/ladybug/adapter.py:47-114cognee/infrastructure/databases/graph/kuzu/adapter.py:1-23

Neo4j 适配器

Neo4jAdapter 通过 Bolt 协议连接到 Neo4j 实例。

  • 标签:对所有实体使用基础标签 __Node__,以优化全局查找 cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:49-50
  • 约束管理:自动为节点的 id 属性初始化唯一性约束 cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:101-107
  • 弹性:实现 @deadlock_retry() 装饰器,以处理高并发写入期间的临时数据库锁 cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:127-128
  • 可观测性:集成 OpenTelemetry,以跟踪查询执行和行数统计 cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:149-158

来源: cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:52-164

Neptune Analytics 适配器

NeptuneAnalyticsAdapter 是一个混合适配器,结合了 Neptune Analytics 的向量和图功能。

  • 混合能力:扩展了 NeptuneGraphDB 并实现了 VectorDBInterface,为图和向量操作提供统一接口 cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py:45-52
  • 向量集成:将向量集合映射到图节点,使用 neptune.algo.vectors.upsert() 进行嵌入向量的持久化 cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py:160-180

来源: cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py:45-180

Postgres 图适配器

PostgresAdapter 使用两个表(graph_nodegraph_edge)在关系型模式上实现了图结构。

  • 模式:基于 SQLAlchemy 和 asyncpg cognee/infrastructure/databases/graph/postgres/adapter.py:24-33
  • 更新插入逻辑:使用 PostgreSQL 特有的 on_conflict_do_update 实现高效的批量节点/边插入 cognee/infrastructure/databases/graph/postgres/adapter.py:139-152
  • 限制支持原始 Cypher 查询。尝试调用 .query() 会抛出 NotImplementedError cognee/infrastructure/databases/graph/postgres/adapter.py:60-71
  • 搜索兼容性:自然语言搜索(Cypher 生成)和 CypherSearchRetriever 在此后端上被明确禁用 cognee/modules/retrieval/natural_language_retriever.py:111-119cognee/modules/retrieval/cypher_search_retriever.py:43-47

来源: cognee/infrastructure/databases/graph/postgres/adapter.py:1-153cognee/modules/retrieval/cypher_search_retriever.py:34-47

查询模式与检索

图适配器主要通过 Search 模块中的各种 BaseRetriever 实现来使用。

自然语言转 Cypher

对于原生图后端(Neo4j、Ladybug),NaturalLanguageRetriever 会基于图模式使用大语言模型(LLM)生成 Cypher 查询。

Cognee · 自然语言转 Cypher · 图 2
Cognee · 自然语言转 Cypher · 图 2

来源: cognee/modules/retrieval/natural_language_retriever.py:35-105

Cypher 搜索执行

CypherSearchRetriever 允许在兼容的后端上直接执行 Cypher 查询。

  • 校验:检查图引擎是否为 PostgresAdapterPostgresHybridAdapter,如果是则抛出 SearchTypeNotSupported cognee/modules/retrieval/cypher_search_retriever.py:38-47
  • 执行:在确认图不为空后,直接调用适配器的 query() 方法 cognee/modules/retrieval/cypher_search_retriever.py:49-55

来源: cognee/modules/retrieval/cypher_search_retriever.py:33-61

配置与工厂

工厂系统会根据系统配置实例化正确的适配器。

提供商适配器类是否支持 Cypher
ladybug / kuzuLadybugAdapter
neo4jNeo4jAdapter
neptuneNeptuneAnalyticsAdapter
postgresPostgresAdapter

来源: cognee/infrastructure/databases/graph/postgres/adapter.py:67-71cognee/modules/retrieval/cypher_search_retriever.py:35-47cognee/modules/retrieval/natural_language_retriever.py:108-119