LightRAG/reproduce/Step_1.py

52 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-10-11 15:16:43 +08:00
import os
import json
import time
2025-03-03 18:33:42 +08:00
import asyncio
2024-10-11 15:16:43 +08:00
from lightrag import LightRAG
2025-03-03 18:33:42 +08:00
from lightrag.kg.shared_storage import initialize_pipeline_status
2024-10-11 15:16:43 +08:00
2024-10-11 15:16:43 +08:00
def insert_text(rag, file_path):
with open(file_path, mode="r") as f:
2024-10-11 15:16:43 +08:00
unique_contexts = json.load(f)
2024-10-11 15:16:43 +08:00
retries = 0
max_retries = 3
while retries < max_retries:
try:
rag.insert(unique_contexts)
break
except Exception as e:
retries += 1
print(f"Insertion failed, retrying ({retries}/{max_retries}), error: {e}")
time.sleep(10)
if retries == max_retries:
print("Insertion failed after exceeding the maximum number of retries")
2024-10-11 15:16:43 +08:00
cls = "agriculture"
WORKING_DIR = f"../{cls}"
2024-10-11 15:16:43 +08:00
if not os.path.exists(WORKING_DIR):
os.mkdir(WORKING_DIR)
2025-03-03 18:40:03 +08:00
2025-03-03 18:33:42 +08:00
async def initialize_rag():
rag = LightRAG(working_dir=WORKING_DIR)
2024-10-11 15:16:43 +08:00
2025-03-03 18:33:42 +08:00
await rag.initialize_storages()
await initialize_pipeline_status()
return rag
2025-03-03 18:40:03 +08:00
2025-03-03 18:33:42 +08:00
def main():
# Initialize RAG instance
rag = asyncio.run(initialize_rag())
insert_text(rag, f"../datasets/unique_contexts/{cls}_unique_contexts.json")
if __name__ == "__main__":
main()