| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | import os | 
					
						
							|  |  |  | from lightrag import LightRAG, QueryParam | 
					
						
							| 
									
										
										
										
											2025-01-25 00:11:00 +01:00
										 |  |  | from lightrag.llm.openai import gpt_4o_mini_complete | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | ######### | 
					
						
							|  |  |  | # Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert() | 
					
						
							| 
									
										
										
										
											2024-11-06 11:18:14 -05:00
										 |  |  | # import nest_asyncio | 
					
						
							|  |  |  | # nest_asyncio.apply() | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | ######### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-01 16:29:36 -04:00
										 |  |  | WORKING_DIR = "./dickens" | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | if not os.path.exists(WORKING_DIR): | 
					
						
							|  |  |  |     os.mkdir(WORKING_DIR) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | rag = LightRAG( | 
					
						
							|  |  |  |     working_dir=WORKING_DIR, | 
					
						
							| 
									
										
										
										
											2024-11-06 11:18:14 -05:00
										 |  |  |     llm_model_func=gpt_4o_mini_complete,  # Use gpt_4o_mini_complete LLM model | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  |     # llm_model_func=gpt_4o_complete  # Optionally, use a stronger model | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-11 15:19:42 +08:00
										 |  |  | with open("./dickens/book.txt", "r", encoding="utf-8") as f: | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  |     rag.insert(f.read()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Perform naive search | 
					
						
							| 
									
										
										
										
											2024-11-06 11:18:14 -05:00
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="naive")) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Perform local search | 
					
						
							| 
									
										
										
										
											2024-11-06 11:18:14 -05:00
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="local")) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Perform global search | 
					
						
							| 
									
										
										
										
											2024-11-06 11:18:14 -05:00
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="global")) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-27 15:37:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Perform hybrid search | 
					
						
							| 
									
										
										
										
											2024-11-06 11:18:14 -05:00
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid")) | 
					
						
							|  |  |  | ) |