File size: 12,618 Bytes
e513185 968c182 e513185 968c182 e513185 968c182 46cdd2b e513185 46cdd2b e513185 46cdd2b e513185 968c182 46cdd2b e513185 46cdd2b e513185 968c182 e513185 46cdd2b e513185 46cdd2b e513185 968c182 e513185 46cdd2b 968c182 46cdd2b e513185 46cdd2b e513185 46cdd2b 968c182 e513185 968c182 46cdd2b 968c182 46cdd2b e513185 968c182 522afb4 968c182 e513185 968c182 13fffd5 968c182 13fffd5 968c182 | 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 | from __future__ import annotations
import json
import logging
from collections import defaultdict
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple
from src.app.settings import settings
from src.rag.chunking_legal import legal_chunk
from src.rag.ingestion import ingest_document, upsert_chunks
from src.rag.legal_versioning import annotate_graph_with_versioning, build_evidence_ref_index, build_version_map
from src.storage.qdrant_store import ensure_collection, get_qdrant_client
from src.utils.loader import load_document
logger = logging.getLogger(__name__)
REFERENCE_NODE_TYPES_STRICT = {"article", "clause", "point"}
REFERENCE_NODE_TYPES_EXTENDED = REFERENCE_NODE_TYPES_STRICT | {"section", "item", "bullet"}
def _norm_space(text: str) -> str:
return " ".join((text or "").split()).strip()
def _unique_dicts(items: List[Dict[str, Any]], keys: List[str]) -> List[Dict[str, Any]]:
seen = set()
out: List[Dict[str, Any]] = []
for item in items:
sig = tuple(str(item.get(k, "")).strip().lower() for k in keys)
if sig in seen:
continue
seen.add(sig)
out.append(item)
return out
def _index_nodes(nodes: List[Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
return {str(n["node_id"]): n for n in nodes if n.get("node_id")}
def _build_adjacency(edges: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
adj: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
for e in edges:
if e.get("source_id"):
adj[str(e["source_id"])].append(e)
return dict(adj)
def _build_reverse_adjacency(edges: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
rev: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
for e in edges:
if e.get("target_id"):
rev[str(e["target_id"])].append(e)
return dict(rev)
def refresh_graph_indexes(graph: Dict[str, Any]) -> Dict[str, Any]:
graph["node_index"] = _index_nodes(list(graph.get("nodes", [])))
graph["adjacency"] = _build_adjacency(list(graph.get("edges", [])))
graph["reverse_adjacency"] = _build_reverse_adjacency(list(graph.get("edges", [])))
return graph
def build_graph(
graph_nodes: List[Dict[str, Any]],
entity_nodes: Optional[List[Dict[str, Any]]] = None,
summary_nodes: Optional[List[Dict[str, Any]]] = None,
graph_edges: Optional[List[Dict[str, Any]]] = None,
) -> Dict[str, Any]:
entity_nodes = entity_nodes or []
summary_nodes = summary_nodes or []
graph_edges = graph_edges or []
all_nodes = _unique_dicts(list(graph_nodes) + list(entity_nodes) + list(summary_nodes), ["node_id"])
all_edges = _unique_dicts(
list(graph_edges),
["source_id", "target_id", "relation_type", "source_text", "target_text", "target_article", "target_clause", "target_point"],
)
graph = {"nodes": all_nodes, "edges": all_edges}
return refresh_graph_indexes(graph)
def materialize_graph_from_ingestion(ingestion_output: Dict[str, Any]) -> Dict[str, Any]:
graph = build_graph(
graph_nodes=ingestion_output.get("graph_nodes", []),
entity_nodes=ingestion_output.get("entity_nodes", []),
summary_nodes=ingestion_output.get("summary_nodes", []),
graph_edges=ingestion_output.get("graph_edges", []),
)
return refresh_graph_indexes(graph)
def _snapshot_path() -> Path:
p = Path(settings.graph_snapshot_path)
if not p.is_absolute():
p = Path.cwd() / p
return p
def _collect_node_children(node: Dict[str, Any]) -> List[str]:
md = dict(node.get("metadata") or {})
return [str(x) for x in (md.get("children_ids") or []) if str(x).strip()]
def _node_type(node: Dict[str, Any]) -> str:
md = dict(node.get("metadata") or {})
return str(node.get("node_type") or md.get("node_type") or "").strip().lower()
def _node_md(node: Dict[str, Any]) -> Dict[str, Any]:
return dict(node.get("metadata") or {})
def _is_reference_capable(node: Dict[str, Any]) -> bool:
node_type = _node_type(node)
children = _collect_node_children(node)
md = _node_md(node)
if node_type in REFERENCE_NODE_TYPES_STRICT:
return True
if node_type in REFERENCE_NODE_TYPES_EXTENDED and children:
return True
if children and (_norm_space(str(md.get("path_title") or "")) or _norm_space(str(node.get("text") or ""))):
return True
return False
def _is_section_anchor(node: Dict[str, Any]) -> bool:
node_type = _node_type(node)
return node_type in {"article", "clause", "point", "section", "item", "bullet"} and _is_reference_capable(node)
def _descendants(graph: Dict[str, Any], node_id: str) -> List[str]:
node_index = graph.get("node_index", {}) or {}
out: List[str] = []
seen: Set[str] = set()
queue = [node_id]
while queue:
cur = queue.pop(0)
node = node_index.get(cur)
if not node:
continue
children = _collect_node_children(node)
for child_id in children:
if child_id in seen:
continue
seen.add(child_id)
out.append(child_id)
queue.append(child_id)
return out
def collect_subtree_nodes(graph: Dict[str, Any], node_id: str) -> List[Dict[str, Any]]:
node_index = graph.get("node_index", {}) or {}
ids = [node_id] + _descendants(graph, node_id)
nodes = [node_index[nid] for nid in ids if nid in node_index]
nodes.sort(key=lambda n: int((_node_md(n).get("order_index") or 0)))
return nodes
def collect_subtree_text(graph: Dict[str, Any], node_id: str) -> str:
parts: List[str] = []
for node in collect_subtree_nodes(graph, node_id):
text = _norm_space(str(node.get("text") or ""))
if text:
parts.append(text)
return "\n".join(parts).strip()
def collect_amendment_source_nodes(graph: Dict[str, Any], node_id: str) -> List[Dict[str, Any]]:
reverse_adj = graph.get("reverse_adjacency", {}) or {}
node_index = graph.get("node_index", {}) or {}
out: List[Dict[str, Any]] = []
seen: Set[str] = set()
for edge in reverse_adj.get(node_id, []):
rel = str(edge.get("relation_type") or "").upper()
if rel not in {"AMENDS", "REPEALS", "REPLACES", "PARTIALLY_AMENDS", "PARTIALLY_REPEALS"}:
continue
src = str(edge.get("source_id") or "")
if not src or src in seen or src not in node_index:
continue
seen.add(src)
out.append(node_index[src])
return out
def _build_doc_index(graph: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
node_index = graph.get("node_index", {}) or {}
docs: Dict[str, Dict[str, Any]] = {}
for node_id, node in node_index.items():
md = _node_md(node)
doc_id = str(md.get("doc_id") or "").strip()
if not doc_id:
continue
doc = docs.setdefault(
doc_id,
{
"id": doc_id,
"doc_id": doc_id,
"official_title": str(md.get("official_title") or md.get("law_name") or "").strip(),
"doc_sketch": "",
"law_name": str(md.get("law_name") or md.get("official_title") or "").strip(),
"law_type": str(md.get("law_type") or md.get("doc_type") or "Unknown"),
"doc_type": str(md.get("doc_type") or md.get("law_type") or "Unknown"),
"source": str(md.get("source") or md.get("issuing_agency") or "LocalFile"),
"issuing_agency": str(md.get("issuing_agency") or md.get("source") or "").strip(),
"year": int(md.get("year") or 0),
"date_raw": str(md.get("date_raw") or "").strip(),
"doc_number": str(md.get("doc_number") or "").strip(),
"reference_node_ids_strict": [],
"reference_node_ids": [],
"evidence_node_ids": [],
"member_node_ids": [],
"doc_sketch_node_id": "",
},
)
artifact = str(md.get("artifact_type") or "evidence")
if artifact == "doc_sketch":
doc["doc_sketch"] = str(node.get("text") or md.get("retrieval_text") or "")
doc["doc_sketch_node_id"] = node_id
elif artifact in {"evidence", "article_bundle", "section_bundle"}:
doc["evidence_node_ids"].append(node_id)
if _is_reference_capable(node):
doc["reference_node_ids"].append(node_id)
if _node_type(node) in REFERENCE_NODE_TYPES_STRICT:
doc["reference_node_ids_strict"].append(node_id)
doc["member_node_ids"].append(node_id)
for doc in docs.values():
for key in ["reference_node_ids_strict", "reference_node_ids", "evidence_node_ids", "member_node_ids"]:
seen: Set[str] = set()
cleaned: List[str] = []
for nid in doc[key]:
if nid in seen:
continue
seen.add(nid)
cleaned.append(nid)
doc[key] = cleaned
return docs
def finalize_graph(graph: Dict[str, Any]) -> Dict[str, Any]:
graph = refresh_graph_indexes(graph)
graph = annotate_graph_with_versioning(graph)
graph = refresh_graph_indexes(graph)
graph["doc_index"] = _build_doc_index(graph)
graph["evidence_ref_index"] = build_evidence_ref_index(graph)
graph["version_map"] = build_version_map(graph)
return graph
def build_runtime_graph(*, input_dir: Optional[str] = None, glob_pattern: Optional[str] = None) -> Dict[str, Any]:
base = Path(input_dir or settings.normalized_dir)
pattern = glob_pattern or settings.normalized_glob
if not base.exists():
logger.warning("Missing normalized dir: %s. Building an empty runtime graph.", base)
return finalize_graph(build_graph(graph_nodes=[], graph_edges=[]))
all_nodes: List[Dict[str, Any]] = []
all_edges: List[Dict[str, Any]] = []
for fp in sorted(base.glob(pattern)):
if fp.suffix.lower() not in {".txt", ".pdf", ".doc", ".docx"}:
continue
text = load_document(fp)
out = ingest_document(text, fallback_doc_name=fp.stem)
all_nodes.extend(out.get("graph_nodes", []))
all_edges.extend(out.get("graph_edges", []))
graph = build_graph(graph_nodes=all_nodes, graph_edges=all_edges)
return finalize_graph(graph)
def save_graph_snapshot(graph: Dict[str, Any]) -> Path:
p = _snapshot_path()
p.parent.mkdir(parents=True, exist_ok=True)
payload = {
"nodes": list(graph.get("nodes", [])),
"edges": list(graph.get("edges", [])),
}
p.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
return p
def load_graph_snapshot() -> Optional[Dict[str, Any]]:
p = _snapshot_path()
if not p.exists():
return None
try:
raw = json.loads(p.read_text(encoding="utf-8"))
except Exception as exc:
logger.warning("Failed to load graph snapshot: %s", exc)
return None
graph = build_graph(graph_nodes=list(raw.get("nodes", [])), graph_edges=list(raw.get("edges", [])))
return finalize_graph(graph)
def reindex_qdrant_from_normalized(*, input_dir: Optional[str] = None, glob_pattern: Optional[str] = None) -> Dict[str, Any]:
base = Path(input_dir or settings.normalized_dir)
pattern = glob_pattern or settings.normalized_glob
if not base.exists():
logger.warning("Missing normalized dir: %s. Skipping Qdrant reindex.", base)
return {"status": "skipped", "indexed_chunks": 0, "reason": "missing_normalized_dir"}
client = get_qdrant_client()
ensure_collection(client)
total = 0
for fp in sorted(base.glob(pattern)):
if fp.suffix.lower() not in {".txt", ".pdf", ".doc", ".docx"}:
continue
text = load_document(fp)
chunks = legal_chunk(text, fallback_doc_name=fp.stem)
total += upsert_chunks(chunks)
return {"status": "ok", "indexed_chunks": total}
def rebuild_everything(*, input_dir: Optional[str] = None, glob_pattern: Optional[str] = None) -> Dict[str, Any]:
reindex_result = reindex_qdrant_from_normalized(input_dir=input_dir, glob_pattern=glob_pattern)
graph = build_runtime_graph(input_dir=input_dir, glob_pattern=glob_pattern)
snapshot = save_graph_snapshot(graph)
return {
"status": "ok",
"indexed_chunks": reindex_result.get("indexed_chunks", 0),
"graph_nodes": len(graph.get("nodes", [])),
"graph_edges": len(graph.get("edges", [])),
"snapshot_path": str(snapshot),
}
|