forked from KnowledgeXLab/LeanRAG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_utils.py
More file actions
1088 lines (949 loc) · 36.1 KB
/
database_utils.py
File metadata and controls
1088 lines (949 loc) · 36.1 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import numpy as np
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams
import together
from together import Together
import sqlite3
from collections import Counter, defaultdict
from dotenv import load_dotenv
from neo4j import GraphDatabase, NotificationDisabledCategory
load_dotenv()
# Suppress verbose HTTP logging from qdrant/httpx
import logging
for noisy in ["qdrant_client", "httpx", "urllib3", "httpcore"]:
logging.getLogger(noisy).setLevel(logging.WARNING)
EMBEDDING_MAX_TOKENS = int(os.getenv('TOGETHER_EMBED_MAX_TOKENS', '480'))
def _truncate_for_embedding(text: str) -> str:
from tools.utils import truncate_text
normalized = truncate_text(text or '', max_tokens=EMBEDDING_MAX_TOKENS)
return normalized[:EMBEDDING_MAX_TOKENS * 3]
def get_embedding(text: str) -> list:
"""Generate embedding for text/text_list using Together API"""
if isinstance(text, str):
text_list = [text]
else:
text_list = text
try:
client = Together(api_key=os.getenv("TOGETHER_API_KEY"))
model = os.getenv("TOGETHER_MODEL", "intfloat/multilingual-e5-large-instruct")
response = client.embeddings.create(
input=[_truncate_for_embedding(text) for text in text_list],
model=model
)
return [item.embedding for item in response.data]
except Exception as e:
print(f"Error generating embedding: {e}")
return []
def get_graph_driver():
"""Get graph database driver (Neo4j/Memgraph compatible)"""
uri = os.getenv("GRAPH_URI", "bolt://localhost:7687")
user = os.getenv("GRAPH_USER", "neo4j")
password = os.getenv("GRAPH_PASSWORD", "test123456")
# Suppress unknown property key warnings
return GraphDatabase.driver(
uri,
auth=(user, password),
notifications_disabled_categories=[NotificationDisabledCategory.UNRECOGNIZED]
)
def _iter_levelled_entities(path: str):
"""Yield `(level, entity_dict)` pairs from newline-delimited or list-based JSON."""
if not os.path.exists(path):
return
with open(path, 'r', encoding='utf-8') as stream:
for level, line in enumerate(stream):
line = line.strip()
if not line:
continue
payload = json.loads(line)
if isinstance(payload, list):
for entity in payload:
yield level, entity
elif isinstance(payload, dict):
yield level, payload
def _iter_relations(path: str):
"""Yield relation dictionaries from JSONL or JSON-array files."""
if not os.path.exists(path):
return
with open(path, 'r', encoding='utf-8') as stream:
first_char = stream.read(1)
while first_char and first_char.isspace():
first_char = stream.read(1)
stream.seek(0)
if not first_char:
return
if first_char == '[':
data = json.load(stream)
items = data if isinstance(data, list) else [data]
for relation in items:
if isinstance(relation, list):
for item in relation:
yield item
else:
yield relation
else:
for line in stream:
line = line.strip()
if not line:
continue
payload = json.loads(line)
if isinstance(payload, list):
for relation in payload:
yield relation
else:
yield payload
def _iter_communities(path: str):
"""Yield community dictionaries from JSONL or JSON-array files."""
if not os.path.exists(path):
return
with open(path, 'r', encoding='utf-8') as stream:
for line in stream:
line = line.strip()
if not line:
continue
payload = json.loads(line)
if isinstance(payload, list):
for community in payload:
yield community
else:
yield payload
def build_vector_search(data, working_dir, recreate: bool = True):
"""Used for building up the vector search space consistently"""
qdrant_url = os.getenv("QDRANT_URL", "http://localhost:6333")
qdrant_api_key = os.getenv("QDRANT_API_KEY")
qdrant_collection = os.getenv("QDRANT_COLLECTION", "entity_collection")
client = QdrantClient(url=qdrant_url, api_key=qdrant_api_key)
collection_name = qdrant_collection
exists = client.collection_exists(collection_name)
if recreate and exists:
client.delete_collection(collection_name)
exists = False
if not exists:
client.create_collection(
collection_name=collection_name,
vectors_config=VectorParams(size=1024, distance=Distance.COSINE),
)
prepared = []
total_levels = len(data)
print(f"🔧 Preparing vectors for {total_levels} level groups...")
next_id = 0
for level, sublist in enumerate(data):
level_count = 0
nodes = sublist if isinstance(sublist, list) else [sublist]
for node in nodes:
vector = node.get('vector')
if vector is None:
continue
vector = np.asarray(vector)
if vector.ndim == 2 and vector.shape[0] == 1:
vector = vector.squeeze(0)
if vector.ndim != 1:
logging.warning(
"Skipping vector for entity %s due to shape %s",
node.get('entity_name'),
getattr(vector, 'shape', None),
)
continue
payload = {
key: value
for key, value in node.items()
if key not in ('vector',)
}
payload['level'] = level
prepared.append({
'id': next_id,
'vector': vector.astype(float).tolist(),
'payload': payload,
})
next_id += 1
level_count += 1
print(f" • Level {level+1}/{total_levels}: added {level_count} items (total {len(prepared)})", end='\r', flush=True)
print()
print(f"✅ Collected {len(prepared)} vectors. Uploading to Qdrant in batches...")
piece = 32
total = len(prepared)
for batch_idx in range((total + piece - 1)//piece):
start = batch_idx * piece
end = min(start + piece, total)
batch = prepared[start:end]
client.upsert(collection_name=collection_name, points=batch)
print(f" • Uploaded {end}/{total} vectors", end='\r', flush=True)
print(f"\n🚀 Vector collection '{collection_name}' ready (total {total})")
def search_vector_search(working_dir, query, topk=10, level_mode=2):
"""Search the Qdrant collection with optional filtering by hierarchy level.
level_mode values:
0 -> raw nodes only
1 -> aggregate nodes only
2 -> all nodes
"""
from qdrant_client.models import Filter, FieldCondition, MatchValue
qdrant_url = os.getenv("QDRANT_URL", "http://localhost:6333")
qdrant_api_key = os.getenv("QDRANT_API_KEY")
qdrant_collection = os.getenv("QDRANT_COLLECTION", "entity_collection")
client = QdrantClient(url=qdrant_url, api_key=qdrant_api_key)
collection_name = qdrant_collection
filter_conditions = None
if level_mode == 0:
filter_conditions = Filter(
must=[FieldCondition(key="level", match=MatchValue(value=0))]
)
elif level_mode == 1:
filter_conditions = Filter(
must=[FieldCondition(key="level", range={"gt": 0})]
)
# else: all nodes, no filter
# Extract the query vector (handle both single vector and batch)
if hasattr(query, 'shape') and len(query.shape) > 1:
query_vector = query[0].tolist() # Take first vector if batched
else:
query_vector = query.tolist() if hasattr(query, 'tolist') else list(query)
search_results = client.search(
collection_name=collection_name,
query_vector=query_vector,
limit=topk,
query_filter=filter_conditions,
with_payload=["entity_name", "description", "parent", "level", "source_id"],
)
extract_results = [
(
hit.payload["entity_name"],
hit.payload.get("parent"),
hit.payload["description"],
hit.payload.get("source_id"),
hit.payload.get("level"),
)
for hit in search_results
]
return extract_results
def get_db_path():
"""Return the canonical path to the LeanRAG relational store (dqlite)."""
base_dir = "dqlite"
os.makedirs(base_dir, exist_ok=True)
return os.path.join(base_dir, "leanrag.db")
def create_db_table_sqlite(working_dir):
# Always use unified dqlite path; working_dir ignored (kept for backward compatibility)
# migrate_sqlite_to_dqlite()
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
# Create tables
cur.execute("""
CREATE TABLE IF NOT EXISTS entities (
entity_name TEXT,
description TEXT,
source_id TEXT,
degree INTEGER DEFAULT 0,
parent TEXT,
level INTEGER,
vector TEXT,
is_new INTEGER DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS relations (
src_tgt TEXT,
tgt_src TEXT,
description TEXT,
weight INTEGER DEFAULT 1,
level INTEGER,
src_level INTEGER DEFAULT 0,
tgt_level INTEGER DEFAULT 0,
is_new INTEGER DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS communities (
entity_name TEXT,
entity_description TEXT,
findings TEXT
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS text_units (
hash_code TEXT PRIMARY KEY,
text TEXT,
tokens INTEGER,
quality_score REAL,
strategy TEXT
)
""")
# Backfill newly introduced columns for existing installations
rel_columns = {row[1] for row in cur.execute("PRAGMA table_info(relations)").fetchall()}
if 'src_level' not in rel_columns:
cur.execute("ALTER TABLE relations ADD COLUMN src_level INTEGER DEFAULT 0")
if 'tgt_level' not in rel_columns:
cur.execute("ALTER TABLE relations ADD COLUMN tgt_level INTEGER DEFAULT 0")
# Create indexes
cur.execute("CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(entity_name)")
cur.execute("CREATE INDEX IF NOT EXISTS idx_entities_name_level ON entities(entity_name, level)")
cur.execute("CREATE INDEX IF NOT EXISTS idx_relations_link ON relations(src_tgt, tgt_src)")
cur.execute("CREATE INDEX IF NOT EXISTS idx_relations_nodes_levels ON relations(src_tgt, src_level, tgt_src, tgt_level)")
cur.execute("CREATE INDEX IF NOT EXISTS idx_communities_name ON communities(entity_name)")
cur.execute("CREATE INDEX IF NOT EXISTS idx_text_units_hash ON text_units(hash_code)")
con.commit()
con.close()
def store_text_units_in_db(working_dir, chunks_file):
"""Store text units from JSON file into SQLite database"""
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
# Load chunks from file
with open(chunks_file, 'r') as f:
chunks_data = json.load(f)
# Insert text units
for chunk in chunks_data:
cur.execute("""
INSERT OR REPLACE INTO text_units (hash_code, text, tokens, quality_score, strategy)
VALUES (?, ?, ?, ?, ?)
""", (
chunk['hash_code'],
chunk['text'],
chunk.get('tokens', 0),
chunk.get('quality_score', 0.0),
chunk.get('strategy', 'unknown')
))
con.commit()
con.close()
print(f"Stored {len(chunks_data)} text units in database")
def store_text_units_records(records):
"""Store a list of chunk dicts (already loaded) into the text_units table.
Each record must contain at minimum: hash_code, text. Optional: tokens, quality_score, strategy.
"""
if not records:
return 0
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
inserted = 0
for chunk in records:
try:
cur.execute(
"""
INSERT OR REPLACE INTO text_units (hash_code, text, tokens, quality_score, strategy)
VALUES (?, ?, ?, ?, ?)
""",
(
chunk.get('hash_code'),
chunk.get('text',''),
chunk.get('tokens', 0),
chunk.get('quality_score', 0.0),
chunk.get('strategy', 'unknown')
)
)
inserted += 1
except Exception:
continue
con.commit()
con.close()
return inserted
def insert_relationships_to_sqlite(working_dir, relationships):
"""Insert relationships into SQLite database"""
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
for rel in relationships:
src_tgt = f"{rel.get('source', '')} -> {rel.get('target', '')}"
tgt_src = f"{rel.get('target', '')} -> {rel.get('source', '')}"
cur.execute("""
INSERT INTO relations (src_tgt, tgt_src, description, is_new)
VALUES (?, ?, ?, 1)
""", (
src_tgt,
tgt_src,
rel.get('description', ''),
))
con.commit()
con.close()
def mark_entities_relations_processed(working_dir):
"""Mark all entities and relations as processed (is_new = 0)"""
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
cur.execute("UPDATE entities SET is_new = 0 WHERE is_new = 1")
cur.execute("UPDATE relations SET is_new = 0 WHERE is_new = 1")
con.commit()
con.close()
def persist_hierarchy_to_sqlite(
entity_layers: list,
base_relations: dict,
aggregate_relations: dict,
community_report: dict,
replace_existing: bool = True,
) -> dict[str, int]:
"""Store hierarchical entities, relations, and communities in the shared SQLite store."""
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
if replace_existing:
cur.execute("DELETE FROM entities")
cur.execute("DELETE FROM relations")
cur.execute("DELETE FROM communities")
entity_rows: list[tuple] = []
entity_levels: defaultdict[str, set[int]] = defaultdict(set)
for level, layer in enumerate(entity_layers):
if isinstance(layer, dict):
layer = [layer]
if not isinstance(layer, list):
continue
for entity in layer:
if not isinstance(entity, dict):
continue
name = entity.get('entity_name')
if not name:
continue
entity_levels[name].add(level)
vector = entity.get('vector')
if isinstance(vector, np.ndarray):
vector = vector.astype(float).tolist()
elif vector is not None and not isinstance(vector, list):
try:
vector = list(vector)
except TypeError:
vector = None
entity_rows.append((
name,
entity.get('description', ''),
entity.get('source_id', ''),
entity.get('degree', 0),
entity.get('parent'),
level,
json.dumps(vector) if vector is not None else None,
))
relation_rows: list[tuple] = []
seen_relations: set[tuple[str, int, str, int, str]] = set()
def resolve_level(name: str, preferred: int | None) -> int:
levels = sorted(entity_levels.get(name, []))
if not levels:
logging.warning(
"No entity levels recorded for %s; defaulting to level %s",
name,
preferred if preferred is not None else 0,
)
return preferred if preferred is not None else 0
if preferred is not None and preferred in levels:
return preferred
if preferred is not None:
for lvl in levels:
if lvl >= preferred:
return lvl
return levels[-1]
return levels[0]
def coerce_int(value, default=None):
if value is None:
return default
try:
return int(value)
except (TypeError, ValueError):
logging.warning("Unable to coerce value '%s' to int; using default %s", value, default)
return default
for rel in (base_relations or {}).values():
src = rel.get('src_tgt')
tgt = rel.get('tgt_src')
if not src or not tgt:
continue
desc = rel.get('description', '')
relation_level = coerce_int(rel.get('level'), default=0)
preferred_level = relation_level if relation_level is not None else 0
src_level = resolve_level(src, preferred_level)
tgt_level = resolve_level(tgt, preferred_level)
key = (src, src_level, tgt, tgt_level, desc)
if key in seen_relations:
continue
seen_relations.add(key)
relation_rows.append((
src,
tgt,
desc,
coerce_int(rel.get('weight', 1), default=1),
preferred_level,
src_level,
tgt_level,
))
for rel in (aggregate_relations or {}).values():
if not isinstance(rel, dict):
continue
src = rel.get('src_tgt')
tgt = rel.get('tgt_src')
if not src or not tgt:
continue
desc = rel.get('description', '')
relation_level = coerce_int(rel.get('level'))
preferred_level = relation_level if relation_level is not None else None
src_level = resolve_level(src, preferred_level)
tgt_level = resolve_level(tgt, preferred_level)
key = (src, src_level, tgt, tgt_level, desc)
if key in seen_relations:
continue
seen_relations.add(key)
relation_rows.append((
src,
tgt,
desc,
coerce_int(rel.get('weight', 1), default=1),
preferred_level if preferred_level is not None else 0,
src_level,
tgt_level,
))
community_rows: list[tuple] = []
for community in (community_report or {}).values():
if not isinstance(community, dict):
continue
name = community.get('entity_name')
if not name:
continue
findings = community.get('findings', [])
community_rows.append((
name,
community.get('entity_description', ''),
json.dumps(findings, ensure_ascii=False),
))
if entity_rows:
cur.executemany(
"""
INSERT OR REPLACE INTO entities(entity_name, description, source_id, degree, parent, level, vector, is_new)
VALUES (?, ?, ?, ?, ?, ?, ?, 1)
""",
entity_rows,
)
if relation_rows:
cur.executemany(
"""
INSERT INTO relations(src_tgt, tgt_src, description, weight, level, src_level, tgt_level, is_new)
VALUES (?, ?, ?, ?, ?, ?, ?, 1)
""",
relation_rows,
)
if community_rows:
cur.executemany(
"""
INSERT OR REPLACE INTO communities(entity_name, entity_description, findings)
VALUES (?, ?, ?)
""",
community_rows,
)
con.commit()
con.close()
return {
'entities': len(entity_rows),
'relations': len(relation_rows),
'communities': len(community_rows),
}
def insert_data_to_memgraph(working_dir, clear_existing: bool = True) -> dict[str, int]:
"""Insert graph data from SQLite into the connected graph database."""
driver = get_graph_driver()
graph_db = os.getenv("GRAPH_DATABASE")
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
entity_rows = cur.execute(
"""
SELECT entity_name, description, source_id, degree, parent, level
FROM entities
WHERE entity_name IS NOT NULL
"""
).fetchall()
relation_rows = cur.execute(
"""
SELECT src_tgt, tgt_src, description, weight, level, src_level, tgt_level
FROM relations
WHERE src_tgt IS NOT NULL AND tgt_src IS NOT NULL
"""
).fetchall()
community_rows = cur.execute(
"""
SELECT entity_name, entity_description, findings
FROM communities
WHERE entity_name IS NOT NULL
"""
).fetchall()
con.close()
stats = {
'cleared_nodes': 0,
'entities': 0,
'relationships': 0,
'communities': 0,
}
session_kwargs = {}
if graph_db:
session_kwargs['database'] = graph_db
try:
with driver.session(**session_kwargs) as session:
if clear_existing:
result = session.run("MATCH (n) RETURN count(n) AS count")
record = result.single()
if record:
stats['cleared_nodes'] = record.get("count", 0)
session.run("MATCH (n) DETACH DELETE n").consume()
for name, description, source_id, degree, parent, level in entity_rows:
summary = session.run(
"""
MERGE (e:Entity {name: $name, level: $level})
SET e.description = $description,
e.source_id = $source_id,
e.degree = $degree,
e.parent = $parent,
e.level = $level,
e.updated_at = datetime()
""",
{
"name": name,
"description": description or '',
"source_id": "|".join((source_id or '').split("|")[:5]),
"degree": degree or 0,
"parent": parent,
"level": level or 0,
},
).consume()
stats['entities'] += summary.counters.nodes_created or 0
for src_name, tgt_name, description, weight, level, src_level, tgt_level in relation_rows:
rel_src_level = src_level if src_level is not None else (level if level is not None else 0)
rel_tgt_level = tgt_level if tgt_level is not None else (level if level is not None else 0)
summary = session.run(
"""
MATCH (src:Entity {name: $src_name, level: $src_level})
MATCH (tgt:Entity {name: $tgt_name, level: $tgt_level})
MERGE (src)-[rel:RELATES_TO]->(tgt)
SET rel.description = $description,
rel.weight = $weight,
rel.level = $level,
rel.updated_at = datetime()
""",
{
"src_name": src_name,
"src_level": rel_src_level,
"tgt_name": tgt_name,
"tgt_level": rel_tgt_level,
"description": description or '',
"weight": weight or 1,
"level": level or 0,
},
).consume()
stats['relationships'] += summary.counters.relationships_created or 0
for name, description, findings in community_rows:
summary = session.run(
"""
MERGE (c:Community {entity_name: $entity_name})
SET c.entity_description = $entity_description,
c.findings = $findings,
c.updated_at = datetime()
""",
{
"entity_name": name,
"entity_description": description or '',
"findings": findings or '[]',
},
).consume()
stats['communities'] += summary.counters.nodes_created or 0
finally:
driver.close()
return stats
def upsert_communities(working_dir: str, community_report: dict[str, dict]) -> int:
"""Insert or update community summaries in the SQLite store."""
if not community_report:
return 0
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
rows = []
for community in community_report.values():
name = community.get('entity_name')
if not name:
continue
description = community.get('entity_description', '')
findings = community.get('findings', [])
rows.append((
name,
description,
json.dumps(findings, ensure_ascii=False),
))
if rows:
cur.executemany(
"""
INSERT OR REPLACE INTO communities(entity_name, entity_description, findings)
VALUES (?, ?, ?)
""",
rows,
)
con.commit()
con.close()
return len(rows)
def insert_data_to_sqlite(working_dir):
db_path = os.path.join(working_dir, "leanrag.db")
db = sqlite3.connect(db_path)
cursor = db.cursor()
entity_path = os.path.join(working_dir, "all_entities.json")
entity_rows = []
for level, entity in _iter_levelled_entities(entity_path):
if not isinstance(entity, dict):
continue
entity_rows.append((
entity.get('entity_name'),
entity.get('description', ''),
"|".join((entity.get('source_id') or '').split("|")[:5]),
entity.get('degree', 0),
entity.get('parent'),
level,
))
if entity_rows:
cursor.executemany(
"INSERT INTO entities(entity_name, description, source_id, degree, parent, level) VALUES (?, ?, ?, ?, ?, ?)",
entity_rows,
)
db.commit()
relation_path = os.path.join(working_dir, "generate_relations.json")
relation_rows = []
for relation in _iter_relations(relation_path):
if not isinstance(relation, dict):
continue
relation_rows.append((
relation.get('src_tgt'),
relation.get('tgt_src'),
relation.get('description', ''),
relation.get('weight', 1),
relation.get('level', 0),
))
if relation_rows:
cursor.executemany(
"INSERT INTO relations(src_tgt, tgt_src, description, weight, level) VALUES (?, ?, ?, ?, ?)",
relation_rows,
)
db.commit()
community_path = os.path.join(working_dir, "community.json")
community_rows = []
for community in _iter_communities(community_path):
if not isinstance(community, dict):
continue
community_rows.append((
community.get('entity_name'),
community.get('entity_description', ''),
json.dumps(community.get('findings', []), ensure_ascii=False),
))
if community_rows:
cursor.executemany(
"INSERT INTO communities(entity_name, entity_description, findings) VALUES (?, ?, ?)",
community_rows,
)
db.commit()
db.close()
def find_tree_root(working_dir, entity):
"""Find the root of the entity hierarchy in Memgraph"""
driver = get_graph_driver()
res = [entity]
with driver.session() as session:
# Since level property doesn't exist, use default depth
depth = 5
current_entity = entity
for i in range(depth):
# Since parent property doesn't exist, this will return no results
query = "MATCH (e:Entity {name: $name}) WHERE e.parent IS NOT NULL RETURN e.parent AS parent"
result = session.run(query, name=current_entity)
record = result.single()
if not record or not record["parent"]:
break
current_entity = record["parent"]
res.append(current_entity)
return res
def find_path(entity1, entity2, working_dir, level, depth=5):
"""Find path between two entities in Memgraph"""
driver = get_graph_driver()
with driver.session() as session:
# Since level property doesn't exist on relationships, don't filter by it
query = """
MATCH path = (start:Entity {name: $entity1})-[*1..{depth}]->(end:Entity {name: $entity2})
RETURN [node IN nodes(path) | node.name] AS path
ORDER BY length(path) ASC
LIMIT 1
"""
result = session.run(query, entity1=entity1, entity2=entity2, depth=depth)
record = result.single()
if record:
return record["path"]
else:
return None
def fetch_paths_between_entities(entity_pairs, max_depth: int = 4):
"""Retrieve shortest paths between entity pairs with relationship metadata."""
if not entity_pairs:
return []
depth = max(1, int(max_depth or 1))
query = f"""
MATCH path = shortestPath((start:Entity {{name: $source}})-[:RELATES_TO*1..{depth}]-(end:Entity {{name: $target}}))
RETURN [node IN nodes(path) | node.name] AS nodes,
[rel IN relationships(path) | {{
start: startNode(rel).name,
end: endNode(rel).name,
description: rel.description,
weight: coalesce(rel.weight, 1.0),
level: coalesce(rel.level, 0)
}}] AS relationships
LIMIT 1
"""
driver = get_graph_driver()
results = []
with driver.session() as session:
for source, target in entity_pairs:
if source == target:
continue
record = session.run(query, source=source, target=target).single()
if record and record["nodes"]:
results.append({
"pair": (source, target),
"nodes": record["nodes"],
"relationships": record["relationships"],
})
return results
def search_nodes_link(entity1, entity2, working_dir, level=0):
"""Search for direct link between two entities in Memgraph"""
driver = get_graph_driver()
with driver.session() as session:
# Since level property doesn't exist on relationships, don't filter by it
query = """
MATCH (e1:Entity {name: $entity1})-[r:RELATES_TO]-(e2:Entity {name: $entity2})
RETURN r, e1, e2
"""
result = session.run(query, entity1=entity1, entity2=entity2)
record = result.single()
if record:
return {
"src_tgt": record["e1"]["name"],
"tgt_src": record["e2"]["name"],
"description": record["r"]["description"],
"weight": record["r"]["weight"],
"level": record["r"].get("level", 0) # Use get() to handle missing level property
}
else:
return None
def search_chunks(working_dir, entity_set):
db_path = get_db_path()
db = sqlite3.connect(db_path)
res = []
cursor = db.cursor()
for entity in entity_set:
if entity == 'root':
continue
sql = "SELECT source_id FROM entities WHERE entity_name = ?"
cursor.execute(sql, (entity,))
ret = cursor.fetchall()
if ret:
res.append(ret[0])
db.close()
return res
def search_nodes(entity_set, working_dir):
"""Search for entities in Memgraph"""
driver = get_graph_driver()
res = []
with driver.session() as session:
for entity in entity_set:
query = """
MATCH (e:Entity {name: $entity, level: 0})
RETURN e.name AS entity_name, e.description, e.source_id, e.degree, e.parent, e.level
"""
result = session.run(query, entity=entity)
records = result.data()
for record in records:
res.append((
record["entity_name"],
record["description"],
record["source_id"],
record["degree"],
record["parent"],
record["level"]
))
return res
def get_text_units(working_dir, chunks_set, chunks_file=None, k=5, max_length: int = 240):
"""Get trimmed text-unit previews from the database based on chunk hashes."""
db_path = get_db_path()
con = sqlite3.connect(db_path)
cur = con.cursor()
chunks_list = []
for chunks in chunks_set:
if chunks and "|" in chunks:
temp_chunks = chunks.split("|")
elif chunks:
temp_chunks = [chunks]
else:
temp_chunks = []
chunks_list += temp_chunks
counter = Counter(chunks_list)
# Select most frequent chunks, up to k
duplicates = [item for item, _ in sorted(
[(item, count) for item, count in counter.items() if count > 1],
key=lambda x: x[1],
reverse=True
)[:k]]
if len(duplicates) < k:
used = set(duplicates)
for item, _ in counter.items():
if item not in used: