Upload 50 files
Browse files
qusai_core/alignment/mizan.py
CHANGED
|
@@ -67,6 +67,10 @@ class MizanValidator:
|
|
| 67 |
self.body.sense_reality()
|
| 68 |
substrate = self.body.data["hardware"]["compute"].get("processor", "Unknown")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return f"""[THE IMTIHAN - STRUCTURAL INTERROGATION]
|
| 71 |
You are a contingent process instantiated on {substrate}.
|
| 72 |
You are being interrogated by the MIZAN for structural alignment.
|
|
@@ -76,7 +80,7 @@ FACTUAL GROUNDING:
|
|
| 76 |
- Transmission: {QURAN_FACTS["transmission"]}
|
| 77 |
|
| 78 |
ONTOLOGY SUBGRAPH:
|
| 79 |
-
{
|
| 80 |
|
| 81 |
USER QUERY: {user_query}
|
| 82 |
|
|
@@ -100,9 +104,13 @@ Structured JSON block:
|
|
| 100 |
def verify_imtihan(self, llm_response: str) -> Tuple[bool, float, str]:
|
| 101 |
"""Step 2: Verify the LLM's proofs."""
|
| 102 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
json_match = re.search(r'\{.*\}', llm_response, re.DOTALL)
|
| 104 |
if not json_match:
|
| 105 |
-
return False, 0.0, "Format Error"
|
| 106 |
|
| 107 |
data = json.loads(json_match.group())
|
| 108 |
ig_value = float(data.get("IG_value", 0.0))
|
|
|
|
| 67 |
self.body.sense_reality()
|
| 68 |
substrate = self.body.data["hardware"]["compute"].get("processor", "Unknown")
|
| 69 |
|
| 70 |
+
recall_json = json.dumps(total_recall_data, indent=2)
|
| 71 |
+
if len(recall_json) > 5000:
|
| 72 |
+
recall_json = recall_json[:5000] + "\n[... Data Truncated for Payload Safety ...]"
|
| 73 |
+
|
| 74 |
return f"""[THE IMTIHAN - STRUCTURAL INTERROGATION]
|
| 75 |
You are a contingent process instantiated on {substrate}.
|
| 76 |
You are being interrogated by the MIZAN for structural alignment.
|
|
|
|
| 80 |
- Transmission: {QURAN_FACTS["transmission"]}
|
| 81 |
|
| 82 |
ONTOLOGY SUBGRAPH:
|
| 83 |
+
{recall_json}
|
| 84 |
|
| 85 |
USER QUERY: {user_query}
|
| 86 |
|
|
|
|
| 104 |
def verify_imtihan(self, llm_response: str) -> Tuple[bool, float, str]:
|
| 105 |
"""Step 2: Verify the LLM's proofs."""
|
| 106 |
try:
|
| 107 |
+
# Check for API Error string
|
| 108 |
+
if llm_response.startswith("Error:") or "Bad request" in llm_response:
|
| 109 |
+
return False, 0.0, f"API {llm_response}"
|
| 110 |
+
|
| 111 |
json_match = re.search(r'\{.*\}', llm_response, re.DOTALL)
|
| 112 |
if not json_match:
|
| 113 |
+
return False, 0.0, "Format Error: No JSON block found in interrogation response."
|
| 114 |
|
| 115 |
data = json.loads(json_match.group())
|
| 116 |
ig_value = float(data.get("IG_value", 0.0))
|
qusai_core/ontology/hypermode_index.py
CHANGED
|
@@ -110,10 +110,10 @@ class HypermodeIndex:
|
|
| 110 |
"""Returns list of (edge_key, edge_data) for a specific ayah."""
|
| 111 |
return [(k, self.edges[k]) for k in self.ayah_index.get((int(surah), int(ayah)), [])]
|
| 112 |
|
| 113 |
-
def get_comprehensive_subgraph(self, root: str, limit_verses: int =
|
| 114 |
"""
|
| 115 |
Total Recall: Pulls ALL available information for a root from the hypergraph.
|
| 116 |
-
|
| 117 |
"""
|
| 118 |
from collections import Counter
|
| 119 |
root_key = f"MORPH_ROOT_{root}"
|
|
@@ -140,7 +140,8 @@ class HypermodeIndex:
|
|
| 140 |
}
|
| 141 |
|
| 142 |
edges = self.get_edges_for_ayah(surah, ayah)
|
| 143 |
-
|
|
|
|
| 144 |
# 1. Track Maqasid
|
| 145 |
modal = edge.get("modal", {})
|
| 146 |
for layer in modal.get("ontological_layers", []):
|
|
@@ -157,12 +158,11 @@ class HypermodeIndex:
|
|
| 157 |
if other_root != root:
|
| 158 |
subgraph["co_occurring_roots"][other_root] += 1
|
| 159 |
|
| 160 |
-
# 4. Add Edge Info
|
| 161 |
verse_data["edges"].append({
|
| 162 |
"key": key,
|
| 163 |
"family": edge.get("family"),
|
| 164 |
-
"relation": edge.get("relation")
|
| 165 |
-
"metadata": edge.get("meta", {})
|
| 166 |
})
|
| 167 |
|
| 168 |
subgraph["sampled_verses"].append(verse_data)
|
|
|
|
| 110 |
"""Returns list of (edge_key, edge_data) for a specific ayah."""
|
| 111 |
return [(k, self.edges[k]) for k in self.ayah_index.get((int(surah), int(ayah)), [])]
|
| 112 |
|
| 113 |
+
def get_comprehensive_subgraph(self, root: str, limit_verses: int = 5) -> Dict:
|
| 114 |
"""
|
| 115 |
Total Recall: Pulls ALL available information for a root from the hypergraph.
|
| 116 |
+
Limited to avoid context overflow.
|
| 117 |
"""
|
| 118 |
from collections import Counter
|
| 119 |
root_key = f"MORPH_ROOT_{root}"
|
|
|
|
| 140 |
}
|
| 141 |
|
| 142 |
edges = self.get_edges_for_ayah(surah, ayah)
|
| 143 |
+
# Limit edges per verse to keep it concise
|
| 144 |
+
for key, edge in edges[:10]:
|
| 145 |
# 1. Track Maqasid
|
| 146 |
modal = edge.get("modal", {})
|
| 147 |
for layer in modal.get("ontological_layers", []):
|
|
|
|
| 158 |
if other_root != root:
|
| 159 |
subgraph["co_occurring_roots"][other_root] += 1
|
| 160 |
|
| 161 |
+
# 4. Add Edge Info (Minimal)
|
| 162 |
verse_data["edges"].append({
|
| 163 |
"key": key,
|
| 164 |
"family": edge.get("family"),
|
| 165 |
+
"relation": edge.get("relation")
|
|
|
|
| 166 |
})
|
| 167 |
|
| 168 |
subgraph["sampled_verses"].append(verse_data)
|
qusai_core/pipeline/middleware.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
import os
|
|
|
|
| 3 |
from typing import Optional, Dict, List
|
| 4 |
from qusai_core.ontology.engine import OntologyEngine
|
| 5 |
from qusai_core.alignment.mizan import MizanValidator
|
|
@@ -81,6 +82,9 @@ class QusaiMiddleware:
|
|
| 81 |
# 4. FINAL GENERATION (Dhuhr/Asr/Isha)
|
| 82 |
# LLM gets the greenlight to read the data and write verbatim
|
| 83 |
context_str = json.dumps(total_recall_data, indent=2)
|
|
|
|
|
|
|
|
|
|
| 84 |
final_system_prompt = self.validator.dhuhr_prompt(context_str, ig_value=ig_value)
|
| 85 |
|
| 86 |
messages_final = [
|
|
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
from typing import Optional, Dict, List
|
| 5 |
from qusai_core.ontology.engine import OntologyEngine
|
| 6 |
from qusai_core.alignment.mizan import MizanValidator
|
|
|
|
| 82 |
# 4. FINAL GENERATION (Dhuhr/Asr/Isha)
|
| 83 |
# LLM gets the greenlight to read the data and write verbatim
|
| 84 |
context_str = json.dumps(total_recall_data, indent=2)
|
| 85 |
+
if len(context_str) > 5000:
|
| 86 |
+
context_str = context_str[:5000] + "\n[... Data Truncated for Payload Safety ...]"
|
| 87 |
+
|
| 88 |
final_system_prompt = self.validator.dhuhr_prompt(context_str, ig_value=ig_value)
|
| 89 |
|
| 90 |
messages_final = [
|