AbdulMoid commited on
Commit
58d53e4
·
verified ·
1 Parent(s): 9e16f37

Update graph_rag.py

Browse files
Files changed (1) hide show
  1. graph_rag.py +25 -22
graph_rag.py CHANGED
@@ -13,28 +13,36 @@ logger = logging.getLogger(__name__)
13
  load_dotenv()
14
 
15
  def unzip_folder(zip_path, extract_path):
16
- output_dir = os.path.join(extract_path, "ragtest")
17
- os.makedirs(output_dir, exist_ok=True)
18
-
19
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
20
- zip_ref.extractall(output_dir)
21
 
22
- logger.info(f"Extracted contents to {output_dir}")
23
- logger.info(f"Contents of {output_dir}:")
24
- for file in os.listdir(output_dir):
25
- logger.info(os.path.join(output_dir, file))
 
26
 
27
- return output_dir
 
 
 
 
 
 
28
 
29
- def run_graphrag_query(query):
 
 
30
  # Define the command
31
  command = [
32
  "python", "-m", "graphrag.query",
33
- "--root", "./ragtest",
34
- "--method", "local",
35
  query
36
  ]
37
 
 
 
38
  # Run the command
39
  result = subprocess.run(command, capture_output=True, text=True)
40
 
@@ -42,22 +50,20 @@ def run_graphrag_query(query):
42
  if result.returncode == 0:
43
  return result.stdout
44
  else:
 
45
  return result.stderr
46
 
47
  def qa_tool_graph_rag(user_question):
48
  try:
49
  # Paths from environment variables
50
  zip_path = os.getenv('ZIP_PATH', '/home/user/app/ragtest.zip')
51
- extract_path = os.getenv('EXTRACT_PATH', '/home/user/app')
52
 
53
  # Unzip the folder
54
  output_dir = unzip_folder(zip_path, extract_path)
55
 
56
- # Change to the directory containing the ragtest folder
57
- os.chdir(extract_path)
58
-
59
  # Run the GraphRAG query
60
- answer = run_graphrag_query(user_question)
61
 
62
  logger.info(f"GraphRAG answer generated: {answer}")
63
 
@@ -73,8 +79,5 @@ def qa_tool_graph_rag(user_question):
73
 
74
  finally:
75
  # Clean up: remove the extracted directory
76
- if 'output_dir' in locals():
77
- shutil.rmtree(output_dir)
78
-
79
- # Change back to the original directory
80
- os.chdir('/home/user/app')
 
13
  load_dotenv()
14
 
15
  def unzip_folder(zip_path, extract_path):
 
 
 
16
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
17
+ zip_ref.extractall(extract_path)
18
 
19
+ logger.info(f"Extracted contents to {extract_path}")
20
+ logger.info(f"Contents of {extract_path}:")
21
+ for root, dirs, files in os.walk(extract_path):
22
+ for file in files:
23
+ logger.info(os.path.join(root, file))
24
 
25
+ return extract_path
26
+
27
+ def find_data_dir(root_dir):
28
+ for root, dirs, files in os.walk(root_dir):
29
+ if 'output' in dirs:
30
+ return os.path.join(root, 'output')
31
+ return root_dir
32
 
33
+ def run_graphrag_query(query, root_dir):
34
+ data_dir = find_data_dir(root_dir)
35
+
36
  # Define the command
37
  command = [
38
  "python", "-m", "graphrag.query",
39
+ "--root", data_dir,
40
+ "--method", "global",
41
  query
42
  ]
43
 
44
+ logger.info(f"Running command: {' '.join(command)}")
45
+
46
  # Run the command
47
  result = subprocess.run(command, capture_output=True, text=True)
48
 
 
50
  if result.returncode == 0:
51
  return result.stdout
52
  else:
53
+ logger.error(f"GraphRAG error: {result.stderr}")
54
  return result.stderr
55
 
56
  def qa_tool_graph_rag(user_question):
57
  try:
58
  # Paths from environment variables
59
  zip_path = os.getenv('ZIP_PATH', '/home/user/app/ragtest.zip')
60
+ extract_path = os.getenv('EXTRACT_PATH', '/home/user/app/ragtest')
61
 
62
  # Unzip the folder
63
  output_dir = unzip_folder(zip_path, extract_path)
64
 
 
 
 
65
  # Run the GraphRAG query
66
+ answer = run_graphrag_query(user_question, output_dir)
67
 
68
  logger.info(f"GraphRAG answer generated: {answer}")
69
 
 
79
 
80
  finally:
81
  # Clean up: remove the extracted directory
82
+ if os.path.exists(extract_path):
83
+ shutil.rmtree(extract_path)