Create reality adapter
Browse files- reality adapter +130 -0
reality adapter
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
adapter_actual_reality.py
|
| 4 |
+
|
| 5 |
+
Adapter + persistence for ACTUAL_REALITY_MODULE_v2 -> lm_quant_veritas
|
| 6 |
+
|
| 7 |
+
Features:
|
| 8 |
+
- Accepts event + optional lm_quant_veritas context (coherence_score, entanglement_signature, metadata)
|
| 9 |
+
- Calls RealityInterface.analyze_event()
|
| 10 |
+
- Persists result to JSONL (append) and optionally to SQLite
|
| 11 |
+
- Loads runtime config from YAML (fallback to defaults)
|
| 12 |
+
- Minimal external deps: pyyaml (optional), sqlite3 (stdlib), json, pathlib
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
import json
|
| 17 |
+
import sqlite3
|
| 18 |
+
import logging
|
| 19 |
+
from dataclasses import dataclass, field, asdict
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
from typing import Dict, Any, Optional
|
| 23 |
+
|
| 24 |
+
# Import the RealityInterface from your ACTUAL_REALITY_MODULE_v2
|
| 25 |
+
# Adjust the import path as needed (if it's a module/package).
|
| 26 |
+
try:
|
| 27 |
+
from ACTUAL_REALITY_MODULE_v2 import RealityInterface
|
| 28 |
+
except Exception:
|
| 29 |
+
# if module name differs or file is in same folder, try local import fallback
|
| 30 |
+
from actual_reality_module_v2 import RealityInterface # type: ignore
|
| 31 |
+
|
| 32 |
+
# Optional: pyyaml for config; not strictly required
|
| 33 |
+
try:
|
| 34 |
+
import yaml
|
| 35 |
+
except Exception:
|
| 36 |
+
yaml = None
|
| 37 |
+
|
| 38 |
+
logger = logging.getLogger("AR_Adapter")
|
| 39 |
+
logger.addHandler(logging.StreamHandler())
|
| 40 |
+
logger.setLevel(logging.INFO)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
DEFAULT_CONFIG = {
|
| 44 |
+
"persistence": {
|
| 45 |
+
"jsonl_path": "actual_reality_analysis.jsonl",
|
| 46 |
+
"sqlite_path": "actual_reality_analysis.db",
|
| 47 |
+
"use_sqlite": True,
|
| 48 |
+
},
|
| 49 |
+
"adapter": {
|
| 50 |
+
"coherence_field": "coherence_score",
|
| 51 |
+
"entanglement_field": "entanglement_signature",
|
| 52 |
+
"include_lm_context": True,
|
| 53 |
+
},
|
| 54 |
+
"reality_interface": {
|
| 55 |
+
"keyword_similarity_weight": 0.6,
|
| 56 |
+
"metrics_shift_sensitivity": 0.25
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@dataclass
|
| 62 |
+
class LMContext:
|
| 63 |
+
"""Simple container for lm_quant_veritas-style metrics to be attached to analysis."""
|
| 64 |
+
coherence_score: Optional[float] = None
|
| 65 |
+
entanglement_signature: Optional[Dict[str, Any]] = field(default_factory=dict)
|
| 66 |
+
model_version: Optional[str] = None
|
| 67 |
+
additional_meta: Dict[str, Any] = field(default_factory=dict)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class ActualRealityAdapter:
|
| 71 |
+
def __init__(self, config_path: Optional[str] = None, reality_interface: Optional[RealityInterface] = None):
|
| 72 |
+
self.config = self._load_config(config_path)
|
| 73 |
+
self.ri = reality_interface or RealityInterface()
|
| 74 |
+
# apply tunables from config to the RealityInterface if present
|
| 75 |
+
ri_cfg = self.config.get("reality_interface", {})
|
| 76 |
+
for k, v in ri_cfg.items():
|
| 77 |
+
if hasattr(self.ri, k):
|
| 78 |
+
setattr(self.ri, k, v)
|
| 79 |
+
|
| 80 |
+
self.jsonl_path = Path(self.config["persistence"]["jsonl_path"])
|
| 81 |
+
self.sqlite_path = Path(self.config["persistence"]["sqlite_path"])
|
| 82 |
+
self.use_sqlite = bool(self.config["persistence"].get("use_sqlite", True))
|
| 83 |
+
|
| 84 |
+
if self.use_sqlite:
|
| 85 |
+
# ensure sqlite DB and table exist
|
| 86 |
+
self._init_sqlite_db()
|
| 87 |
+
|
| 88 |
+
def _load_config(self, config_path: Optional[str]) -> Dict[str, Any]:
|
| 89 |
+
if config_path and yaml and Path(config_path).exists():
|
| 90 |
+
try:
|
| 91 |
+
with open(config_path, "r", encoding="utf-8") as fh:
|
| 92 |
+
cfg = yaml.safe_load(fh)
|
| 93 |
+
merged = DEFAULT_CONFIG.copy()
|
| 94 |
+
merged.update(cfg or {})
|
| 95 |
+
return merged
|
| 96 |
+
except Exception as e:
|
| 97 |
+
logger.warning("Failed to load YAML config (%s). Using defaults. Error: %s", config_path, e)
|
| 98 |
+
return DEFAULT_CONFIG.copy()
|
| 99 |
+
|
| 100 |
+
def _init_sqlite_db(self):
|
| 101 |
+
conn = sqlite3.connect(self.sqlite_path)
|
| 102 |
+
cur = conn.cursor()
|
| 103 |
+
cur.execute(
|
| 104 |
+
"""
|
| 105 |
+
CREATE TABLE IF NOT EXISTS analyses (
|
| 106 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 107 |
+
timestamp TEXT,
|
| 108 |
+
surface_event TEXT,
|
| 109 |
+
analysis_json TEXT,
|
| 110 |
+
coherence_score REAL,
|
| 111 |
+
entanglement_json TEXT,
|
| 112 |
+
model_version TEXT
|
| 113 |
+
)
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
+
conn.commit()
|
| 117 |
+
conn.close()
|
| 118 |
+
|
| 119 |
+
def analyze_and_record(self, surface_event: str, lm_context: Optional[LMContext] = None) -> Dict[str, Any]:
|
| 120 |
+
"""
|
| 121 |
+
Core API: analyze the event, augment with LM context, persist, and return the analysis dict.
|
| 122 |
+
"""
|
| 123 |
+
lc = lm_context or LMContext()
|
| 124 |
+
# 1) perform analysis
|
| 125 |
+
analysis = self.ri.analyze_event(surface_event)
|
| 126 |
+
|
| 127 |
+
# 2) attach lm_quant_veritas context if configured to do so
|
| 128 |
+
if self.config["adapter"].get("include_lm_context", True):
|
| 129 |
+
analysis["_lm_context"] = {
|
| 130 |
+
"coherence_score": lc.coherence
|