milvus_kwargs_configuration_demo.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. Example: Configuring Milvus Index Parameters via vector_db_storage_cls_kwargs
  3. This example demonstrates how to configure Milvus indexing parameters through
  4. vector_db_storage_cls_kwargs, which is the recommended approach when using
  5. frameworks that build on top of LightRAG (like RAGAnything).
  6. This approach allows configuration to be passed through framework layers without
  7. requiring environment variable changes or direct code modifications.
  8. """
  9. import os
  10. import asyncio
  11. from lightrag import LightRAG, QueryParam
  12. from lightrag.llm.openai import openai_complete_if_cache, openai_embed
  13. async def main():
  14. # Configure Milvus connection
  15. os.environ["MILVUS_URI"] = "http://localhost:19530"
  16. # os.environ["MILVUS_USER"] = "root"
  17. # os.environ["MILVUS_PASSWORD"] = "your_password"
  18. # os.environ["MILVUS_DB_NAME"] = "lightrag"
  19. # Initialize LightRAG with Milvus index configuration via vector_db_storage_cls_kwargs
  20. # This is the recommended approach for framework integration (e.g., RAGAnything)
  21. rag = LightRAG(
  22. working_dir="./demo_index",
  23. llm_model_func=openai_complete_if_cache,
  24. embedding_func=openai_embed,
  25. # Specify Milvus as the vector storage backend
  26. vector_storage="MilvusVectorDBStorage",
  27. # Configure Milvus indexing parameters via vector_db_storage_cls_kwargs
  28. # These parameters are extracted and passed to MilvusIndexConfig
  29. vector_db_storage_cls_kwargs={
  30. # Required parameter for all vector storage backends
  31. "cosine_better_than_threshold": 0.2,
  32. # Milvus index configuration parameters
  33. # All of these can be configured via vector_db_storage_cls_kwargs
  34. # Index type (AUTOINDEX, HNSW, HNSW_SQ, IVF_FLAT, etc.)
  35. "index_type": "HNSW",
  36. # Distance metric (COSINE, L2, IP)
  37. "metric_type": "COSINE",
  38. # HNSW parameters
  39. "hnsw_m": 32, # Number of connections per layer (2-2048)
  40. "hnsw_ef_construction": 256, # Size of dynamic candidate list during construction
  41. "hnsw_ef": 150, # Size of dynamic candidate list during search
  42. # IVF parameters (used when index_type is IVF_FLAT, IVF_SQ8, IVF_PQ)
  43. # "ivf_nlist": 2048, # Number of cluster units
  44. # "ivf_nprobe": 32, # Number of units to query
  45. # HNSW_SQ parameters (requires Milvus 2.6.8+)
  46. # "sq_type": "SQ8", # Quantization type (SQ4U, SQ6, SQ8, BF16, FP16)
  47. # "sq_refine": True, # Enable refinement
  48. # "sq_refine_type": "FP32", # Refinement type
  49. # "sq_refine_k": 20, # Number of candidates to refine
  50. },
  51. )
  52. # Initialize storage backends
  53. await rag.initialize_storages()
  54. print(
  55. "✅ LightRAG initialized with Milvus index configuration via vector_db_storage_cls_kwargs"
  56. )
  57. print(
  58. f" Index Type: {rag.vector_db_storages['entities'].index_config.index_type}"
  59. )
  60. print(
  61. f" Metric Type: {rag.vector_db_storages['entities'].index_config.metric_type}"
  62. )
  63. print(f" HNSW M: {rag.vector_db_storages['entities'].index_config.hnsw_m}")
  64. print(
  65. f" HNSW EF Construction: {rag.vector_db_storages['entities'].index_config.hnsw_ef_construction}"
  66. )
  67. print(f" HNSW EF: {rag.vector_db_storages['entities'].index_config.hnsw_ef}")
  68. # Example: Insert some text
  69. sample_text = """
  70. LightRAG is a Retrieval-Augmented Generation framework that uses graph-based
  71. knowledge representation for enhanced information retrieval. It supports multiple
  72. vector storage backends including Milvus, which offers advanced indexing options
  73. for optimal performance.
  74. """
  75. await rag.ainsert(sample_text)
  76. print("\n✅ Sample text inserted")
  77. # Example: Query with different modes
  78. result = await rag.aquery("What is LightRAG?", param=QueryParam(mode="hybrid"))
  79. print(f"\n✅ Query result: {result[:200]}...")
  80. # Cleanup
  81. await rag.finalize_storages()
  82. if __name__ == "__main__":
  83. print("=" * 80)
  84. print("Milvus Configuration via vector_db_storage_cls_kwargs Example")
  85. print("=" * 80)
  86. print()
  87. print("This example shows how to configure Milvus indexing parameters through")
  88. print("vector_db_storage_cls_kwargs, which is ideal for framework integration.")
  89. print()
  90. print("Key Benefits:")
  91. print(" • No environment variable changes required")
  92. print(" • Configuration can be passed through framework layers")
  93. print(" • Perfect for RAGAnything and similar frameworks")
  94. print(" • All 11 index parameters are supported")
  95. print()
  96. print("=" * 80)
  97. print()
  98. asyncio.run(main())