factory.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Storage backend class factory.
  2. Resolves a storage backend name (e.g. ``"JsonKVStorage"``) to its concrete
  3. implementation class. The four default backends are imported directly so
  4. they always work without depending on the ``STORAGES`` registry; everything
  5. else is resolved lazily through the registry.
  6. """
  7. from __future__ import annotations
  8. import importlib
  9. from typing import Any, Callable
  10. from lightrag.kg import STORAGES
  11. def get_storage_class(storage_name: str) -> Callable[..., Any]:
  12. """Return the storage backend class for ``storage_name``."""
  13. if storage_name == "JsonKVStorage":
  14. from lightrag.kg.json_kv_impl import JsonKVStorage
  15. return JsonKVStorage
  16. if storage_name == "NanoVectorDBStorage":
  17. from lightrag.kg.nano_vector_db_impl import NanoVectorDBStorage
  18. return NanoVectorDBStorage
  19. if storage_name == "NetworkXStorage":
  20. from lightrag.kg.networkx_impl import NetworkXStorage
  21. return NetworkXStorage
  22. if storage_name == "JsonDocStatusStorage":
  23. from lightrag.kg.json_doc_status_impl import JsonDocStatusStorage
  24. return JsonDocStatusStorage
  25. # Fallback to dynamic import for other storage implementations.
  26. # STORAGES values are relative paths (e.g. ".kg.postgres_impl") authored
  27. # against the top-level ``lightrag`` package, so anchor the import there
  28. # rather than letting it resolve against this module's own package.
  29. import_path = STORAGES[storage_name]
  30. module = importlib.import_module(import_path, package="lightrag")
  31. return getattr(module, storage_name)