agentic_huge_data_base / wiki
页面 Cognee · 5 数据库架构·DeepWiki 中文全文译文

5 · 数据库架构(Database Architecture)

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

项目Cognee 章节5 状态全文译文 模块存储与持久化、检索、召回与索引、模型调用与提供方适配、配置治理
源码线索
  • cognee/base_config.py
  • cognee/context_global_variables.py
  • cognee/infrastructure/databases/graph/config.py
  • cognee/infrastructure/databases/graph/get_graph_engine.py
  • cognee/infrastructure/databases/graph/graph_db_interface.py
  • cognee/infrastructure/databases/graph/kuzu/adapter.py
  • cognee/infrastructure/databases/graph/neo4j_driver/adapter.py
  • cognee/infrastructure/databases/utils/get_or_create_dataset_database.py
  • cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py
  • cognee/infrastructure/databases/vector/config.py
模块标签
  • 存储与持久化
  • 检索、召回与索引
  • 模型调用与提供方适配
  • 配置治理
  • 系统架构

章节正文

数据库架构

数据库架构

相关源文件

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

  • cognee/base_config.py
  • cognee/context_global_variables.py
  • cognee/infrastructure/databases/graph/config.py
  • cognee/infrastructure/databases/graph/get_graph_engine.py
  • cognee/infrastructure/databases/graph/graph_db_interface.py
  • cognee/infrastructure/databases/graph/kuzu/adapter.py
  • cognee/infrastructure/databases/graph/neo4j_driver/adapter.py
  • cognee/infrastructure/databases/utils/get_or_create_dataset_database.py
  • cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py
  • cognee/infrastructure/databases/vector/config.py
  • cognee/infrastructure/databases/vector/create_vector_engine.py
  • cognee/infrastructure/databases/vector/get_vector_engine.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/users/models/DatasetDatabase.py
  • cognee/root_dir.py
  • cognee/shared/lru_cache.py

本文档概述了 Cognee 的三数据库架构、支持多种数据库提供方的适配器模式,以及用于初始化数据库引擎的工厂函数。有关特定数据库适配器的详细信息,请参见向量数据库适配器图数据库适配器关系数据库适配器。有关配置详情,请参见数据库配置与选择。有关多租户数据库隔离,请参见多租户访问控制

目的与范围

Cognee 采用三数据库架构,每种数据库类型服务于不同的目的:

数据库类型目的主要操作
向量数据库通过嵌入向量进行语义相似性搜索存储和检索文本片段嵌入向量、实体嵌入向量、三元组嵌入向量
图数据库存储知识图谱结构和关系节点/边操作、图遍历、关系查询
关系数据库存储元数据、用户数据、数据集、权限结构化数据的增删改查操作、会话管理

这种分离方式可以在保持系统数据一致性的同时,为每种操作类型提供优化的性能。

来源:cognee/infrastructure/databases/vector/vector_db_interface.py:1-30cognee/infrastructure/databases/graph/graph_db_interface.py:17-40cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py:25-50

三数据库架构

下图展示了三种数据库类型如何与 Cognee 核心操作及其对应的代码实体进行交互:

Cognee · 三数据库架构 · 图 1
Cognee · 三数据库架构 · 图 1

存储职责

每种数据库类型存储特定的数据结构:

向量数据库

  • 文档片段嵌入向量和实体名称嵌入向量。
  • 用于关系感知搜索的三元组嵌入向量。
  • 通过 create_vector_engine 处理,该函数会解析 EmbeddingEngine cognee/infrastructure/databases/vector/create_vector_engine.py:82-96

图数据库

  • 从数据模型中提取的节点和边。
  • 用于遍历查询的图结构。
  • 支持多种提供方,如 Kuzu(通过 LadybugAdapter)、Neo4j 和 Postgres cognee/infrastructure/databases/graph/get_graph_engine.py:158-185

关系数据库

  • DatasetDataUserDatasetDatabase 的元数据 cognee/infrastructure/databases/utils/get_or_create_dataset_database.py:103-116
  • SQLAlchemyAdapter 管理,该适配器处理异步会话 cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:51-85

来源:cognee/infrastructure/databases/vector/create_vector_engine.py:48-109cognee/infrastructure/databases/graph/get_graph_engine.py:115-185cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:51-93

适配器模式实现

Cognee 使用适配器模式来支持多种数据库提供方。每个适配器实现一个通用接口,同时处理提供方特定的细节。

Cognee · 适配器模式实现 · 图 2
Cognee · 适配器模式实现 · 图 2

接口定义

  • VectorDBInterface:定义集合管理和向量搜索的方法 cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py:73-73
  • GraphDBInterface:定义节点/边操作和图查询的方法 cognee/infrastructure/databases/graph/graph_db_interface.py:17-40
  • SQLAlchemyAdapter:具体类,支持 SQLite 和 PostgreSQL,并支持异步执行 cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:51-51

来源:cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py:73-76cognee/infrastructure/databases/graph/graph_db_interface.py:17-40cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:51-93

数据库提供方支持

数据库类型提供方适配器类工厂函数
向量LanceDBLanceDBAdaptercreate_vector_engine cognee/infrastructure/databases/vector/create_vector_engine.py:48-48
向量PGVectorPGVectorAdaptercreate_vector_engine cognee/infrastructure/databases/vector/create_vector_engine.py:48-48
KuzuLadybugAdaptercreate_graph_engine cognee/infrastructure/databases/graph/get_graph_engine.py:115-115
Neo4jNeo4jAdaptercreate_graph_engine cognee/infrastructure/databases/graph/get_graph_engine.py:115-115
关系SQLiteSQLAlchemyAdapterget_relational_engine cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:69-69

来源:cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py:73-76cognee/infrastructure/databases/graph/kuzu/adapter.py:10-17cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:52-60

工厂函数与初始化

数据库引擎通过工厂函数创建,这些函数读取配置并实例化相应的适配器。

统一提供方:如果 USE_UNIFIED_PROVIDER 设置为 pghybrid,则向量引擎和图引擎将默认使用连接到主关系数据库的 PGVectorAdapterPostgresAdapter cognee/infrastructure/databases/graph/get_graph_engine.py:159-166cognee/infrastructure/databases/vector/create_vector_engine.py:78-96

图引擎工厂create_graph_engine 函数根据 graph_database_provider 初始化适配器。它会处理特定需求,例如 Ladybug/Kuzu 的 graph_file_path 或 Neo4j 的 graph_database_url cognee/infrastructure/databases/graph/get_graph_engine.py:115-185

向量引擎工厂create_vector_engine 函数解析 embedding_engine 并实例化 LanceDBAdapterPGVectorAdapter 等适配器 cognee/infrastructure/databases/vector/create_vector_engine.py:48-109

来源:cognee/infrastructure/databases/graph/get_graph_engine.py:115-185cognee/infrastructure/databases/vector/create_vector_engine.py:48-109

多租户访问控制

Cognee 通过为不同数据集和用户创建独立的数据库实例(文件或模式)来支持多用户隔离。

  1. 上下文管理:系统使用 ContextVar 为每个异步任务存储 vector_db_configgraph_db_config cognee/context_global_variables.py:23-27
  2. 动态数据库创建get_or_create_dataset_database 创建一个 DatasetDatabase 记录,将用户和数据集链接到特定的数据库连接信息 cognee/infrastructure/databases/utils/get_or_create_dataset_database.py:64-116
  3. 访问控制标志:多租户行为由 backend_access_control_enabled() 控制,该函数检查配置的提供方是否支持隔离 cognee/context_global_variables.py:83-96

来源:cognee/context_global_variables.py:23-96cognee/infrastructure/databases/utils/get_or_create_dataset_database.py:64-116

缓存层

Cognee 维护一个缓存层来存储临时数据和计算结果。BaseConfig 定义了 cache_root_directory,默认为 .cognee_cache,但可以自动配置为 S3 存储 cognee/base_config.py:14-30。数据库引擎实例本身通过专用的 closing_lru_cache 进行缓存,以管理资源生命周期 cognee/infrastructure/databases/graph/get_graph_engine.py:7-8cognee/infrastructure/databases/vector/create_vector_engine.py:7-8

有关详细信息,请参见缓存层

来源:cognee/base_config.py:11-30cognee/infrastructure/databases/graph/get_graph_engine.py:7-13