forked from tigergraph/graphrag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest_data.py
More file actions
260 lines (222 loc) · 9.89 KB
/
Copy pathingest_data.py
File metadata and controls
260 lines (222 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import requests
import time
import os
import json
import logging
import sys
from requests.auth import HTTPBasicAuth
# --- Configuration ---
API_BASE_URL = "http://127.0.0.1:80" # Proxied via Nginx
GRAPH_NAME = "cyber_graph" # Your target graph
USERNAME = "tigergraph"
PASSWORD = "tigergraph"
# Setup Logging and Encoding
if sys.stdout.encoding != 'utf-8':
try:
sys.stdout.reconfigure(encoding='utf-8')
except AttributeError:
# Fallback for older python versions if needed
pass
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class GraphRAGIngestor:
def __init__(self, base_url, graph_name, username, password):
self.base_url = base_url.rstrip('/')
self.graph_name = graph_name
self.auth = HTTPBasicAuth(username, password)
self.headers = {"Accept": "application/json"}
def create_graph(self):
"""
Step 0.1: Create the graph if it doesn't exist.
"""
url = f"{self.base_url}/ui/{self.graph_name}/create_graph"
logger.info(f"🔨 Creating graph: {self.graph_name}...")
try:
response = requests.post(url, auth=self.auth)
response.raise_for_status()
logger.info(f"✅ Graph created/verified: {response.json().get('message')}")
except Exception as e:
if "already exists" in str(e).lower():
logger.info(f"ℹ️ Graph {self.graph_name} already exists.")
else:
logger.error(f"❌ Graph Creation Failed: {e}")
raise
def initialize_graph(self):
"""
Step 0.2: Initialize the graph with GraphRAG schema and queries.
"""
url = f"{self.base_url}/ui/{self.graph_name}/initialize_graph"
logger.info(f"⚙️ Initializing graph schema for {self.graph_name}...")
try:
response = requests.post(url, auth=self.auth)
response.raise_for_status()
logger.info(f"✅ Graph initialized successfully.")
except Exception as e:
logger.error(f"❌ Graph Initialization Failed: {e}")
raise
def upload_file(self, file_path):
"""
PHASE 1: Upload raw file to the server's local storage.
Endpoint: /ui/{graphname}/uploads
Expected Response: JSON with status 'success' and list of uploaded files.
Failure Points: Disk full, permissions, or Nginx client_max_body_size exceeded.
"""
start_time = time.time()
url = f"{self.base_url}/ui/{self.graph_name}/uploads"
filename = os.path.basename(file_path)
logger.info(f"🚀 Phase 1: Uploading {filename}...")
try:
with open(file_path, 'rb') as f:
files = [('files', (filename, f, 'application/octet-stream'))]
# overwrite=true allows us to re-upload files during testing
response = requests.post(url, auth=self.auth, files=files, params={"overwrite": "true"})
response.raise_for_status()
duration = time.time() - start_time
logger.info(f"✅ Upload successful. Duration: {duration:.2f}s")
return duration
except Exception as e:
logger.error(f"❌ Phase 1 Failed: {e}")
raise
def create_ingest_job(self):
"""
PHASE 2: Pre-process uploaded files and prepare a TigerGraph Loading Job.
Endpoint: /ui/{graphname}/create_ingest
Expected Response: JSON containing 'load_job_id' and 'data_source_id' dict.
Failure Points: File format not supported, OCR errors (if PDF), or missing temp directories.
"""
start_time = time.time()
url = f"{self.base_url}/ui/{self.graph_name}/create_ingest"
logger.info(f"⚙️ Phase 2: Preparing ingestion config...")
# 'server' source points to the 'uploads' directory where Phase 1 saved files
payload = {
"data_source": "server",
"data_source_config": {
"data_path": f"uploads/{self.graph_name}"
},
"file_format": "multi" # Handles PDF, TXT, MD automatically
}
try:
response = requests.post(url, auth=self.auth, json=payload)
response.raise_for_status()
data = response.json()
duration = time.time() - start_time
logger.info(f"✅ Ingest config created. Duration: {duration:.2f}s")
return data, duration
except Exception as e:
logger.error(f"❌ Phase 2 Failed: {e}")
raise
def run_ingestion(self, ingest_config):
"""
PHASE 3: Trigger the actual load job into TigerGraph vertices.
Endpoint: /ui/{graphname}/ingest
Expected Response: Job status details (JobId, Log location).
Failure Points: TigerGraph GSQL server down, loading job not installed, or data corruption.
"""
start_time = time.time()
url = f"{self.base_url}/ui/{self.graph_name}/ingest"
logger.info(f"📥 Phase 3: Loading data into TigerGraph...")
# We pass the exact keys returned from Phase 2
payload = {
"load_job_id": ingest_config["load_job_id"],
"data_source_id": ingest_config["data_source_id"],
"file_path": "in_temp_storage"
}
try:
response = requests.post(url, auth=self.auth, json=payload)
response.raise_for_status()
duration = time.time() - start_time
logger.info(f"✅ Data loaded into vertices. Duration: {duration:.2f}s")
return duration
except Exception as e:
logger.error(f"❌ Phase 3 Failed: {e}")
raise
def rebuild_graph(self):
"""
PHASE 4: Trigger the GraphRAG extraction (Entities, Relationships, Communities).
Endpoint: /ui/{graphname}/rebuild_graph
Note: This is an ASYNC trigger.
Failure Points: LLM API keys invalid, Rate limits, or ECC service down.
"""
start_time = time.time()
url = f"{self.base_url}/ui/{self.graph_name}/rebuild_graph"
logger.info(f"🧠 Phase 4: Triggering GraphRAG Rebuild (Extraction)...")
try:
response = requests.post(url, auth=self.auth)
response.raise_for_status()
logger.info("✅ Rebuild triggered successfully.")
return start_time
except Exception as e:
logger.error(f"❌ Phase 4 Trigger Failed: {e}")
raise
def monitor_rebuild(self, trigger_time):
"""
Monitoring: Poll the status until the GraphRAG extraction is complete.
Endpoint: /ui/{graphname}/rebuild_status
"""
url = f"{self.base_url}/ui/{self.graph_name}/rebuild_status"
logger.info("⏳ Monitoring extraction progress (this may take a few minutes)...")
while True:
try:
response = requests.get(url, auth=self.auth)
response.raise_for_status()
status_data = response.json()
is_running = status_data.get("is_running", False)
status = status_data.get("status", "unknown")
if not is_running and status in ["completed", "failed", "idle"]:
duration = time.time() - trigger_time
if status == "completed":
logger.info(f"🎊 GraphRAG Rebuild Complete! Total Rebuild Time: {duration:.2f}s")
else:
logger.warning(f"⚠️ Rebuild finished with status: {status}")
return duration
logger.info(f" - Current Status: {status}...")
time.sleep(10) # Poll every 10 seconds
except Exception as e:
logger.warning(f" - Status check failed (retrying): {e}")
time.sleep(5)
def run_full_ingestion(file_path):
ingestor = GraphRAGIngestor(API_BASE_URL, GRAPH_NAME, USERNAME, PASSWORD)
print("\n" + "="*50)
print(f"🛠️ STARTING INGESTION FOR: {os.path.basename(file_path)}")
print("="*50 + "\n")
max_retries = 3
retry_delay = 10
for attempt in range(max_retries):
try:
# ingestor.create_graph()
# ingestor.initialize_graph()
break
except Exception as e:
if attempt < max_retries - 1:
logger.warning(f"⚠️ Initial steps failed (Attempt {attempt+1}/{max_retries}). Retrying in {retry_delay}s... Error: {e}")
time.sleep(retry_delay)
else:
print(f"❌ CRITICAL FAILURE: Failed after {max_retries} attempts. {e}")
return
try:
t1 = ingestor.upload_file(file_path)
config, t2 = ingestor.create_ingest_job()
t3 = ingestor.run_ingestion(config)
# Rebuild phase
rebuild_start = ingestor.rebuild_graph()
t4 = ingestor.monitor_rebuild(rebuild_start)
total_ingest = t1 + t2 + t3
print("\n" + "="*50)
print("🏁 FINAL METRICS")
print("-" * 50)
print(f"📊 Total Ingestion Duration: {total_ingest:.2f}s")
print(f"🧠 Total Rebuild Duration: {t4:.2f}s")
print(f"✨ OVERALL SUCCESS: {os.path.basename(file_path)} is now in the Graph!")
print("="*50 + "\n")
except Exception as e:
print("\n" + "!"*50)
print(f"❌ CRITICAL FAILURE: {e}")
print("!"*50 + "\n")
if __name__ == "__main__":
# Example usage
test_file = "cybersecurity_test.txt"
if os.path.exists(test_file):
run_full_ingestion(test_file)
else:
print(f"❌ Error: Please create '{test_file}' before running.")
print(" Quick Fix: echo 'APT-28 Malware Report...' > threat_report.txt")