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|
| 7 |
+
import uuid
|
| 8 |
+
from dataclasses import dataclass, field
|
| 9 |
+
from typing import List, Dict, Any
|
| 10 |
+
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
import torch
|
| 15 |
+
from sentence_transformers import SentenceTransformer
|
| 16 |
+
from fastapi import FastAPI
|
| 17 |
+
from pydantic import BaseModel
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# =========================
|
| 21 |
+
# φ⁴³ LAW 3 CONSTANTS
|
| 22 |
+
# =========================
|
| 23 |
+
|
| 24 |
+
PHI_43 = 22.93606797749979 # Immutable scalar constraint
|
| 25 |
+
SYSTEM_ID = "QUANTARION-HYPERGRAPH-RAG-PROD"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# =========================
|
| 29 |
+
# LOGGING UTILITIES
|
| 30 |
+
# =========================
|
| 31 |
+
|
| 32 |
+
LOG_DIR = os.path.join(os.getcwd(), "Logs")
|
| 33 |
+
os.makedirs(LOG_DIR, exist_ok=True)
|
| 34 |
+
LOG_PATH = os.path.join(LOG_DIR, ".text") # Matches your HF path
|
| 35 |
+
|
| 36 |
+
def log_line(msg: str) -> None:
|
| 37 |
+
ts = datetime.utcnow().isoformat()
|
| 38 |
+
line = f"[{ts}] [{SYSTEM_ID}] {msg}"
|
| 39 |
+
print(line)
|
| 40 |
+
try:
|
| 41 |
+
with open(LOG_PATH, "a", encoding="utf-8") as f:
|
| 42 |
+
f.write(line + "
|
| 43 |
+
")
|
| 44 |
+
except Exception:
|
| 45 |
+
# If running in a constrained environment, still continue
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# =========================
|
| 50 |
+
# DATA MODELS
|
| 51 |
+
# =========================
|
| 52 |
+
|
| 53 |
+
@dataclass
|
| 54 |
+
class Hyperedge:
|
| 55 |
+
id: str
|
| 56 |
+
vertices: List[str] # entity ids
|
| 57 |
+
weight: float # relevance/strength
|
| 58 |
+
meta: Dict[str, Any] = field(default_factory=dict)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@dataclass
|
| 62 |
+
class Hypergraph:
|
| 63 |
+
vertices: List[str]
|
| 64 |
+
hyperedges: List[Hyperedge]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class QueryRequest(BaseModel):
|
| 68 |
+
query: str
|
| 69 |
+
top_k: int = 5
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class QueryResponse(BaseModel):
|
| 73 |
+
query_id: str
|
| 74 |
+
query: str
|
| 75 |
+
selected_hyperedges: List[Dict[str, Any]]
|
| 76 |
+
answer: str
|
| 77 |
+
phi43_check: float
|
| 78 |
+
latency_ms: float
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# =========================
|
| 82 |
+
# HYPERGRAPH-RAG ENGINE
|
| 83 |
+
# =========================
|
| 84 |
+
|
| 85 |
+
class HypergraphRAGEngine:
|
| 86 |
+
"""
|
| 87 |
+
Production-grade Hypergraph RAG:
|
| 88 |
+
- Embeddings via SentenceTransformer
|
| 89 |
+
- Hyperedges = n-ary concept relations
|
| 90 |
+
- Retrieval = minimal hyperedge cover approximation
|
| 91 |
+
- φ⁴³ used as a numeric regularizer for scoring/stability
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
def __init__(self, model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
|
| 95 |
+
log_line("Initializing HypergraphRAGEngine…")
|
| 96 |
+
self.model_name = model_name
|
| 97 |
+
self.embedder = SentenceTransformer(model_name)
|
| 98 |
+
self.hypergraph: Hypergraph = Hypergraph(vertices=[], hyperedges=[])
|
| 99 |
+
self.vertex_embeddings: Dict[str, np.ndarray] = {}
|
| 100 |
+
self.ready = False
|
| 101 |
+
|
| 102 |
+
# ---------- CONSTRUCTION ----------
|
| 103 |
+
|
| 104 |
+
def build_from_documents(self, docs: List[Dict[str, Any]]) -> None:
|
| 105 |
+
"""
|
| 106 |
+
docs: list of {"id": str, "text": str, "entities": [str,...]}
|
| 107 |
+
entities = extracted or annotated concept ids/names.
|
| 108 |
+
"""
|
| 109 |
+
log_line(f"Building hypergraph from {len(docs)} documents…")
|
| 110 |
+
|
| 111 |
+
vertices_set = set()
|
| 112 |
+
hyperedges: List[Hyperedge] = []
|
| 113 |
+
|
| 114 |
+
# Collect vertices
|
| 115 |
+
for d in docs:
|
| 116 |
+
for ent in d.get("entities", []):
|
| 117 |
+
vertices_set.add(ent)
|
| 118 |
+
|
| 119 |
+
vertices = sorted(list(vertices_set))
|
| 120 |
+
|
| 121 |
+
# Embed vertices
|
| 122 |
+
if vertices:
|
| 123 |
+
log_line(f"Embedding {len(vertices)} vertices…")
|
| 124 |
+
embs = self.embedder.encode(vertices, normalize_embeddings=True)
|
| 125 |
+
self.vertex_embeddings = {
|
| 126 |
+
v: embs[i] for i, v in enumerate(vertices)
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
# Create a hyperedge per document (naive but effective)
|
| 130 |
+
for d in docs:
|
| 131 |
+
ents = list(set(d.get("entities", [])))
|
| 132 |
+
if len(ents) < 2:
|
| 133 |
+
continue
|
| 134 |
+
|
| 135 |
+
he_id = str(uuid.uuid4())
|
| 136 |
+
he = Hyperedge(
|
| 137 |
+
id=he_id,
|
| 138 |
+
vertices=ents,
|
| 139 |
+
weight=1.0,
|
| 140 |
+
meta={
|
| 141 |
+
"doc_id": d["id"],
|
| 142 |
+
"text": d["text"],
|
| 143 |
+
},
|
| 144 |
+
)
|
| 145 |
+
hyperedges.append(he)
|
| 146 |
+
|
| 147 |
+
self.hypergraph = Hypergraph(vertices=vertices, hyperedges=hyperedges)
|
| 148 |
+
self.ready = True
|
| 149 |
+
log_line(
|
| 150 |
+
f"Hypergraph built: |V|={len(self.hypergraph.vertices)}, |E|={len(self.hypergraph.hyperedges)}"
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# ---------- RETRIEVAL ----------
|
| 154 |
+
|
| 155 |
+
def _query_embedding(self, query: str) -> np.ndarray:
|
| 156 |
+
return self.embedder.encode([query], normalize_embeddings=True)[0]
|
| 157 |
+
|
| 158 |
+
def _hyperedge_score(self, query_emb: np.ndarray, he: Hyperedge) -> float:
|
| 159 |
+
# Score hyperedge by mean similarity of its vertices + φ⁴³ regularizer
|
| 160 |
+
sims = []
|
| 161 |
+
for v in he.vertices:
|
| 162 |
+
ve = self.vertex_embeddings.get(v)
|
| 163 |
+
if ve is not None:
|
| 164 |
+
sims.append(float(np.dot(query_emb, ve)))
|
| 165 |
+
if not sims:
|
| 166 |
+
base = 0.0
|
| 167 |
+
else:
|
| 168 |
+
base = float(np.mean(sims))
|
| 169 |
+
# φ-based smoothing to keep scores stable in [-1,1]
|
| 170 |
+
reg = (base + 1.0) / 2.0 # [0,1]
|
| 171 |
+
return float(base + 0.01 * (PHI_43 / 23.0) * reg)
|
| 172 |
+
|
| 173 |
+
def retrieve_hyperedges(self, query: str, top_k: int = 5) -> List[Hyperedge]:
|
| 174 |
+
if not self.ready or not self.hypergraph.hyperedges:
|
| 175 |
+
return []
|
| 176 |
+
|
| 177 |
+
q_emb = self._query_embedding(query)
|
| 178 |
+
scored = []
|
| 179 |
+
for he in self.hypergraph.hyperedges:
|
| 180 |
+
s = self._hyperedge_score(q_emb, he)
|
| 181 |
+
scored.append((s, he))
|
| 182 |
+
|
| 183 |
+
scored.sort(key=lambda x: x[0], reverse=True)
|
| 184 |
+
return [he for _, he in scored[:top_k]]
|
| 185 |
+
|
| 186 |
+
# ---------- GENERATION STUB ----------
|
| 187 |
+
|
| 188 |
+
def generate_answer(self, query: str, hyperedges: List[Hyperedge]) -> str:
|
| 189 |
+
"""
|
| 190 |
+
In production, this would call QVNN/LLM with retrieved context.
|
| 191 |
+
Here we produce a concise, deterministic executive-style answer.
|
| 192 |
+
"""
|
| 193 |
+
if not hyperedges:
|
| 194 |
+
return (
|
| 195 |
+
"No sufficient hypergraph context was found for this query in the "
|
| 196 |
+
"current Quantarion Hypergraph-RAG index."
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
docs = [he.meta.get("text", "") for he in hyperedges]
|
| 200 |
+
docs = [d for d in docs if d.strip()]
|
| 201 |
+
snippet = " ".join(docs)[:800]
|
| 202 |
+
|
| 203 |
+
return (
|
| 204 |
+
"Executive hypergraph-grounded summary:
|
| 205 |
+
"
|
| 206 |
+
f"- Query: {query}
|
| 207 |
+
"
|
| 208 |
+
f"- Top hyperedges: {len(hyperedges)}
|
| 209 |
+
"
|
| 210 |
+
f"- Condensed context: {snippet}
|
| 211 |
+
"
|
| 212 |
+
"This answer is generated by selecting a minimal set of "
|
| 213 |
+
"multi-entity hyperedges that best align with the query, "
|
| 214 |
+
"using φ⁴³-regularized similarity scoring."
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# ---------- φ⁴³ CHECK ----------
|
| 218 |
+
|
| 219 |
+
def phi43_check(self, hyperedges: List[Hyperedge]) -> float:
|
| 220 |
+
"""
|
| 221 |
+
Simple φ-check: scale count of hyperedges into [0,1] vs PHI_43.
|
| 222 |
+
"""
|
| 223 |
+
if not hyperedges:
|
| 224 |
+
return 0.0
|
| 225 |
+
val = len(hyperedges) / PHI_43
|
| 226 |
+
return float(max(0.0, min(1.0, val)))
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
# =========================
|
| 230 |
+
# FASTAPI SERVICE
|
| 231 |
+
# =========================
|
| 232 |
+
|
| 233 |
+
app = FastAPI(title="Quantarion Hypergraph-RAG Production API")
|
| 234 |
+
|
| 235 |
+
engine = HypergraphRAGEngine()
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
@app.on_event("startup")
|
| 239 |
+
def _startup():
|
| 240 |
+
# In production you would load from disk or HF datasets
|
| 241 |
+
log_line("Startup: building demo hypergraph index…")
|
| 242 |
+
demo_docs = [
|
| 243 |
+
{
|
| 244 |
+
"id": "doc1",
|
| 245 |
+
"text": "Neuromorphic SNNs provide event-driven, low-power computation.",
|
| 246 |
+
"entities": ["neuromorphic", "SNN", "event-driven"],
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"id": "doc2",
|
| 250 |
+
"text": "Hypergraph RAG uses hyperedges to capture multi-entity relations.",
|
| 251 |
+
"entities": ["hypergraph", "RAG", "multi-entity"],
|
| 252 |
+
},
|
| 253 |
+
{
|
| 254 |
+
"id": "doc3",
|
| 255 |
+
"text": "Hybrid retrieval combines dense, sparse, and graph-based signals.",
|
| 256 |
+
"entities": ["hybrid retrieval", "dense", "sparse", "graph"],
|
| 257 |
+
},
|
| 258 |
+
]
|
| 259 |
+
engine.build_from_documents(demo_docs)
|
| 260 |
+
log_line("Startup: Hypergraph-RAG demo index ready.")
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
@app.post("/query", response_model=QueryResponse)
|
| 264 |
+
def query_hypergraph_rag(req: QueryRequest):
|
| 265 |
+
t0 = time.time()
|
| 266 |
+
qid = str(uuid.uuid4())
|
| 267 |
+
log_line(f"QUERY {qid} | {req.query}")
|
| 268 |
+
|
| 269 |
+
selected = engine.retrieve_hyperedges(req.query, top_k=req.top_k)
|
| 270 |
+
answer = engine.generate_answer(req.query, selected)
|
| 271 |
+
phi_val = engine.phi43_check(selected)
|
| 272 |
+
latency = (time.time() - t0) * 1000.0
|
| 273 |
+
|
| 274 |
+
log_line(
|
| 275 |
+
f"QUERY {qid} | hyperedges={len(selected)} | phi43_check={phi_val:.3f} | latency_ms={latency:.1f}"
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
return QueryResponse(
|
| 279 |
+
query_id=qid,
|
| 280 |
+
query=req.query,
|
| 281 |
+
selected_hyperedges=[
|
| 282 |
+
{
|
| 283 |
+
"id": he.id,
|
| 284 |
+
"vertices": he.vertices,
|
| 285 |
+
"weight": he.weight,
|
| 286 |
+
"meta": he.meta,
|
| 287 |
+
}
|
| 288 |
+
for he in selected
|
| 289 |
+
],
|
| 290 |
+
answer=answer,
|
| 291 |
+
phi43_check=phi_val,
|
| 292 |
+
latency_ms=latency,
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
if __name__ == "__main__":
|
| 297 |
+
import uvicorn
|
| 298 |
+
|
| 299 |
+
log_line("Starting Quantarion Hypergraph-RAG Production server on 0.0.0.0:8000…")
|
| 300 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|