lightrag_openai_neo4j_milvus_redis_demo.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import asyncio
  3. from lightrag import LightRAG, QueryParam
  4. from lightrag.llm.ollama import ollama_embed, openai_complete_if_cache
  5. from lightrag.utils import EmbeddingFunc
  6. # WorkingDir
  7. ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
  8. WORKING_DIR = os.path.join(ROOT_DIR, "myKG")
  9. if not os.path.exists(WORKING_DIR):
  10. os.mkdir(WORKING_DIR)
  11. print(f"WorkingDir: {WORKING_DIR}")
  12. # redis
  13. os.environ["REDIS_URI"] = "redis://localhost:6379"
  14. # neo4j
  15. BATCH_SIZE_NODES = 500
  16. BATCH_SIZE_EDGES = 100
  17. os.environ["NEO4J_URI"] = "neo4j://localhost:7687"
  18. os.environ["NEO4J_USERNAME"] = "neo4j"
  19. os.environ["NEO4J_PASSWORD"] = "12345678"
  20. # milvus
  21. os.environ["MILVUS_URI"] = "http://localhost:19530"
  22. os.environ["MILVUS_USER"] = "root"
  23. os.environ["MILVUS_PASSWORD"] = "Milvus"
  24. os.environ["MILVUS_DB_NAME"] = "lightrag"
  25. async def llm_model_func(
  26. prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
  27. ) -> str:
  28. return await openai_complete_if_cache(
  29. "deepseek-chat",
  30. prompt,
  31. system_prompt=system_prompt,
  32. history_messages=history_messages,
  33. api_key="",
  34. base_url="",
  35. **kwargs,
  36. )
  37. embedding_func = EmbeddingFunc(
  38. embedding_dim=768,
  39. max_token_size=512,
  40. func=lambda texts: ollama_embed(
  41. texts, embed_model="shaw/dmeta-embedding-zh", host="http://117.50.173.35:11434"
  42. ),
  43. )
  44. async def initialize_rag():
  45. rag = LightRAG(
  46. working_dir=WORKING_DIR,
  47. llm_model_func=llm_model_func,
  48. summary_max_tokens=10000,
  49. embedding_func=embedding_func,
  50. chunk_token_size=512,
  51. chunk_overlap_token_size=256,
  52. kv_storage="RedisKVStorage",
  53. graph_storage="Neo4JStorage",
  54. vector_storage="MilvusVectorDBStorage",
  55. doc_status_storage="RedisKVStorage",
  56. )
  57. await rag.initialize_storages() # Auto-initializes pipeline_status
  58. return rag
  59. def main():
  60. # Initialize RAG instance
  61. rag = asyncio.run(initialize_rag())
  62. with open("./book.txt", "r", encoding="utf-8") as f:
  63. rag.insert(f.read())
  64. # Perform naive search
  65. print(
  66. rag.query(
  67. "What are the top themes in this story?", param=QueryParam(mode="naive")
  68. )
  69. )
  70. # Perform local search
  71. print(
  72. rag.query(
  73. "What are the top themes in this story?", param=QueryParam(mode="local")
  74. )
  75. )
  76. # Perform global search
  77. print(
  78. rag.query(
  79. "What are the top themes in this story?", param=QueryParam(mode="global")
  80. )
  81. )
  82. # Perform hybrid search
  83. print(
  84. rag.query(
  85. "What are the top themes in this story?", param=QueryParam(mode="hybrid")
  86. )
  87. )
  88. if __name__ == "__main__":
  89. main()