import os import asyncio from lightrag import LightRAG, QueryParam from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed from lightrag.kg.shared_storage import initialize_pipeline_status WORKING_DIR = "./dickens" if not os.path.exists(WORKING_DIR): os.mkdir(WORKING_DIR) async def initialize_rag(): rag = LightRAG( working_dir=WORKING_DIR, embedding_func=openai_embed, llm_model_func=gpt_4o_mini_complete, # llm_model_func=gpt_4o_complete ) await rag.initialize_storages() await initialize_pipeline_status() return rag async def main(): # Initialize RAG instance rag = await initialize_rag() with open("./book.txt", "r", encoding="utf-8") as f: await rag.ainsert(f.read()) # Perform naive search print( await rag.aquery( "What are the top themes in this story?", param=QueryParam(mode="naive") ) ) # Perform local search print( await rag.aquery( "What are the top themes in this story?", param=QueryParam(mode="local") ) ) # Perform global search print( await rag.aquery( "What are the top themes in this story?", param=QueryParam(mode="global") ) ) # Perform hybrid search print( await rag.aquery( "What are the top themes in this story?", param=QueryParam(mode="hybrid") ) ) if __name__ == "__main__": asyncio.run(main())