1+ import os
2+ import time
3+ from lightrag import LightRAG , QueryParam
4+ from lightrag .llm import ollama_model_complete , ollama_embedding
5+ from lightrag .utils import EmbeddingFunc
6+
7+ # 工作目录和文本文件目录路径
8+ WORKING_DIR = "./dickens"
9+ TEXT_FILES_DIR = "/llm/mt"
10+
11+ # 如果工作目录不存在,则创建该目录
12+ if not os .path .exists (WORKING_DIR ):
13+ os .mkdir (WORKING_DIR )
14+
15+ # 初始化 LightRAG
16+ rag = LightRAG (
17+ working_dir = WORKING_DIR ,
18+ llm_model_func = ollama_model_complete ,
19+ llm_model_name = "qwen2.5:3b-instruct-max-context" ,
20+ embedding_func = EmbeddingFunc (
21+ embedding_dim = 768 ,
22+ max_token_size = 8192 ,
23+ func = lambda texts : ollama_embedding (texts , embed_model = "nomic-embed-text" ),
24+ ),
25+ )
26+
27+ # 读取 TEXT_FILES_DIR 目录下所有的 .txt 文件
28+ texts = []
29+ for filename in os .listdir (TEXT_FILES_DIR ):
30+ if filename .endswith ('.txt' ):
31+ file_path = os .path .join (TEXT_FILES_DIR , filename )
32+ with open (file_path , 'r' , encoding = 'utf-8' ) as file :
33+ texts .append (file .read ())
34+
35+ # 批量插入文本到 LightRAG,带有重试机制
36+ def insert_texts_with_retry (rag , texts , retries = 3 , delay = 5 ):
37+ for _ in range (retries ):
38+ try :
39+ rag .insert (texts )
40+ return
41+ except Exception as e :
42+ print (f"Error occurred during insertion: { e } . Retrying in { delay } seconds..." )
43+ time .sleep (delay )
44+ raise RuntimeError ("Failed to insert texts after multiple retries." )
45+
46+ insert_texts_with_retry (rag , texts )
47+
48+ # 执行不同类型的查询,并处理潜在的错误
49+ try :
50+ print (rag .query ("What are the top themes in this story?" , param = QueryParam (mode = "naive" )))
51+ except Exception as e :
52+ print (f"Error performing naive search: { e } " )
53+
54+ try :
55+ print (rag .query ("What are the top themes in this story?" , param = QueryParam (mode = "local" )))
56+ except Exception as e :
57+ print (f"Error performing local search: { e } " )
58+
59+ try :
60+ print (rag .query ("What are the top themes in this story?" , param = QueryParam (mode = "global" )))
61+ except Exception as e :
62+ print (f"Error performing global search: { e } " )
63+
64+ try :
65+ print (rag .query ("What are the top themes in this story?" , param = QueryParam (mode = "hybrid" )))
66+ except Exception as e :
67+ print (f"Error performing hybrid search: { e } " )
68+
69+ # 清理 VRAM 资源的函数
70+ def clear_vram ():
71+ os .system ("sudo nvidia-smi --gpu-reset" )
72+
73+ # 定期清理 VRAM 以防止溢出
74+ clear_vram_interval = 3600 # 每小时清理一次
75+ start_time = time .time ()
76+
77+ while True :
78+ current_time = time .time ()
79+ if current_time - start_time > clear_vram_interval :
80+ clear_vram ()
81+ start_time = current_time
82+ time .sleep (60 ) # 每分钟检查一次时间
0 commit comments