Buckets:

download
raw
8.65 kB
"""This module contains the configuration for the graph database."""
import os
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict
import pydantic
from pydantic import Field
from cognee.base_config import get_base_config
from cognee.root_dir import ensure_absolute_path
from cognee.shared.data_models import KnowledgeGraph
# Single source of truth for Kuzu defaults lives next to the adapter.
# Imported here so config-side env Fields and the adapter agree.
from cognee.infrastructure.databases.graph.kuzu.adapter import (
DEFAULT_KUZU_BUFFER_POOL_SIZE,
DEFAULT_KUZU_MAX_DB_SIZE,
)
class GraphConfig(BaseSettings):
"""
Represents the configuration for a graph system, including parameters for graph file
storage and database connections.
Public methods:
- to_dict
- to_hashable_dict
Instance variables:
- graph_filename
- graph_database_provider
- graph_database_url
- graph_database_username
- graph_database_password
- graph_database_port
- graph_database_key
- graph_file_path
- graph_model
- graph_topology
- model_config
"""
# Using Field we are able to dynamically load current GRAPH_DATABASE_PROVIDER value in the model validator part
# and determine default graph db file and path based on this parameter if no values are provided
graph_database_provider: str = Field("ladybug", env="GRAPH_DATABASE_PROVIDER")
graph_database_url: str = ""
graph_database_name: str = ""
graph_database_username: str = ""
graph_database_password: str = ""
graph_database_host: str = ""
graph_database_allow_anonymous: bool = False
graph_database_port: int = 123
graph_database_key: str = ""
graph_file_path: str = ""
graph_filename: str = ""
graph_model: object = KnowledgeGraph
graph_topology: object = KnowledgeGraph
graph_dataset_database_handler: str = "ladybug"
graph_database_subprocess_enabled: bool = True
# Kuzu tuning. 0 means "use Kuzu's default" (one thread per CPU).
kuzu_num_threads: int = Field(0, env="KUZU_NUM_THREADS")
kuzu_buffer_pool_size: int = Field(DEFAULT_KUZU_BUFFER_POOL_SIZE, env="KUZU_BUFFER_POOL_SIZE")
kuzu_max_db_size: int = Field(DEFAULT_KUZU_MAX_DB_SIZE, env="KUZU_MAX_DB_SIZE")
model_config = SettingsConfigDict(env_file=".env", extra="allow", populate_by_name=True)
# Model validator updates graph_filename and path dynamically after class creation based on current database provider
# If no specific graph_filename or path are provided
@pydantic.model_validator(mode="after")
def fill_derived(self):
provider = self.graph_database_provider.lower()
self.graph_database_provider = provider
graph_dataset_database_handler = self.graph_dataset_database_handler.lower()
self.graph_dataset_database_handler = graph_dataset_database_handler
if provider == "kuzu" and graph_dataset_database_handler == "ladybug":
self.graph_dataset_database_handler = "kuzu"
if provider in ("postgres", "postgres_demo") and graph_dataset_database_handler in (
"ladybug",
"postgres",
"postgres_demo",
):
self.graph_dataset_database_handler = "postgres_graph"
if provider == "neo4j" and graph_dataset_database_handler in ("ladybug", "neo4j"):
self.graph_dataset_database_handler = "neo4j"
if provider == "turso" and graph_dataset_database_handler in ("ladybug", "turso"):
self.graph_dataset_database_handler = "turso_graph"
base_config = get_base_config()
databases_directory_path = os.path.join(base_config.system_root_directory, "databases")
# Set default filename if no filename is provided. For the Ladybug rename, keep using an
# existing default Kuzu database path so local users do not silently start with an empty graph.
if not self.graph_filename:
graph_directory = self.graph_file_path or databases_directory_path
self.graph_filename = f"cognee_graph_{provider}"
if provider == "ladybug":
legacy_graph_path = os.path.join(graph_directory, "cognee_graph_kuzu")
if os.path.exists(legacy_graph_path):
self.graph_filename = "cognee_graph_kuzu"
# Handle graph file path
if self.graph_file_path:
# Check if absolute path is provided
self.graph_file_path = ensure_absolute_path(
os.path.join(self.graph_file_path, self.graph_filename)
)
else:
# Default path
self.graph_file_path = os.path.join(databases_directory_path, self.graph_filename)
return self
def to_dict(self) -> dict:
"""
Return the configuration as a dictionary.
This dictionary contains all the configurations related to the graph, which includes
details for file storage and database connectivity.
Returns:
--------
- dict: A dictionary representation of the configuration settings.
"""
return {
"graph_filename": self.graph_filename,
"graph_database_provider": self.graph_database_provider,
"graph_database_url": self.graph_database_url,
"graph_database_username": self.graph_database_username,
"graph_database_password": self.graph_database_password,
"graph_database_host": self.graph_database_host,
"graph_database_allow_anonymous": self.graph_database_allow_anonymous,
"graph_database_port": self.graph_database_port,
"graph_database_key": self.graph_database_key,
"graph_file_path": self.graph_file_path,
"graph_model": self.graph_model,
"graph_topology": self.graph_topology,
"model_config": self.model_config,
"graph_dataset_database_handler": self.graph_dataset_database_handler,
"graph_database_subprocess_enabled": self.graph_database_subprocess_enabled,
"kuzu_num_threads": self.kuzu_num_threads,
"kuzu_buffer_pool_size": self.kuzu_buffer_pool_size,
"kuzu_max_db_size": self.kuzu_max_db_size,
}
def to_hashable_dict(self) -> dict:
"""
Return a hashable dictionary with essential database configuration parameters.
This dictionary excludes certain non-hashable objects and focuses on unique identifiers
for database configurations.
Returns:
--------
- dict: A dictionary representation of the essential database configuration
settings.
"""
return {
"graph_database_provider": self.graph_database_provider,
"graph_database_url": self.graph_database_url,
"graph_database_name": self.graph_database_name,
"graph_database_username": self.graph_database_username,
"graph_database_password": self.graph_database_password,
"graph_database_host": self.graph_database_host,
"graph_database_allow_anonymous": self.graph_database_allow_anonymous,
"graph_database_port": self.graph_database_port,
"graph_database_key": self.graph_database_key,
"graph_file_path": self.graph_file_path,
"graph_dataset_database_handler": self.graph_dataset_database_handler,
"graph_database_subprocess_enabled": self.graph_database_subprocess_enabled,
"kuzu_num_threads": self.kuzu_num_threads,
"kuzu_buffer_pool_size": self.kuzu_buffer_pool_size,
"kuzu_max_db_size": self.kuzu_max_db_size,
}
@lru_cache
def get_graph_config():
"""
Retrieve the graph configuration. This function utilizes caching to return a singleton
instance of the GraphConfig class for efficiency.
It creates and returns a GraphConfig object, which contains various settings related to
graph configuration.
Returns:
--------
- GraphConfig: A GraphConfig instance containing the graph configuration settings.
"""
return GraphConfig()
def get_graph_context_config():
"""This function will get the appropriate graph db config based on async context.
This allows the use of multiple graph databases for different threads, async tasks and parallelization
"""
from cognee.context_global_variables import graph_db_config
if graph_db_config.get():
return graph_db_config.get()
return get_graph_config().to_hashable_dict()

Xet Storage Details

Size:
8.65 kB
·
Xet hash:
93a94e7d1b79d47146c9bec3e7b26ecc218c5e19fa4c44d0f55e6bc3d89d96ce

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.