Add advanced GSQL queries: PPR scoring, shortest paths, spreading activation, community detection"
Browse files- graphrag/layers/gsql_advanced.py +173 -0
graphrag/layers/gsql_advanced.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Advanced GSQL Queries for GraphRAG Novelties
|
| 3 |
+
=============================================
|
| 4 |
+
Queries that run inside TigerGraph for PPR, paths, and community detection.
|
| 5 |
+
Install once via: graph.install_advanced_queries()
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
# ββ Personalized PageRank (CatRAG-inspired) ββββββββββββββ
|
| 9 |
+
PPR_QUERY = """
|
| 10 |
+
CREATE OR REPLACE QUERY pprFromSeeds(
|
| 11 |
+
SET<STRING> seedEntityIds,
|
| 12 |
+
FLOAT damping,
|
| 13 |
+
INT maxIter
|
| 14 |
+
) FOR GRAPH {graphname} {{
|
| 15 |
+
|
| 16 |
+
TYPEDEF TUPLE<STRING entity_id, STRING name, DOUBLE score> EntityPPR;
|
| 17 |
+
HeapAccum<EntityPPR>(50, score DESC) @@topEntities;
|
| 18 |
+
SumAccum<DOUBLE> @score;
|
| 19 |
+
SumAccum<DOUBLE> @newScore;
|
| 20 |
+
SumAccum<INT> @outDegree;
|
| 21 |
+
|
| 22 |
+
# Initialize
|
| 23 |
+
AllEntities = {{Entity.*}};
|
| 24 |
+
AllEntities = SELECT e FROM AllEntities:e
|
| 25 |
+
ACCUM e.@outDegree += 1;
|
| 26 |
+
|
| 27 |
+
Seeds = SELECT e FROM AllEntities:e
|
| 28 |
+
WHERE e.entity_id IN seedEntityIds
|
| 29 |
+
ACCUM e.@score += 1.0 / seedEntityIds.size();
|
| 30 |
+
|
| 31 |
+
# Power iteration
|
| 32 |
+
FOREACH iter IN RANGE[1, maxIter] DO
|
| 33 |
+
AllEntities = SELECT e FROM AllEntities:e -(RELATED_TO:rel)- Entity:nbr
|
| 34 |
+
ACCUM
|
| 35 |
+
DOUBLE contribution = CASE WHEN nbr.@outDegree > 0
|
| 36 |
+
THEN nbr.@score * damping * rel.weight / nbr.@outDegree
|
| 37 |
+
ELSE 0.0 END,
|
| 38 |
+
e.@newScore += contribution
|
| 39 |
+
POST-ACCUM
|
| 40 |
+
e.@score = e.@newScore + CASE WHEN e.entity_id IN seedEntityIds
|
| 41 |
+
THEN (1.0 - damping) / seedEntityIds.size()
|
| 42 |
+
ELSE 0.0 END,
|
| 43 |
+
e.@newScore = 0;
|
| 44 |
+
END;
|
| 45 |
+
|
| 46 |
+
# Collect top entities by PPR score
|
| 47 |
+
AllEntities = SELECT e FROM AllEntities:e
|
| 48 |
+
WHERE e.@score > 0.001
|
| 49 |
+
ACCUM @@topEntities += EntityPPR(e.entity_id, e.name, e.@score);
|
| 50 |
+
|
| 51 |
+
PRINT @@topEntities;
|
| 52 |
+
}}
|
| 53 |
+
|
| 54 |
+
INSTALL QUERY pprFromSeeds
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
# ββ Shortest Path Between Entity Pairs βββββββββββββββββββ
|
| 58 |
+
SHORTEST_PATH_QUERY = """
|
| 59 |
+
CREATE OR REPLACE QUERY findReasoningPaths(
|
| 60 |
+
STRING sourceId,
|
| 61 |
+
STRING targetId,
|
| 62 |
+
INT maxDepth
|
| 63 |
+
) FOR GRAPH {graphname} {{
|
| 64 |
+
|
| 65 |
+
ListAccum<STRING> @pathTrace;
|
| 66 |
+
SetAccum<STRING> @@visitedNodes;
|
| 67 |
+
ListAccum<STRING> @@pathDescriptions;
|
| 68 |
+
|
| 69 |
+
Source = {{Entity.*}};
|
| 70 |
+
Source = SELECT e FROM Source:e WHERE e.entity_id == sourceId
|
| 71 |
+
ACCUM e.@pathTrace += e.name;
|
| 72 |
+
|
| 73 |
+
FOREACH depth IN RANGE[1, maxDepth] DO
|
| 74 |
+
Source = SELECT nbr FROM Source:e -(RELATED_TO:rel)- Entity:nbr
|
| 75 |
+
WHERE nbr.entity_id NOT IN @@visitedNodes
|
| 76 |
+
ACCUM
|
| 77 |
+
nbr.@pathTrace += e.@pathTrace,
|
| 78 |
+
nbr.@pathTrace += (" -[" + rel.relation_type + "]-> " + nbr.name),
|
| 79 |
+
@@visitedNodes += nbr.entity_id;
|
| 80 |
+
|
| 81 |
+
# Check if target reached
|
| 82 |
+
Target = SELECT e FROM Source:e WHERE e.entity_id == targetId
|
| 83 |
+
ACCUM @@pathDescriptions += e.@pathTrace;
|
| 84 |
+
END;
|
| 85 |
+
|
| 86 |
+
PRINT @@pathDescriptions;
|
| 87 |
+
}}
|
| 88 |
+
|
| 89 |
+
INSTALL QUERY findReasoningPaths
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
# ββ Spreading Activation βββββββββββββββββββββββββββββββββ
|
| 93 |
+
SPREADING_ACTIVATION_QUERY = """
|
| 94 |
+
CREATE OR REPLACE QUERY spreadingActivation(
|
| 95 |
+
SET<STRING> seedEntityIds,
|
| 96 |
+
FLOAT decayFactor,
|
| 97 |
+
INT maxSteps,
|
| 98 |
+
FLOAT threshold
|
| 99 |
+
) FOR GRAPH {graphname} {{
|
| 100 |
+
|
| 101 |
+
TYPEDEF TUPLE<STRING entity_id, STRING name, DOUBLE activation> ActivatedEntity;
|
| 102 |
+
HeapAccum<ActivatedEntity>(100, activation DESC) @@activated;
|
| 103 |
+
SumAccum<DOUBLE> @activation;
|
| 104 |
+
SumAccum<DOUBLE> @newActivation;
|
| 105 |
+
|
| 106 |
+
# Initialize seeds
|
| 107 |
+
Seeds = {{Entity.*}};
|
| 108 |
+
Seeds = SELECT e FROM Seeds:e
|
| 109 |
+
WHERE e.entity_id IN seedEntityIds
|
| 110 |
+
ACCUM e.@activation += 1.0;
|
| 111 |
+
|
| 112 |
+
# Propagate
|
| 113 |
+
FOREACH step IN RANGE[1, maxSteps] DO
|
| 114 |
+
ActiveNodes = SELECT e FROM Seeds:e -(RELATED_TO:rel)- Entity:nbr
|
| 115 |
+
WHERE e.@activation > threshold
|
| 116 |
+
ACCUM
|
| 117 |
+
DOUBLE spread = e.@activation * rel.weight * decayFactor,
|
| 118 |
+
nbr.@newActivation += CASE WHEN spread > threshold THEN spread ELSE 0.0 END
|
| 119 |
+
POST-ACCUM
|
| 120 |
+
e.@activation = e.@newActivation,
|
| 121 |
+
e.@newActivation = 0;
|
| 122 |
+
END;
|
| 123 |
+
|
| 124 |
+
# Collect all activated entities
|
| 125 |
+
AllEntities = {{Entity.*}};
|
| 126 |
+
AllEntities = SELECT e FROM AllEntities:e
|
| 127 |
+
WHERE e.@activation > threshold
|
| 128 |
+
ACCUM @@activated += ActivatedEntity(e.entity_id, e.name, e.@activation);
|
| 129 |
+
|
| 130 |
+
PRINT @@activated;
|
| 131 |
+
}}
|
| 132 |
+
|
| 133 |
+
INSTALL QUERY spreadingActivation
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
# ββ Get Entity Neighborhood for Community Detection ββββββ
|
| 137 |
+
ENTITY_NEIGHBORHOOD_QUERY = """
|
| 138 |
+
CREATE OR REPLACE QUERY getEntityNeighborhood(
|
| 139 |
+
SET<STRING> entityIds,
|
| 140 |
+
INT hops
|
| 141 |
+
) FOR GRAPH {graphname} {{
|
| 142 |
+
|
| 143 |
+
SetAccum<STRING> @@nodeIds;
|
| 144 |
+
SetAccum<STRING> @@edgeDescriptions;
|
| 145 |
+
SumAccum<INT> @@nodeCount;
|
| 146 |
+
SumAccum<INT> @@edgeCount;
|
| 147 |
+
|
| 148 |
+
Seeds = {{Entity.*}};
|
| 149 |
+
Seeds = SELECT e FROM Seeds:e WHERE e.entity_id IN entityIds
|
| 150 |
+
ACCUM @@nodeIds += e.entity_id, @@nodeCount += 1;
|
| 151 |
+
|
| 152 |
+
FOREACH hop IN RANGE[1, hops] DO
|
| 153 |
+
Seeds = SELECT nbr FROM Seeds:e -(RELATED_TO:rel)- Entity:nbr
|
| 154 |
+
WHERE nbr.entity_id NOT IN @@nodeIds
|
| 155 |
+
ACCUM
|
| 156 |
+
@@nodeIds += nbr.entity_id,
|
| 157 |
+
@@nodeCount += 1,
|
| 158 |
+
@@edgeCount += 1,
|
| 159 |
+
@@edgeDescriptions += (e.name + "|" + rel.relation_type + "|" + nbr.name + "|" + to_string(rel.weight));
|
| 160 |
+
END;
|
| 161 |
+
|
| 162 |
+
PRINT @@nodeIds, @@edgeDescriptions, @@nodeCount, @@edgeCount;
|
| 163 |
+
}}
|
| 164 |
+
|
| 165 |
+
INSTALL QUERY getEntityNeighborhood
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
ALL_ADVANCED_QUERIES = {
|
| 169 |
+
"pprFromSeeds": PPR_QUERY,
|
| 170 |
+
"findReasoningPaths": SHORTEST_PATH_QUERY,
|
| 171 |
+
"spreadingActivation": SPREADING_ACTIVATION_QUERY,
|
| 172 |
+
"getEntityNeighborhood": ENTITY_NEIGHBORHOOD_QUERY,
|
| 173 |
+
}
|