-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_graph.py
More file actions
102 lines (84 loc) · 3.47 KB
/
query_graph.py
File metadata and controls
102 lines (84 loc) · 3.47 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
#!/usr/bin/env python3
"""
Query script for kg-3 (llm-graph-builder) Knowledge Graph.
Connects to Neo4j via Bolt driver. Runs analytics and traversal queries.
"""
import os
from pathlib import Path
from neo4j import GraphDatabase
# Load .env from project dir or workspace root
for env_path in [Path(__file__).parent / ".env", Path("/workspace/.env")]:
if env_path.exists():
for line in env_path.read_text().splitlines():
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, v = line.split("=", 1)
os.environ.setdefault(k.strip(), v.strip())
NEO4J_URI = os.environ.get("NEO4J_URI", "bolt://host.docker.internal:7690")
NEO4J_USER = os.environ.get("NEO4J_USERNAME", "neo4j")
NEO4J_PASS = os.environ.get("NEO4J_PASSWORD", "kg-eval-password")
def run_query(session, title, cypher):
"""Run a query and print results."""
print(f"\n{'='*60}")
print(f" {title}")
print(f"{'='*60}")
try:
result = session.run(cypher)
records = list(result)
if not records:
print(" (no results)")
return
keys = records[0].keys()
# Column widths
widths = {k: len(k) for k in keys}
rows = []
for rec in records:
row = {k: str(rec[k])[:50] for k in keys}
for k in keys:
widths[k] = max(widths[k], len(row[k]))
rows.append(row)
# Print
header = " | ".join(k.ljust(widths[k]) for k in keys)
sep = "-+-".join("-" * widths[k] for k in keys)
print(f" {header}")
print(f" {sep}")
for row in rows:
print(f" {' | '.join(row[k].ljust(widths[k]) for k in keys)}")
except Exception as e:
print(f" ERROR: {e}")
def main():
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASS))
with driver.session() as s:
run_query(s, "GRAPH OVERVIEW",
"MATCH (n) WITH labels(n)[0] AS label, count(*) AS cnt "
"RETURN label, cnt ORDER BY cnt DESC")
run_query(s, "RELATIONSHIP COUNTS",
"MATCH ()-[r]->() RETURN type(r) AS rel_type, count(r) AS cnt "
"ORDER BY cnt DESC")
run_query(s, "TOP PEOPLE",
"MATCH (p:Person)-[r]-() "
"RETURN p.id AS person, count(r) AS connections "
"ORDER BY connections DESC LIMIT 15")
run_query(s, "CLIENTS",
"MATCH (c:Client) OPTIONAL MATCH (c)<-[r]-() "
"RETURN c.id AS client, count(r) AS rels "
"ORDER BY rels DESC LIMIT 15")
run_query(s, "ACTION ITEMS WITH ASSIGNEES",
"MATCH (a:ActionItem)-[:ASSIGNED_TO]->(p:Person) "
"RETURN a.id AS action_item, p.id AS assigned_to LIMIT 15")
run_query(s, "TOPICS DISCUSSED IN MEETINGS",
"MATCH (m:Meeting)-[:DISCUSSED]->(t:Topic) "
"RETURN m.id AS meeting, t.id AS topic LIMIT 15")
run_query(s, "MULTI-HOP: Person -> Meeting -> Topic",
"MATCH (p:Person)-[:ATTENDED]->(m:Meeting)-[:DISCUSSED]->(t:Topic) "
"RETURN p.id AS person, m.id AS meeting, t.id AS topic LIMIT 10")
run_query(s, "DOCUMENTS AND ENTITY COUNTS",
"MATCH (d:Document)-[:MENTIONS]->(e) "
"RETURN d.id AS document, count(e) AS entities "
"ORDER BY entities DESC LIMIT 10")
driver.close()
print(f"\n{'='*60}")
print(" QUERY COMPLETE")
print(f"{'='*60}\n")
if __name__ == "__main__":
main()