| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from lightrag import LightRAG, QueryParam | 
					
						
							| 
									
										
										
										
											2025-01-25 00:11:00 +01:00
										 |  |  | from lightrag.llm.hf import hf_model_complete, hf_embed | 
					
						
							| 
									
										
										
										
											2024-10-15 21:11:12 +08:00
										 |  |  | from lightrag.utils import EmbeddingFunc | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  | from transformers import AutoModel, AutoTokenizer | 
					
						
							| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | WORKING_DIR = "./dickens" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if not os.path.exists(WORKING_DIR): | 
					
						
							|  |  |  |     os.mkdir(WORKING_DIR) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | rag = LightRAG( | 
					
						
							|  |  |  |     working_dir=WORKING_DIR, | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  |     llm_model_func=hf_model_complete, | 
					
						
							|  |  |  |     llm_model_name="meta-llama/Llama-3.1-8B-Instruct", | 
					
						
							| 
									
										
										
										
											2024-10-15 21:11:12 +08:00
										 |  |  |     embedding_func=EmbeddingFunc( | 
					
						
							|  |  |  |         embedding_dim=384, | 
					
						
							|  |  |  |         max_token_size=5000, | 
					
						
							| 
									
										
										
										
											2025-01-25 00:11:00 +01:00
										 |  |  |         func=lambda texts: hf_embed( | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  |             texts, | 
					
						
							|  |  |  |             tokenizer=AutoTokenizer.from_pretrained( | 
					
						
							|  |  |  |                 "sentence-transformers/all-MiniLM-L6-v2" | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             embed_model=AutoModel.from_pretrained( | 
					
						
							|  |  |  |                 "sentence-transformers/all-MiniLM-L6-v2" | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-10-15 21:11:12 +08:00
										 |  |  |     ), | 
					
						
							| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 16:01:40 +08:00
										 |  |  | with open("./book.txt", "r", encoding="utf-8") as f: | 
					
						
							| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  |     rag.insert(f.read()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Perform naive search | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="naive")) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Perform local search | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="local")) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Perform global search | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="global")) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-15 19:40:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Perform hybrid search | 
					
						
							| 
									
										
										
										
											2024-10-19 09:43:17 +05:30
										 |  |  | print( | 
					
						
							|  |  |  |     rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid")) | 
					
						
							|  |  |  | ) |