-
Notifications
You must be signed in to change notification settings - Fork 575
/
Copy pathgraphDB_dataAccess.py
437 lines (388 loc) · 21.8 KB
/
graphDB_dataAccess.py
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import logging
import os
from datetime import datetime
from langchain_community.graphs import Neo4jGraph
from src.shared.common_fn import create_gcs_bucket_folder_name_hashed, delete_uploaded_local_file, load_embedding_model
from src.document_sources.gcs_bucket import delete_file_from_gcs
from src.shared.constants import BUCKET_UPLOAD
from src.entities.source_node import sourceNode
from src.communities import MAX_COMMUNITY_LEVELS
import json
from dotenv import load_dotenv
load_dotenv()
class graphDBdataAccess:
def __init__(self, graph: Neo4jGraph):
self.graph = graph
def update_exception_db(self, file_name, exp_msg):
try:
job_status = "Failed"
result = self.get_current_status_document_node(file_name)
is_cancelled_status = result[0]['is_cancelled']
if bool(is_cancelled_status) == True:
job_status = 'Cancelled'
self.graph.query("""MERGE(d:Document {fileName :$fName}) SET d.status = $status, d.errorMessage = $error_msg""",
{"fName":file_name, "status":job_status, "error_msg":exp_msg})
except Exception as e:
error_message = str(e)
logging.error(f"Error in updating document node status as failed: {error_message}")
raise Exception(error_message)
def create_source_node(self, obj_source_node:sourceNode):
try:
job_status = "New"
logging.info("creating source node if does not exist")
self.graph.query("""MERGE(d:Document {fileName :$fn}) SET d.fileSize = $fs, d.fileType = $ft ,
d.status = $st, d.url = $url, d.awsAccessKeyId = $awsacc_key_id,
d.fileSource = $f_source, d.createdAt = $c_at, d.updatedAt = $u_at,
d.processingTime = $pt, d.errorMessage = $e_message, d.nodeCount= $n_count,
d.relationshipCount = $r_count, d.model= $model, d.gcsBucket=$gcs_bucket,
d.gcsBucketFolder= $gcs_bucket_folder, d.language= $language,d.gcsProjectId= $gcs_project_id,
d.is_cancelled=False, d.total_chunks=0, d.processed_chunk=0,
d.access_token=$access_token""",
{"fn":obj_source_node.file_name, "fs":obj_source_node.file_size, "ft":obj_source_node.file_type, "st":job_status,
"url":obj_source_node.url,
"awsacc_key_id":obj_source_node.awsAccessKeyId, "f_source":obj_source_node.file_source, "c_at":obj_source_node.created_at,
"u_at":obj_source_node.created_at, "pt":0, "e_message":'', "n_count":0, "r_count":0, "model":obj_source_node.model,
"gcs_bucket": obj_source_node.gcsBucket, "gcs_bucket_folder": obj_source_node.gcsBucketFolder,
"language":obj_source_node.language, "gcs_project_id":obj_source_node.gcsProjectId,
"access_token":obj_source_node.access_token})
except Exception as e:
error_message = str(e)
logging.info(f"error_message = {error_message}")
self.update_exception_db(self, obj_source_node.file_name, error_message)
raise Exception(error_message)
def update_source_node(self, obj_source_node:sourceNode):
try:
params = {}
if obj_source_node.file_name is not None and obj_source_node.file_name != '':
params['fileName'] = obj_source_node.file_name
if obj_source_node.status is not None and obj_source_node.status != '':
params['status'] = obj_source_node.status
if obj_source_node.created_at is not None:
params['createdAt'] = obj_source_node.created_at
if obj_source_node.updated_at is not None:
params['updatedAt'] = obj_source_node.updated_at
if obj_source_node.processing_time is not None and obj_source_node.processing_time != 0:
params['processingTime'] = round(obj_source_node.processing_time.total_seconds(),2)
if obj_source_node.node_count is not None :
params['nodeCount'] = obj_source_node.node_count
if obj_source_node.relationship_count is not None :
params['relationshipCount'] = obj_source_node.relationship_count
if obj_source_node.model is not None and obj_source_node.model != '':
params['model'] = obj_source_node.model
if obj_source_node.total_chunks is not None and obj_source_node.total_chunks != 0:
params['total_chunks'] = obj_source_node.total_chunks
if obj_source_node.is_cancelled is not None:
params['is_cancelled'] = obj_source_node.is_cancelled
if obj_source_node.processed_chunk is not None :
params['processed_chunk'] = obj_source_node.processed_chunk
if obj_source_node.retry_condition is not None :
params['retry_condition'] = obj_source_node.retry_condition
param= {"props":params}
print(f'Base Param value 1 : {param}')
query = "MERGE(d:Document {fileName :$props.fileName}) SET d += $props"
logging.info("Update source node properties")
self.graph.query(query,param)
except Exception as e:
error_message = str(e)
self.update_exception_db(self.file_name,error_message)
raise Exception(error_message)
def get_source_list(self):
"""
Args:
uri: URI of the graph to extract
db_name: db_name is database name to connect to graph db
userName: Username to use for graph creation ( if None will use username from config file )
password: Password to use for graph creation ( if None will use password from config file )
file: File object containing the PDF file to be used
model: Type of model to use ('Diffbot'or'OpenAI GPT')
Returns:
Returns a list of sources that are in the database by querying the graph and
sorting the list by the last updated date.
"""
logging.info("Get existing files list from graph")
query = "MATCH(d:Document) WHERE d.fileName IS NOT NULL RETURN d ORDER BY d.updatedAt DESC"
result = self.graph.query(query)
list_of_json_objects = [entry['d'] for entry in result]
return list_of_json_objects
def update_KNN_graph(self):
"""
Update the graph node with SIMILAR relationship where embedding scrore match
"""
index = self.graph.query("""show indexes yield * where type = 'VECTOR' and name = 'vector'""")
# logging.info(f'show index vector: {index}')
knn_min_score = os.environ.get('KNN_MIN_SCORE')
if len(index) > 0:
logging.info('update KNN graph')
self.graph.query("""MATCH (c:Chunk)
WHERE c.embedding IS NOT NULL AND count { (c)-[:SIMILAR]-() } < 5
CALL db.index.vector.queryNodes('vector', 6, c.embedding) yield node, score
WHERE node <> c and score >= $score MERGE (c)-[rel:SIMILAR]-(node) SET rel.score = score
""",
{"score":float(knn_min_score)}
)
else:
logging.info("Vector index does not exist, So KNN graph not update")
def check_account_access(self, database):
query = """
SHOW USER PRIVILEGES
YIELD *
WHERE graph = $database AND action IN ['read']
RETURN COUNT(*) AS readAccessCount
"""
try:
logging.info(f"Checking access for database: {database}")
result = self.graph.query(query, params={"database": database})
read_access_count = result[0]["readAccessCount"] if result else 0
logging.info(f"Read access count: {read_access_count}")
if read_access_count > 0:
logging.info("The account has read access.")
return False
else:
logging.info("The account has write access.")
return True
except Exception as e:
logging.error(f"Error checking account access: {e}")
return False
def check_gds_version(self):
try:
gds_procedure_count = """
SHOW PROCEDURES
YIELD name
WHERE name STARTS WITH "gds."
RETURN COUNT(*) AS totalGdsProcedures
"""
result = self.graph.query(gds_procedure_count)
total_gds_procedures = result[0]['totalGdsProcedures'] if result else 0
enable_communities = os.environ.get('ENABLE_COMMUNITIES','').upper() == "TRUE"
logging.info(f"Enable Communities {enable_communities}")
if enable_communities and total_gds_procedures > 0:
logging.info("GDS is available in the database.")
return True
else:
logging.info("Communities are disabled or GDS is not available in the database.")
return False
except Exception as e:
logging.error(f"An error occurred while checking GDS version: {e}")
return False
def connection_check_and_get_vector_dimensions(self,database):
"""
Get the vector index dimension from database and application configuration and DB connection status
Args:
uri: URI of the graph to extract
userName: Username to use for graph creation ( if None will use username from config file )
password: Password to use for graph creation ( if None will use password from config file )
db_name: db_name is database name to connect to graph db
Returns:
Returns a status of connection from NEO4j is success or failure
"""
db_vector_dimension = self.graph.query("""SHOW INDEXES YIELD *
WHERE type = 'VECTOR' AND name = 'vector'
RETURN options.indexConfig['vector.dimensions'] AS vector_dimensions
""")
result_chunks = self.graph.query("""match (c:Chunk) return size(c.embedding) as embeddingSize, count(*) as chunks,
count(c.embedding) as hasEmbedding
""")
embedding_model = os.getenv('EMBEDDING_MODEL')
embeddings, application_dimension = load_embedding_model(embedding_model)
logging.info(f'embedding model:{embeddings} and dimesion:{application_dimension}')
gds_status = self.check_gds_version()
write_access = self.check_account_access(database=database)
if self.graph:
if len(db_vector_dimension) > 0:
return {'db_vector_dimension': db_vector_dimension[0]['vector_dimensions'], 'application_dimension':application_dimension, 'message':"Connection Successful","gds_status":gds_status,"write_access":write_access}
else:
if len(db_vector_dimension) == 0 and len(result_chunks) == 0:
logging.info("Chunks and vector index does not exists in database")
return {'db_vector_dimension': 0, 'application_dimension':application_dimension, 'message':"Connection Successful","chunks_exists":False,"gds_status":gds_status,"write_access":write_access}
elif len(db_vector_dimension) == 0 and result_chunks[0]['hasEmbedding']==0 and result_chunks[0]['chunks'] > 0:
return {'db_vector_dimension': 0, 'application_dimension':application_dimension, 'message':"Connection Successful","chunks_exists":True,"gds_status":gds_status,"write_access":write_access}
else:
return {'message':"Connection Successful","gds_status": gds_status,"write_access":write_access}
def execute_query(self, query, param=None):
return self.graph.query(query, param)
def get_current_status_document_node(self, file_name):
query = """
MATCH(d:Document {fileName : $file_name}) RETURN d.status AS Status , d.processingTime AS processingTime,
d.nodeCount AS nodeCount, d.model as model, d.relationshipCount as relationshipCount,
d.total_chunks AS total_chunks , d.fileSize as fileSize,
d.is_cancelled as is_cancelled, d.processed_chunk as processed_chunk, d.fileSource as fileSource
"""
param = {"file_name" : file_name}
return self.execute_query(query, param)
def delete_file_from_graph(self, filenames, source_types, deleteEntities:str, merged_dir:str, uri):
# filename_list = filenames.split(',')
filename_list= list(map(str.strip, json.loads(filenames)))
source_types_list= list(map(str.strip, json.loads(source_types)))
gcs_file_cache = os.environ.get('GCS_FILE_CACHE')
# source_types_list = source_types.split(',')
for (file_name,source_type) in zip(filename_list, source_types_list):
merged_file_path = os.path.join(merged_dir, file_name)
if source_type == 'local file' and gcs_file_cache == 'True':
folder_name = create_gcs_bucket_folder_name_hashed(uri, file_name)
delete_file_from_gcs(BUCKET_UPLOAD,folder_name,file_name)
else:
logging.info(f'Deleted File Path: {merged_file_path} and Deleted File Name : {file_name}')
delete_uploaded_local_file(merged_file_path,file_name)
query_to_delete_document="""
MATCH (d:Document) where d.fileName in $filename_list and d.fileSource in $source_types_list
with collect(d) as documents
unwind documents as d
optional match (d)<-[:PART_OF]-(c:Chunk)
detach delete c, d
return count(*) as deletedChunks
"""
query_to_delete_document_and_entities="""
match (d:Document) where d.fileName IN $filename_list and d.fileSource in $source_types_list
detach delete d
with collect(d) as documents
unwind documents as d
match (d)<-[:PART_OF]-(c:Chunk)
detach delete c
with *
match (c)-[:HAS_ENTITY]->(e)
where not exists { (e)<-[:HAS_ENTITY]-()-[:PART_OF]->(d2) where not d2 in documents }
detach delete e
"""
query_to_delete_communities = """
MATCH (c:`__Community__`)
WHERE NOT EXISTS { ()-[:IN_COMMUNITY]->(c) } AND c.level = 0
DETACH DELETE c
WITH *
UNWIND range(1, $max_level) AS level
MATCH (c:`__Community__`)
WHERE c.level = level AND NOT EXISTS { (c)<-[:PARENT_COMMUNITY]-(child) }
DETACH DELETE c
"""
param = {"filename_list" : filename_list, "source_types_list": source_types_list}
community_param = {"max_level":MAX_COMMUNITY_LEVELS}
if deleteEntities == "true":
result = self.execute_query(query_to_delete_document_and_entities, param)
_ = self.execute_query(query_to_delete_communities,community_param)
logging.info(f"Deleting {len(filename_list)} documents = '{filename_list}' from '{source_types_list}' from database")
else :
result = self.execute_query(query_to_delete_document, param)
logging.info(f"Deleting {len(filename_list)} documents = '{filename_list}' from '{source_types_list}' with their entities from database")
return result, len(filename_list)
def list_unconnected_nodes(self):
query = """
MATCH (e:!Chunk&!Document&!`__Community__`)
WHERE NOT exists { (e)--(:!Chunk&!Document&!`__Community__`) }
OPTIONAL MATCH (doc:Document)<-[:PART_OF]-(c:Chunk)-[:HAS_ENTITY]->(e)
RETURN
e {
.*,
embedding: null,
elementId: elementId(e),
labels: CASE
WHEN size(labels(e)) > 1 THEN
apoc.coll.removeAll(labels(e), ["__Entity__"])
ELSE
["Entity"]
END
} AS e,
collect(distinct doc.fileName) AS documents,
count(distinct c) AS chunkConnections
ORDER BY e.id ASC
LIMIT 100
"""
query_total_nodes = """
MATCH (e:!Chunk&!Document&!`__Community__`)
WHERE NOT exists { (e)--(:!Chunk&!Document&!`__Community__`) }
RETURN count(*) as total
"""
nodes_list = self.execute_query(query)
total_nodes = self.execute_query(query_total_nodes)
return nodes_list, total_nodes[0]
def delete_unconnected_nodes(self,unconnected_entities_list):
entities_list = list(map(str.strip, json.loads(unconnected_entities_list)))
query = """
MATCH (e) WHERE elementId(e) IN $elementIds
DETACH DELETE e
"""
param = {"elementIds":entities_list}
return self.execute_query(query,param)
def get_duplicate_nodes_list(self):
score_value = float(os.environ.get('DUPLICATE_SCORE_VALUE'))
text_distance = int(os.environ.get('DUPLICATE_TEXT_DISTANCE'))
query_duplicate_nodes = """
MATCH (n:!Chunk&!Document&!`__Community__`) with n
WHERE n.embedding is not null and n.id is not null // and size(toString(n.id)) > 3
WITH n ORDER BY count {{ (n)--() }} DESC, size(toString(n.id)) DESC // updated
WITH collect(n) as nodes
UNWIND nodes as n
WITH n, [other in nodes
// only one pair, same labels e.g. Person with Person
WHERE elementId(n) < elementId(other) and labels(n) = labels(other)
// at least embedding similarity of X
AND
(
// either contains each other as substrings or has a text edit distinct of less than 3
(size(toString(other.id)) > 2 AND toLower(n.id) CONTAINS toLower(other.id)) OR
(size(toString(n.id)) > 2 AND toLower(other.id) CONTAINS toLower(n.id))
OR (size(toString(n.id))>5 AND apoc.text.distance(toLower(n.id), toLower(other.id)) < $duplicate_text_distance)
OR
vector.similarity.cosine(other.embedding, n.embedding) > $duplicate_score_value
)] as similar
WHERE size(similar) > 0
// remove duplicate subsets
with collect([n]+similar) as all
CALL {{ with all
unwind all as nodes
with nodes, all
// skip current entry if it's smaller and a subset of any other entry
where none(other in all where other <> nodes and size(other) > size(nodes) and size(apoc.coll.subtract(nodes, other))=0)
return head(nodes) as n, tail(nodes) as similar
}}
OPTIONAL MATCH (doc:Document)<-[:PART_OF]-(c:Chunk)-[:HAS_ENTITY]->(n)
{return_statement}
"""
return_query_duplicate_nodes = """
RETURN n {.*, embedding:null, elementId:elementId(n), labels:labels(n)} as e,
[s in similar | s {.id, .description, labels:labels(s), elementId: elementId(s)}] as similar,
collect(distinct doc.fileName) as documents, count(distinct c) as chunkConnections
ORDER BY e.id ASC
"""
return_query_duplicate_nodes_total = "RETURN COUNT(DISTINCT(n)) as total"
param = {"duplicate_score_value": score_value, "duplicate_text_distance" : text_distance}
nodes_list = self.execute_query(query_duplicate_nodes.format(return_statement=return_query_duplicate_nodes),param=param)
total_nodes = self.execute_query(query_duplicate_nodes.format(return_statement=return_query_duplicate_nodes_total),param=param)
return nodes_list, total_nodes[0]
def merge_duplicate_nodes(self,duplicate_nodes_list):
nodes_list = json.loads(duplicate_nodes_list)
print(f'Nodes list to merge {nodes_list}')
query = """
UNWIND $rows AS row
CALL { with row
MATCH (first) WHERE elementId(first) = row.firstElementId
MATCH (rest) WHERE elementId(rest) IN row.similarElementIds
WITH first, collect (rest) as rest
WITH [first] + rest as nodes
CALL apoc.refactor.mergeNodes(nodes,
{properties:"discard",mergeRels:true, produceSelfRel:false, preserveExistingSelfRels:false, singleElementAsArray:true})
YIELD node
RETURN size(nodes) as mergedCount
}
RETURN sum(mergedCount) as totalMerged
"""
param = {"rows":nodes_list}
return self.execute_query(query,param)
def drop_create_vector_index(self, isVectorIndexExist):
"""
drop and create the vector index when vector index dimesion are different.
"""
embedding_model = os.getenv('EMBEDDING_MODEL')
embeddings, dimension = load_embedding_model(embedding_model)
if isVectorIndexExist == 'true':
self.graph.query("""drop index vector""")
# self.graph.query("""drop index vector""")
self.graph.query("""CREATE VECTOR INDEX `vector` if not exists for (c:Chunk) on (c.embedding)
OPTIONS {indexConfig: {
`vector.dimensions`: $dimensions,
`vector.similarity_function`: 'cosine'
}}
""",
{
"dimensions" : dimension
}
)
return "Drop and Re-Create vector index succesfully"