check_initialization.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python3
  2. """
  3. Diagnostic tool to check LightRAG initialization status.
  4. This tool helps developers verify that their LightRAG instance is properly
  5. initialized and ready to use. It should be called AFTER initialize_storages()
  6. to validate that all components are correctly set up.
  7. Usage:
  8. # Basic usage in your code:
  9. rag = LightRAG(...)
  10. await rag.initialize_storages()
  11. await check_lightrag_setup(rag, verbose=True)
  12. # Run demo from command line:
  13. python -m lightrag.tools.check_initialization --demo
  14. """
  15. import asyncio
  16. import sys
  17. from pathlib import Path
  18. # Add parent directory to path for imports
  19. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  20. from lightrag import LightRAG
  21. from lightrag.base import StoragesStatus
  22. async def check_lightrag_setup(rag_instance: LightRAG, verbose: bool = False) -> bool:
  23. """
  24. Check if a LightRAG instance is properly initialized.
  25. Args:
  26. rag_instance: The LightRAG instance to check
  27. verbose: If True, print detailed diagnostic information
  28. Returns:
  29. True if properly initialized, False otherwise
  30. """
  31. issues = []
  32. warnings = []
  33. print("🔍 Checking LightRAG initialization status...\n")
  34. # Check storage initialization status
  35. if not hasattr(rag_instance, "_storages_status"):
  36. issues.append("LightRAG instance missing _storages_status attribute")
  37. elif rag_instance._storages_status != StoragesStatus.INITIALIZED:
  38. issues.append(
  39. f"Storages not initialized (status: {rag_instance._storages_status.name})"
  40. )
  41. else:
  42. print("✅ Storage status: INITIALIZED")
  43. # Check individual storage components
  44. storage_components = [
  45. ("full_docs", "Document storage"),
  46. ("text_chunks", "Text chunks storage"),
  47. ("entities_vdb", "Entity vector database"),
  48. ("relationships_vdb", "Relationship vector database"),
  49. ("chunks_vdb", "Chunks vector database"),
  50. ("doc_status", "Document status tracker"),
  51. ("llm_response_cache", "LLM response cache"),
  52. ("full_entities", "Entity storage"),
  53. ("full_relations", "Relation storage"),
  54. ("chunk_entity_relation_graph", "Graph storage"),
  55. ]
  56. if verbose:
  57. print("\n📦 Storage Components:")
  58. for component, description in storage_components:
  59. if not hasattr(rag_instance, component):
  60. issues.append(f"Missing storage component: {component} ({description})")
  61. else:
  62. storage = getattr(rag_instance, component)
  63. if storage is None:
  64. warnings.append(f"Storage {component} is None (might be optional)")
  65. elif hasattr(storage, "_storage_lock"):
  66. if storage._storage_lock is None:
  67. issues.append(f"Storage {component} not initialized (lock is None)")
  68. elif verbose:
  69. print(f" ✅ {description}: Ready")
  70. elif verbose:
  71. print(f" ✅ {description}: Ready")
  72. # Check pipeline status
  73. try:
  74. from lightrag.kg.shared_storage import get_namespace_data
  75. get_namespace_data("pipeline_status", workspace=rag_instance.workspace)
  76. print("✅ Pipeline status: INITIALIZED")
  77. except KeyError:
  78. issues.append(
  79. "Pipeline status not initialized - call rag.initialize_storages() first"
  80. )
  81. except Exception as e:
  82. issues.append(f"Error checking pipeline status: {str(e)}")
  83. # Print results
  84. print("\n" + "=" * 50)
  85. if issues:
  86. print("❌ Issues found:\n")
  87. for issue in issues:
  88. print(f" • {issue}")
  89. print("\n📝 To fix, run this initialization sequence:\n")
  90. print(" await rag.initialize_storages()")
  91. print(
  92. "\n📚 Documentation: https://github.com/HKUDS/LightRAG#important-initialization-requirements"
  93. )
  94. if warnings and verbose:
  95. print("\n⚠️ Warnings (might be normal):")
  96. for warning in warnings:
  97. print(f" • {warning}")
  98. return False
  99. else:
  100. print("✅ LightRAG is properly initialized and ready to use!")
  101. if warnings and verbose:
  102. print("\n⚠️ Warnings (might be normal):")
  103. for warning in warnings:
  104. print(f" • {warning}")
  105. return True
  106. async def demo():
  107. """Demonstrate the diagnostic tool with a test instance."""
  108. from lightrag.llm.openai import openai_embed, gpt_4o_mini_complete
  109. print("=" * 50)
  110. print("LightRAG Initialization Diagnostic Tool")
  111. print("=" * 50)
  112. # Create test instance
  113. rag = LightRAG(
  114. working_dir="./test_diagnostic",
  115. embedding_func=openai_embed,
  116. llm_model_func=gpt_4o_mini_complete,
  117. )
  118. print("\n🔄 Initializing storages...\n")
  119. await rag.initialize_storages() # Auto-initializes pipeline_status
  120. print("\n🔍 Checking initialization status:\n")
  121. await check_lightrag_setup(rag, verbose=True)
  122. # Cleanup
  123. import shutil
  124. shutil.rmtree("./test_diagnostic", ignore_errors=True)
  125. if __name__ == "__main__":
  126. import argparse
  127. parser = argparse.ArgumentParser(description="Check LightRAG initialization status")
  128. parser.add_argument(
  129. "--demo", action="store_true", help="Run a demonstration with a test instance"
  130. )
  131. parser.add_argument(
  132. "--verbose",
  133. "-v",
  134. action="store_true",
  135. help="Show detailed diagnostic information",
  136. )
  137. args = parser.parse_args()
  138. if args.demo:
  139. asyncio.run(demo())
  140. else:
  141. print("Run with --demo to see the diagnostic tool in action")
  142. print("Or import this module and use check_lightrag_setup() with your instance")