数据库架构
数据库架构
相关源文件
本章引用的主要源码文件:
cognee/base_config.pycognee/context_global_variables.pycognee/infrastructure/databases/graph/config.pycognee/infrastructure/databases/graph/get_graph_engine.pycognee/infrastructure/databases/graph/graph_db_interface.pycognee/infrastructure/databases/graph/kuzu/adapter.pycognee/infrastructure/databases/graph/neo4j_driver/adapter.pycognee/infrastructure/databases/utils/get_or_create_dataset_database.pycognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.pycognee/infrastructure/databases/vector/config.pycognee/infrastructure/databases/vector/create_vector_engine.pycognee/infrastructure/databases/vector/get_vector_engine.pycognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.pycognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.pycognee/infrastructure/databases/vector/vector_db_interface.pycognee/modules/users/models/DatasetDatabase.pycognee/root_dir.pycognee/shared/lru_cache.py
本文档概述了 Cognee 的三数据库架构、支持多种数据库提供方的适配器模式,以及用于初始化数据库引擎的工厂函数。有关特定数据库适配器的详细信息,请参见向量数据库适配器、图数据库适配器和关系数据库适配器。有关配置详情,请参见数据库配置与选择。有关多租户数据库隔离,请参见多租户访问控制。
目的与范围
Cognee 采用三数据库架构,每种数据库类型服务于不同的目的:
| 数据库类型 | 目的 | 主要操作 |
|---|---|---|
| 向量数据库 | 通过嵌入向量进行语义相似性搜索 | 存储和检索文本片段嵌入向量、实体嵌入向量、三元组嵌入向量 |
| 图数据库 | 存储知识图谱结构和关系 | 节点/边操作、图遍历、关系查询 |
| 关系数据库 | 存储元数据、用户数据、数据集、权限 | 结构化数据的增删改查操作、会话管理 |
这种分离方式可以在保持系统数据一致性的同时,为每种操作类型提供优化的性能。
来源:cognee/infrastructure/databases/vector/vector_db_interface.py:1-30、cognee/infrastructure/databases/graph/graph_db_interface.py:17-40、cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py:25-50
三数据库架构
下图展示了三种数据库类型如何与 Cognee 核心操作及其对应的代码实体进行交互:
存储职责
每种数据库类型存储特定的数据结构:
向量数据库:
- 文档片段嵌入向量和实体名称嵌入向量。
- 用于关系感知搜索的三元组嵌入向量。
- 通过
create_vector_engine处理,该函数会解析EmbeddingEnginecognee/infrastructure/databases/vector/create_vector_engine.py:82-96。
图数据库:
- 从数据模型中提取的节点和边。
- 用于遍历查询的图结构。
- 支持多种提供方,如 Kuzu(通过
LadybugAdapter)、Neo4j 和 Postgrescognee/infrastructure/databases/graph/get_graph_engine.py:158-185。
关系数据库:
Dataset、Data、User和DatasetDatabase的元数据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-109、cognee/infrastructure/databases/graph/get_graph_engine.py:115-185、cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:51-93
适配器模式实现
Cognee 使用适配器模式来支持多种数据库提供方。每个适配器实现一个通用接口,同时处理提供方特定的细节。
接口定义
- 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-76、cognee/infrastructure/databases/graph/graph_db_interface.py:17-40、cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:51-93
数据库提供方支持
| 数据库类型 | 提供方 | 适配器类 | 工厂函数 |
|---|---|---|---|
| 向量 | LanceDB | LanceDBAdapter | create_vector_engine cognee/infrastructure/databases/vector/create_vector_engine.py:48-48 |
| 向量 | PGVector | PGVectorAdapter | create_vector_engine cognee/infrastructure/databases/vector/create_vector_engine.py:48-48 |
| 图 | Kuzu | LadybugAdapter | create_graph_engine cognee/infrastructure/databases/graph/get_graph_engine.py:115-115 |
| 图 | Neo4j | Neo4jAdapter | create_graph_engine cognee/infrastructure/databases/graph/get_graph_engine.py:115-115 |
| 关系 | SQLite | SQLAlchemyAdapter | get_relational_engine cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py:69-69 |
来源:cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py:73-76、cognee/infrastructure/databases/graph/kuzu/adapter.py:10-17、cognee/infrastructure/databases/graph/neo4j_driver/adapter.py:52-60
工厂函数与初始化
数据库引擎通过工厂函数创建,这些函数读取配置并实例化相应的适配器。
统一提供方:如果 USE_UNIFIED_PROVIDER 设置为 pghybrid,则向量引擎和图引擎将默认使用连接到主关系数据库的 PGVectorAdapter 或 PostgresAdapter cognee/infrastructure/databases/graph/get_graph_engine.py:159-166、cognee/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 并实例化 LanceDBAdapter 或 PGVectorAdapter 等适配器 cognee/infrastructure/databases/vector/create_vector_engine.py:48-109。
来源:cognee/infrastructure/databases/graph/get_graph_engine.py:115-185、cognee/infrastructure/databases/vector/create_vector_engine.py:48-109
多租户访问控制
Cognee 通过为不同数据集和用户创建独立的数据库实例(文件或模式)来支持多用户隔离。
- 上下文管理:系统使用
ContextVar为每个异步任务存储vector_db_config和graph_db_configcognee/context_global_variables.py:23-27。 - 动态数据库创建:
get_or_create_dataset_database创建一个DatasetDatabase记录,将用户和数据集链接到特定的数据库连接信息cognee/infrastructure/databases/utils/get_or_create_dataset_database.py:64-116。 - 访问控制标志:多租户行为由
backend_access_control_enabled()控制,该函数检查配置的提供方是否支持隔离cognee/context_global_variables.py:83-96。
来源:cognee/context_global_variables.py:23-96、cognee/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-8、cognee/infrastructure/databases/vector/create_vector_engine.py:7-8。
有关详细信息,请参见缓存层。
来源:cognee/base_config.py:11-30、cognee/infrastructure/databases/graph/get_graph_engine.py:7-13