| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | import os | 
					
						
							|  |  |  | from lightrag import LightRAG, QueryParam | 
					
						
							| 
									
										
										
										
											2025-02-20 10:22:26 +01:00
										 |  |  | from lightrag.llm.llama_index_impl import ( | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  |     llama_index_complete_if_cache, | 
					
						
							|  |  |  |     llama_index_embed, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | from lightrag.utils import EmbeddingFunc | 
					
						
							|  |  |  | from llama_index.llms.litellm import LiteLLM | 
					
						
							|  |  |  | from llama_index.embeddings.litellm import LiteLLMEmbedding | 
					
						
							|  |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  | import nest_asyncio | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | nest_asyncio.apply() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from lightrag.kg.shared_storage import initialize_pipeline_status | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Configure working directory | 
					
						
							| 
									
										
										
										
											2025-02-20 10:22:26 +01:00
										 |  |  | WORKING_DIR = "./index_default" | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | print(f"WORKING_DIR: {WORKING_DIR}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Model configuration | 
					
						
							| 
									
										
										
										
											2025-02-20 10:22:26 +01:00
										 |  |  | LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-4") | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | print(f"LLM_MODEL: {LLM_MODEL}") | 
					
						
							| 
									
										
										
										
											2025-02-20 10:22:26 +01:00
										 |  |  | EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-3-large") | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | print(f"EMBEDDING_MODEL: {EMBEDDING_MODEL}") | 
					
						
							|  |  |  | EMBEDDING_MAX_TOKEN_SIZE = int(os.environ.get("EMBEDDING_MAX_TOKEN_SIZE", 8192)) | 
					
						
							|  |  |  | print(f"EMBEDDING_MAX_TOKEN_SIZE: {EMBEDDING_MAX_TOKEN_SIZE}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # LiteLLM configuration | 
					
						
							|  |  |  | LITELLM_URL = os.environ.get("LITELLM_URL", "http://localhost:4000") | 
					
						
							|  |  |  | print(f"LITELLM_URL: {LITELLM_URL}") | 
					
						
							|  |  |  | LITELLM_KEY = os.environ.get("LITELLM_KEY", "sk-1234") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if not os.path.exists(WORKING_DIR): | 
					
						
							|  |  |  |     os.mkdir(WORKING_DIR) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | # Initialize LLM function | 
					
						
							|  |  |  | async def llm_model_func(prompt, system_prompt=None, history_messages=[], **kwargs): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         # Initialize LiteLLM if not in kwargs | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  |         if "llm_instance" not in kwargs: | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  |             llm_instance = LiteLLM( | 
					
						
							|  |  |  |                 model=f"openai/{LLM_MODEL}",  # Format: "provider/model_name" | 
					
						
							|  |  |  |                 api_base=LITELLM_URL, | 
					
						
							|  |  |  |                 api_key=LITELLM_KEY, | 
					
						
							|  |  |  |                 temperature=0.7, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  |             kwargs["llm_instance"] = llm_instance | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         response = await llama_index_complete_if_cache( | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  |             kwargs["llm_instance"], | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  |             prompt, | 
					
						
							|  |  |  |             system_prompt=system_prompt, | 
					
						
							|  |  |  |             history_messages=history_messages, | 
					
						
							|  |  |  |             **kwargs, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return response | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         print(f"LLM request failed: {str(e)}") | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | # Initialize embedding function | 
					
						
							|  |  |  | async def embedding_func(texts): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         embed_model = LiteLLMEmbedding( | 
					
						
							|  |  |  |             model_name=f"openai/{EMBEDDING_MODEL}", | 
					
						
							|  |  |  |             api_base=LITELLM_URL, | 
					
						
							|  |  |  |             api_key=LITELLM_KEY, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return await llama_index_embed(texts, embed_model=embed_model) | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         print(f"Embedding failed: {str(e)}") | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 14:54:12 +01:00
										 |  |  | # Get embedding dimension | 
					
						
							|  |  |  | async def get_embedding_dim(): | 
					
						
							|  |  |  |     test_text = ["This is a test sentence."] | 
					
						
							|  |  |  |     embedding = await embedding_func(test_text) | 
					
						
							|  |  |  |     embedding_dim = embedding.shape[1] | 
					
						
							|  |  |  |     print(f"embedding_dim={embedding_dim}") | 
					
						
							|  |  |  |     return embedding_dim | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 15:01:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  | async def initialize_rag(): | 
					
						
							|  |  |  |     embedding_dimension = await get_embedding_dim() | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  |     rag = LightRAG( | 
					
						
							|  |  |  |         working_dir=WORKING_DIR, | 
					
						
							|  |  |  |         llm_model_func=llm_model_func, | 
					
						
							|  |  |  |         embedding_func=EmbeddingFunc( | 
					
						
							|  |  |  |             embedding_dim=embedding_dimension, | 
					
						
							|  |  |  |             max_token_size=EMBEDDING_MAX_TOKEN_SIZE, | 
					
						
							|  |  |  |             func=embedding_func, | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await rag.initialize_storages() | 
					
						
							|  |  |  |     await initialize_pipeline_status() | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  |     return rag | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     # Initialize RAG instance | 
					
						
							|  |  |  |     rag = asyncio.run(initialize_rag()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Insert example text | 
					
						
							|  |  |  |     with open("./book.txt", "r", encoding="utf-8") as f: | 
					
						
							|  |  |  |         rag.insert(f.read()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test different query modes | 
					
						
							|  |  |  |     print("\nNaive Search:") | 
					
						
							|  |  |  |     print( | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  |         rag.query( | 
					
						
							|  |  |  |             "What are the top themes in this story?", param=QueryParam(mode="naive") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print("\nLocal Search:") | 
					
						
							|  |  |  |     print( | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  |         rag.query( | 
					
						
							|  |  |  |             "What are the top themes in this story?", param=QueryParam(mode="local") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print("\nGlobal Search:") | 
					
						
							|  |  |  |     print( | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  |         rag.query( | 
					
						
							|  |  |  |             "What are the top themes in this story?", param=QueryParam(mode="global") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print("\nHybrid Search:") | 
					
						
							|  |  |  |     print( | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  |         rag.query( | 
					
						
							|  |  |  |             "What are the top themes in this story?", param=QueryParam(mode="hybrid") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 18:40:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 18:33:42 +08:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     main() |