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|
+
import psutil
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
from typing import Dict, List, Tuple, Optional, Union
|
| 10 |
+
from dataclasses import dataclass, field
|
| 11 |
+
from enum import Enum
|
| 12 |
+
import json
|
| 13 |
+
import hashlib
|
| 14 |
+
from contextlib import asynccontextmanager
|
| 15 |
+
from copy import deepcopy
|
| 16 |
+
from fastapi import FastAPI
|
| 17 |
+
import uvicorn
|
| 18 |
+
from fastapi.responses import JSONResponse, PlainTextResponse
|
| 19 |
+
|
| 20 |
+
# Configure logging
|
| 21 |
+
logging.basicConfig(
|
| 22 |
+
level=logging.INFO,
|
| 23 |
+
format='%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s',
|
| 24 |
+
handlers=[
|
| 25 |
+
logging.StreamHandler(),
|
| 26 |
+
logging.FileHandler("agi_validator.log", mode='a')
|
| 27 |
+
]
|
| 28 |
+
)
|
| 29 |
+
logger = logging.getLogger("AGI_Validator")
|
| 30 |
+
|
| 31 |
+
# --------------------------
|
| 32 |
+
# ENUMERATIONS
|
| 33 |
+
# --------------------------
|
| 34 |
+
class ValidationStatus(Enum):
|
| 35 |
+
SUCCESS = "success"
|
| 36 |
+
PARTIAL_SUCCESS = "partial_success"
|
| 37 |
+
FAILURE = "failure"
|
| 38 |
+
ERROR = "error"
|
| 39 |
+
INSUFFICIENT_DATA = "insufficient_data"
|
| 40 |
+
|
| 41 |
+
class ReasoningMode(Enum):
|
| 42 |
+
DEDUCTIVE = "deductive"
|
| 43 |
+
INDUCTIVE = "inductive"
|
| 44 |
+
ABDUCTIVE = "abductive"
|
| 45 |
+
BAYESIAN = "bayesian"
|
| 46 |
+
CAUSAL = "causal"
|
| 47 |
+
|
| 48 |
+
class KnowledgeDomain(Enum):
|
| 49 |
+
SCIENCE = "science"
|
| 50 |
+
MATHEMATICS = "mathematics"
|
| 51 |
+
PHILOSOPHY = "philosophy"
|
| 52 |
+
HISTORY = "history"
|
| 53 |
+
MEDICINE = "medicine"
|
| 54 |
+
TECHNOLOGY = "technology"
|
| 55 |
+
SOCIAL_SCIENCE = "social_science"
|
| 56 |
+
|
| 57 |
+
# --------------------------
|
| 58 |
+
# DATA MODELS
|
| 59 |
+
# --------------------------
|
| 60 |
+
@dataclass
|
| 61 |
+
class Evidence:
|
| 62 |
+
evidence_id: str
|
| 63 |
+
strength: float
|
| 64 |
+
reliability: float
|
| 65 |
+
source_quality: float = 0.8
|
| 66 |
+
contradictory: bool = False
|
| 67 |
+
timestamp: datetime = field(default_factory=datetime.utcnow)
|
| 68 |
+
domain: Optional[KnowledgeDomain] = None
|
| 69 |
+
|
| 70 |
+
def __post_init__(self):
|
| 71 |
+
if not (0.0 <= self.strength <= 1.0):
|
| 72 |
+
raise ValueError("Evidence strength must be between 0.0 and 1.0")
|
| 73 |
+
if not (0.0 <= self.reliability <= 1.0):
|
| 74 |
+
raise ValueError("Evidence reliability must be between 0.0 and 1.0")
|
| 75 |
+
if not (0.0 <= self.source_quality <= 1.0):
|
| 76 |
+
raise ValueError("Source quality must be between 0.0 and 1.0")
|
| 77 |
+
|
| 78 |
+
@property
|
| 79 |
+
def weighted_strength(self) -> float:
|
| 80 |
+
return self.strength * self.reliability * self.source_quality
|
| 81 |
+
|
| 82 |
+
def to_dict(self) -> Dict:
|
| 83 |
+
return {
|
| 84 |
+
'evidence_id': self.evidence_id,
|
| 85 |
+
'strength': self.strength,
|
| 86 |
+
'reliability': self.reliability,
|
| 87 |
+
'source_quality': self.source_quality,
|
| 88 |
+
'contradictory': self.contradictory,
|
| 89 |
+
'timestamp': self.timestamp.isoformat(),
|
| 90 |
+
'domain': self.domain.value if self.domain else None,
|
| 91 |
+
'weighted_strength': self.weighted_strength
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
@dataclass
|
| 95 |
+
class UniversalClaim:
|
| 96 |
+
claim_id: str
|
| 97 |
+
content: str
|
| 98 |
+
evidence_chain: List[Evidence] = field(default_factory=list)
|
| 99 |
+
reasoning_modes: List[ReasoningMode] = field(default_factory=list)
|
| 100 |
+
sub_domains: List[KnowledgeDomain] = field(default_factory=list)
|
| 101 |
+
causal_mechanisms: List[str] = field(default_factory=list)
|
| 102 |
+
expected_validity: Optional[float] = None
|
| 103 |
+
metadata: Dict = field(default_factory=dict)
|
| 104 |
+
|
| 105 |
+
def __post_init__(self):
|
| 106 |
+
if not self.content.strip():
|
| 107 |
+
raise ValueError("Claim content cannot be empty")
|
| 108 |
+
if self.expected_validity is not None:
|
| 109 |
+
if not (0.0 <= self.expected_validity <= 1.0):
|
| 110 |
+
raise ValueError("Expected validity must be between 0.0 and 1.0")
|
| 111 |
+
if not self.claim_id:
|
| 112 |
+
self.claim_id = self._generate_claim_id()
|
| 113 |
+
|
| 114 |
+
def _generate_claim_id(self) -> str:
|
| 115 |
+
content_hash = hashlib.md5(self.content.encode()).hexdigest()
|
| 116 |
+
return f"claim_{content_hash[:12]}"
|
| 117 |
+
|
| 118 |
+
@property
|
| 119 |
+
def evidence_summary(self) -> Dict:
|
| 120 |
+
if not self.evidence_chain:
|
| 121 |
+
return {'count': 0, 'avg_strength': 0.0, 'avg_reliability': 0.0}
|
| 122 |
+
|
| 123 |
+
strengths = [e.weighted_strength for e in self.evidence_chain]
|
| 124 |
+
reliabilities = [e.reliability for e in self.evidence_chain]
|
| 125 |
+
|
| 126 |
+
return {
|
| 127 |
+
'count': len(self.evidence_chain),
|
| 128 |
+
'avg_strength': np.mean(strengths),
|
| 129 |
+
'avg_reliability': np.mean(reliabilities),
|
| 130 |
+
'contradictory_count': sum(1 for e in self.evidence_chain if e.contradictory)
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
def to_dict(self) -> Dict:
|
| 134 |
+
return {
|
| 135 |
+
'claim_id': self.claim_id,
|
| 136 |
+
'content': self.content,
|
| 137 |
+
'evidence_chain': [e.to_dict() for e in self.evidence_chain],
|
| 138 |
+
'reasoning_modes': [m.value for m in self.reasoning_modes],
|
| 139 |
+
'sub_domains': [d.value for d in self.sub_domains],
|
| 140 |
+
'causal_mechanisms': self.causal_mechanisms,
|
| 141 |
+
'expected_validity': self.expected_validity,
|
| 142 |
+
'evidence_summary': self.evidence_summary,
|
| 143 |
+
'metadata': self.metadata
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
@dataclass
|
| 147 |
+
class RealTimeDataSource:
|
| 148 |
+
source_id: str
|
| 149 |
+
endpoint: str
|
| 150 |
+
domain: KnowledgeDomain
|
| 151 |
+
refresh_interval: int = 3600
|
| 152 |
+
last_updated: datetime = field(default_factory=datetime.utcnow)
|
| 153 |
+
reliability: float = 0.85
|
| 154 |
+
priority: int = 1
|
| 155 |
+
|
| 156 |
+
def needs_refresh(self) -> bool:
|
| 157 |
+
return (datetime.utcnow() - self.last_updated).total_seconds() > self.refresh_interval
|
| 158 |
+
|
| 159 |
+
@dataclass
|
| 160 |
+
class DomainConstraint:
|
| 161 |
+
domain: KnowledgeDomain
|
| 162 |
+
min_evidence: int = 3
|
| 163 |
+
min_reliability: float = 0.7
|
| 164 |
+
required_reasoning_modes: List[ReasoningMode] = field(default_factory=list)
|
| 165 |
+
complexity_factor: float = 1.0
|
| 166 |
+
|
| 167 |
+
# --------------------------
|
| 168 |
+
# CORE VALIDATOR
|
| 169 |
+
# --------------------------
|
| 170 |
+
class EnhancedAGIValidator:
|
| 171 |
+
def __init__(self,
|
| 172 |
+
mcp_enabled: bool = True,
|
| 173 |
+
mcp_timeout: int = 15,
|
| 174 |
+
max_history: int = 100,
|
| 175 |
+
cache_enabled: bool = True,
|
| 176 |
+
real_time_sources: List[RealTimeDataSource] = None,
|
| 177 |
+
domain_constraints: Dict[KnowledgeDomain, DomainConstraint] = None):
|
| 178 |
+
self.mcp_enabled = mcp_enabled
|
| 179 |
+
self.mcp_timeout = mcp_timeout
|
| 180 |
+
self.max_history = max_history
|
| 181 |
+
self.cache_enabled = cache_enabled
|
| 182 |
+
self.mcp_url = "https://agents-mcp-hackathon-consilium-mcp.hf.space/run/predict"
|
| 183 |
+
self.validation_history = []
|
| 184 |
+
self.validation_cache = {}
|
| 185 |
+
self._session = None
|
| 186 |
+
self._mcp_failures = 0
|
| 187 |
+
|
| 188 |
+
# Real-time data and domain constraints
|
| 189 |
+
self.real_time_sources = real_time_sources or self._default_real_time_sources()
|
| 190 |
+
self.domain_constraints = domain_constraints or self._default_domain_constraints()
|
| 191 |
+
self.data_cache = {}
|
| 192 |
+
|
| 193 |
+
logger.info("Enhanced AGI Validator initialized")
|
| 194 |
+
|
| 195 |
+
# --------------------------
|
| 196 |
+
# HELPER METHODS
|
| 197 |
+
# --------------------------
|
| 198 |
+
def _default_real_time_sources(self) -> List[RealTimeDataSource]:
|
| 199 |
+
return [
|
| 200 |
+
RealTimeDataSource("scientific_journals", "https://api.sciencedirect.com/search",
|
| 201 |
+
KnowledgeDomain.SCIENCE, refresh_interval=86400),
|
| 202 |
+
RealTimeDataSource("medical_db", "https://api.medicalevidence.org/v1/claims",
|
| 203 |
+
KnowledgeDomain.MEDICINE, refresh_interval=3600),
|
| 204 |
+
RealTimeDataSource("historical_archive", "https://api.historydb.org/records",
|
| 205 |
+
KnowledgeDomain.HISTORY, refresh_interval=604800)
|
| 206 |
+
]
|
| 207 |
+
|
| 208 |
+
def _default_domain_constraints(self) -> Dict[KnowledgeDomain, DomainConstraint]:
|
| 209 |
+
return {
|
| 210 |
+
KnowledgeDomain.MEDICINE: DomainConstraint(
|
| 211 |
+
min_evidence=5, min_reliability=0.85,
|
| 212 |
+
required_reasoning_modes=[ReasoningMode.CAUSAL, ReasoningMode.BAYESIAN],
|
| 213 |
+
complexity_factor=1.2),
|
| 214 |
+
KnowledgeDomain.SCIENCE: DomainConstraint(
|
| 215 |
+
min_evidence=3, min_reliability=0.75,
|
| 216 |
+
required_reasoning_modes=[ReasoningMode.DEDUCTIVE],
|
| 217 |
+
complexity_factor=1.0),
|
| 218 |
+
KnowledgeDomain.HISTORY: DomainConstraint(
|
| 219 |
+
min_evidence=2, min_reliability=0.65, complexity_factor=0.9)
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
def _get_cache_key(self, claim: UniversalClaim) -> str:
|
| 223 |
+
claim_data = claim.to_dict()
|
| 224 |
+
claim_json = json.dumps(claim_data, sort_keys=True)
|
| 225 |
+
return hashlib.sha256(claim_json.encode()).hexdigest()
|
| 226 |
+
|
| 227 |
+
@asynccontextmanager
|
| 228 |
+
async def _get_session(self):
|
| 229 |
+
if self._session is None:
|
| 230 |
+
connector = aiohttp.TCPConnector(limit=10, limit_per_host=5)
|
| 231 |
+
timeout = aiohttp.ClientTimeout(total=self.mcp_timeout)
|
| 232 |
+
self._session = aiohttp.ClientSession(connector=connector, timeout=timeout)
|
| 233 |
+
|
| 234 |
+
try:
|
| 235 |
+
yield self._session
|
| 236 |
+
except Exception as e:
|
| 237 |
+
logger.error(f"Session error: {e}")
|
| 238 |
+
raise
|
| 239 |
+
|
| 240 |
+
async def close(self):
|
| 241 |
+
if self._session:
|
| 242 |
+
await self._session.close()
|
| 243 |
+
self._session = None
|
| 244 |
+
|
| 245 |
+
# --------------------------
|
| 246 |
+
# REAL-TIME DATA INTEGRATION
|
| 247 |
+
# --------------------------
|
| 248 |
+
async def _fetch_real_time_data(self, source: RealTimeDataSource, query: str) -> Dict:
|
| 249 |
+
cache_key = f"{source.source_id}_{hashlib.md5(query.encode()).hexdigest()}"
|
| 250 |
+
|
| 251 |
+
if self.cache_enabled and cache_key in self.data_cache:
|
| 252 |
+
if not source.needs_refresh():
|
| 253 |
+
return self.data_cache[cache_key]
|
| 254 |
+
|
| 255 |
+
try:
|
| 256 |
+
async with self._get_session() as session:
|
| 257 |
+
params = {"query": query, "limit": 5, "format": "json"}
|
| 258 |
+
headers = {"Accept": "application/json"}
|
| 259 |
+
|
| 260 |
+
async with session.get(
|
| 261 |
+
source.endpoint, params=params, headers=headers,
|
| 262 |
+
timeout=source.refresh_interval/10
|
| 263 |
+
) as response:
|
| 264 |
+
if response.status == 200:
|
| 265 |
+
data = await response.json()
|
| 266 |
+
result = {
|
| 267 |
+
"data": data,
|
| 268 |
+
"timestamp": datetime.utcnow(),
|
| 269 |
+
"source": source.source_id
|
| 270 |
+
}
|
| 271 |
+
self.data_cache[cache_key] = result
|
| 272 |
+
source.last_updated = datetime.utcnow()
|
| 273 |
+
return result
|
| 274 |
+
else:
|
| 275 |
+
logger.warning(f"Data source {source.source_id} returned status {response.status}")
|
| 276 |
+
return {"error": f"HTTP {response.status}", "source": source.source_id}
|
| 277 |
+
|
| 278 |
+
except asyncio.TimeoutError:
|
| 279 |
+
logger.warning(f"Data source {source.source_id} timed out")
|
| 280 |
+
return {"error": "timeout", "source": source.source_id}
|
| 281 |
+
except Exception as e:
|
| 282 |
+
logger.error(f"Error fetching from {source.source_id}: {str(e)}")
|
| 283 |
+
return {"error": str(e), "source": source.source_id}
|
| 284 |
+
|
| 285 |
+
async def _enrich_evidence_with_real_time_data(self, claim: UniversalClaim) -> UniversalClaim:
|
| 286 |
+
domain_sources = [
|
| 287 |
+
s for s in sorted(self.real_time_sources, key=lambda x: x.priority, reverse=True)
|
| 288 |
+
if any(d in claim.sub_domains for d in [s.domain])
|
| 289 |
+
]
|
| 290 |
+
|
| 291 |
+
if not domain_sources:
|
| 292 |
+
return claim
|
| 293 |
+
|
| 294 |
+
tasks = [self._fetch_real_time_data(source, claim.content) for source in domain_sources]
|
| 295 |
+
results = await asyncio.gather(*tasks)
|
| 296 |
+
|
| 297 |
+
new_evidence = []
|
| 298 |
+
for result in results:
|
| 299 |
+
if "error" in result:
|
| 300 |
+
continue
|
| 301 |
+
|
| 302 |
+
evidence_strength = 0.7
|
| 303 |
+
evidence_reliability = result["source"].get("reliability", 0.8)
|
| 304 |
+
|
| 305 |
+
new_evidence.append(Evidence(
|
| 306 |
+
evidence_id=f"rt_{result['source']}_{time.time_ns()}",
|
| 307 |
+
strength=evidence_strength,
|
| 308 |
+
reliability=evidence_reliability,
|
| 309 |
+
source_quality=0.9,
|
| 310 |
+
domain=next((s for s in self.real_time_sources if s.source_id == result["source"]), None).domain,
|
| 311 |
+
timestamp=datetime.utcnow()
|
| 312 |
+
))
|
| 313 |
+
|
| 314 |
+
claim.evidence_chain.extend(new_evidence)
|
| 315 |
+
return claim
|
| 316 |
+
|
| 317 |
+
# --------------------------
|
| 318 |
+
# DOMAIN CONSTRAINT HANDLING
|
| 319 |
+
# --------------------------
|
| 320 |
+
def _apply_domain_constraints(self, claim: UniversalClaim) -> Tuple[UniversalClaim, List[str]]:
|
| 321 |
+
constraint_violations = []
|
| 322 |
+
enhanced_claim = deepcopy(claim)
|
| 323 |
+
|
| 324 |
+
for domain in claim.sub_domains:
|
| 325 |
+
constraint = self.domain_constraints.get(domain)
|
| 326 |
+
if not constraint:
|
| 327 |
+
continue
|
| 328 |
+
|
| 329 |
+
domain_evidence = [e for e in claim.evidence_chain if e.domain == domain]
|
| 330 |
+
if len(domain_evidence) < constraint.min_evidence:
|
| 331 |
+
constraint_violations.append(
|
| 332 |
+
f"Domain {domain.value} requires at least {constraint.min_evidence} evidence pieces"
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
if domain_evidence:
|
| 336 |
+
avg_reliability = np.mean([e.reliability for e in domain_evidence])
|
| 337 |
+
if avg_reliability < constraint.min_reliability:
|
| 338 |
+
constraint_violations.append(
|
| 339 |
+
f"Domain {domain.value} requires minimum evidence reliability of {constraint.min_reliability}"
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
for mode in constraint.required_reasoning_modes:
|
| 343 |
+
if mode not in claim.reasoning_modes:
|
| 344 |
+
enhanced_claim.reasoning_modes.append(mode)
|
| 345 |
+
constraint_violations.append(
|
| 346 |
+
f"Added required reasoning mode {mode.value} for domain {domain.value}"
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
return enhanced_claim, constraint_violations
|
| 350 |
+
|
| 351 |
+
# --------------------------
|
| 352 |
+
# MCP CONSENSUS COMPONENT
|
| 353 |
+
# --------------------------
|
| 354 |
+
async def _get_mcp_consensus(self, claim: UniversalClaim) -> Dict:
|
| 355 |
+
if not self.mcp_enabled:
|
| 356 |
+
logger.info("mCP consensus protocol disabled")
|
| 357 |
+
return self._get_fallback_consensus("mCP disabled")
|
| 358 |
+
|
| 359 |
+
if self._mcp_failures >= 3:
|
| 360 |
+
logger.error("mCP circuit breaker triggered - using fallback")
|
| 361 |
+
return self._get_fallback_consensus("circuit_breaker")
|
| 362 |
+
|
| 363 |
+
cache_key = self._get_cache_key(claim) if self.cache_enabled else None
|
| 364 |
+
if cache_key and cache_key in self.validation_cache:
|
| 365 |
+
logger.info("Using cached mCP consensus")
|
| 366 |
+
return self.validation_cache[cache_key]
|
| 367 |
+
|
| 368 |
+
payload = {
|
| 369 |
+
"claim_text": claim.content,
|
| 370 |
+
"domains": [d.value for d in claim.sub_domains],
|
| 371 |
+
"reasoning_modes": [m.value for m in claim.reasoning_modes],
|
| 372 |
+
"evidence_count": len(claim.evidence_chain),
|
| 373 |
+
"evidence_summary": claim.evidence_summary,
|
| 374 |
+
"causal_mechanisms": claim.causal_mechanisms,
|
| 375 |
+
"validation_mode": "full_mesh",
|
| 376 |
+
"rounds": 3
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
start_time = time.monotonic()
|
| 380 |
+
|
| 381 |
+
try:
|
| 382 |
+
async with self._get_session() as session:
|
| 383 |
+
async with session.post(self.mcp_url, json=payload) as response:
|
| 384 |
+
if response.status == 200:
|
| 385 |
+
result = await response.json()
|
| 386 |
+
elapsed = time.monotonic() - start_time
|
| 387 |
+
|
| 388 |
+
mcp_result = {
|
| 389 |
+
**result.get("data", {}),
|
| 390 |
+
"processing_time": elapsed,
|
| 391 |
+
"reliability": 1.0,
|
| 392 |
+
"cache_hit": False
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
if cache_key:
|
| 396 |
+
self.validation_cache[cache_key] = mcp_result
|
| 397 |
+
|
| 398 |
+
logger.info(f"mCP consensus received in {elapsed:.2f}s")
|
| 399 |
+
self._mcp_failures = 0
|
| 400 |
+
return mcp_result
|
| 401 |
+
else:
|
| 402 |
+
logger.warning(f"mCP returned status {response.status}")
|
| 403 |
+
self._mcp_failures += 1
|
| 404 |
+
return self._get_fallback_consensus(f"HTTP {response.status}")
|
| 405 |
+
|
| 406 |
+
except asyncio.TimeoutError:
|
| 407 |
+
logger.warning("mCP request timed out")
|
| 408 |
+
self._mcp_failures += 1
|
| 409 |
+
return self._get_fallback_consensus("timeout")
|
| 410 |
+
except Exception as e:
|
| 411 |
+
logger.exception(f"Error in mCP request: {str(e)}")
|
| 412 |
+
self._mcp_failures += 1
|
| 413 |
+
return self._get_fallback_consensus(f"error: {str(e)}")
|
| 414 |
+
|
| 415 |
+
def _get_fallback_consensus(self, reason: str = "unknown") -> Dict:
|
| 416 |
+
return {
|
| 417 |
+
"consensus_score": 0.5,
|
| 418 |
+
"confidence_interval": [0.4, 0.6],
|
| 419 |
+
"expert_notes": [f"Consensus service unavailable: {reason}"],
|
| 420 |
+
"reliability": 0.0,
|
| 421 |
+
"processing_time": 0.0,
|
| 422 |
+
"fallback_reason": reason
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
# --------------------------
|
| 426 |
+
# ANALYTICAL COMPONENTS
|
| 427 |
+
# --------------------------
|
| 428 |
+
async def _perform_reasoning_analysis(self, claim: UniversalClaim) -> Dict:
|
| 429 |
+
start_time = time.monotonic()
|
| 430 |
+
|
| 431 |
+
try:
|
| 432 |
+
results = {}
|
| 433 |
+
|
| 434 |
+
# Bayesian reasoning
|
| 435 |
+
if ReasoningMode.BAYESIAN in claim.reasoning_modes:
|
| 436 |
+
prior = 0.5
|
| 437 |
+
evidence_weights = [e.weighted_strength for e in claim.evidence_chain]
|
| 438 |
+
if evidence_weights:
|
| 439 |
+
likelihood = np.mean(evidence_weights)
|
| 440 |
+
posterior = (likelihood * prior) / ((likelihood * prior) + ((1 - likelihood) * (1 - prior)))
|
| 441 |
+
results['bayesian'] = {
|
| 442 |
+
'prior': prior,
|
| 443 |
+
'likelihood': likelihood,
|
| 444 |
+
'posterior': posterior
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
# Causal reasoning
|
| 448 |
+
if ReasoningMode.CAUSAL in claim.reasoning_modes:
|
| 449 |
+
causal_strength = len(claim.causal_mechanisms) / max(5, len(claim.causal_mechanisms))
|
| 450 |
+
results['causal'] = {
|
| 451 |
+
'causal_coherence': min(0.95, 0.5 + causal_strength * 0.4),
|
| 452 |
+
'mechanism_count': len(claim.causal_mechanisms)
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
# Deductive reasoning
|
| 456 |
+
if ReasoningMode.DEDUCTIVE in claim.reasoning_modes:
|
| 457 |
+
contradictory_evidence = sum(1 for e in claim.evidence_chain if e.contradictory)
|
| 458 |
+
consistency = max(0.1, 1.0 - (contradictory_evidence / max(1, len(claim.evidence_chain))))
|
| 459 |
+
results['deductive'] = {'logical_consistency': consistency}
|
| 460 |
+
|
| 461 |
+
processing_time = time.monotonic() - start_time
|
| 462 |
+
|
| 463 |
+
return {
|
| 464 |
+
**results,
|
| 465 |
+
'processing_time': processing_time,
|
| 466 |
+
'reasoning_modes_used': [m.value for m in claim.reasoning_modes]
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
except Exception as e:
|
| 470 |
+
logger.error(f"Reasoning analysis failed: {str(e)}")
|
| 471 |
+
return {
|
| 472 |
+
'error': f"Reasoning analysis failed: {str(e)}",
|
| 473 |
+
'processing_time': time.monotonic() - start_time
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
async def _analyze_evidence_quality(self, claim: UniversalClaim) -> Dict:
|
| 477 |
+
start_time = time.monotonic()
|
| 478 |
+
|
| 479 |
+
try:
|
| 480 |
+
if not claim.evidence_chain:
|
| 481 |
+
return {
|
| 482 |
+
'evidence_score': 0.0,
|
| 483 |
+
'evidence_count': 0,
|
| 484 |
+
'quality_factors': {'no_evidence': True},
|
| 485 |
+
'processing_time': time.monotonic() - start_time
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
strengths = [e.weighted_strength for e in claim.evidence_chain]
|
| 489 |
+
reliabilities = [e.reliability for e in claim.evidence_chain]
|
| 490 |
+
source_qualities = [e.source_quality for e in claim.evidence_chain]
|
| 491 |
+
|
| 492 |
+
domains = set(e.domain for e in claim.evidence_chain if e.domain)
|
| 493 |
+
domain_diversity = len(domains) / max(1, len(KnowledgeDomain))
|
| 494 |
+
|
| 495 |
+
contradictory_count = sum(1 for e in claim.evidence_chain if e.contradictory)
|
| 496 |
+
contradiction_penalty = contradictory_count / len(claim.evidence_chain)
|
| 497 |
+
|
| 498 |
+
base_score = np.mean(strengths)
|
| 499 |
+
reliability_bonus = (np.mean(reliabilities) - 0.5) * 0.2
|
| 500 |
+
source_bonus = (np.mean(source_qualities) - 0.5) * 0.1
|
| 501 |
+
diversity_bonus = domain_diversity * 0.1
|
| 502 |
+
|
| 503 |
+
evidence_score = max(0.0, min(1.0,
|
| 504 |
+
base_score + reliability_bonus + source_bonus + diversity_bonus - contradiction_penalty
|
| 505 |
+
))
|
| 506 |
+
|
| 507 |
+
return {
|
| 508 |
+
'evidence_score': evidence_score,
|
| 509 |
+
'evidence_count': len(claim.evidence_chain),
|
| 510 |
+
'quality_factors': {
|
| 511 |
+
'base_score': base_score,
|
| 512 |
+
'reliability_bonus': reliability_bonus,
|
| 513 |
+
'source_bonus': source_bonus,
|
| 514 |
+
'diversity_bonus': diversity_bonus,
|
| 515 |
+
'contradiction_penalty': contradiction_penalty,
|
| 516 |
+
'domain_diversity': domain_diversity
|
| 517 |
+
},
|
| 518 |
+
'processing_time': time.monotonic() - start_time
|
| 519 |
+
}
|
| 520 |
+
|
| 521 |
+
except Exception as e:
|
| 522 |
+
logger.error(f"Evidence analysis failed: {str(e)}")
|
| 523 |
+
return {
|
| 524 |
+
'evidence_score': 0.5,
|
| 525 |
+
'evidence_count': len(claim.evidence_chain),
|
| 526 |
+
'error': str(e),
|
| 527 |
+
'processing_time': time.monotonic() - start_time
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
async def _metacognitive_assessment(self, claim: UniversalClaim) -> Dict:
|
| 531 |
+
start_time = time.monotonic()
|
| 532 |
+
|
| 533 |
+
try:
|
| 534 |
+
biases_detected = []
|
| 535 |
+
|
| 536 |
+
# Confirmation bias detection
|
| 537 |
+
if claim.evidence_chain:
|
| 538 |
+
supporting = sum(1 for e in claim.evidence_chain if not e.contradictory)
|
| 539 |
+
contradicting = sum(1 for e in claim.evidence_chain if e.contradictory)
|
| 540 |
+
if supporting > 0 and contradicting == 0:
|
| 541 |
+
biases_detected.append("potential_confirmation_bias")
|
| 542 |
+
|
| 543 |
+
# Availability bias
|
| 544 |
+
recent_evidence = sum(1 for e in claim.evidence_chain
|
| 545 |
+
if (datetime.utcnow() - e.timestamp).days < 30)
|
| 546 |
+
if recent_evidence / max(1, len(claim.evidence_chain)) > 0.8:
|
| 547 |
+
biases_detected.append("potential_availability_bias")
|
| 548 |
+
|
| 549 |
+
# Calculate overall quality
|
| 550 |
+
complexity_factor = len(claim.sub_domains) / max(1, len(KnowledgeDomain))
|
| 551 |
+
reasoning_diversity = len(claim.reasoning_modes) / max(1, len(ReasoningMode))
|
| 552 |
+
|
| 553 |
+
overall_quality = (
|
| 554 |
+
0.4 * (1.0 - len(biases_detected) / 5) +
|
| 555 |
+
0.3 * complexity_factor +
|
| 556 |
+
0.3 * reasoning_diversity
|
| 557 |
+
)
|
| 558 |
+
|
| 559 |
+
return {
|
| 560 |
+
'overall_quality': max(0.0, min(1.0, overall_quality)),
|
| 561 |
+
'detected_biases': biases_detected,
|
| 562 |
+
'bias_score': len(biases_detected) / 5,
|
| 563 |
+
'complexity_factor': complexity_factor,
|
| 564 |
+
'reasoning_diversity': reasoning_diversity,
|
| 565 |
+
'processing_time': time.monotonic() - start_time
|
| 566 |
+
}
|
| 567 |
+
except Exception as e:
|
| 568 |
+
logger.error(f"Metacognitive assessment failed: {str(e)}")
|
| 569 |
+
return {
|
| 570 |
+
'error': f"Metacognitive assessment failed: {str(e)}",
|
| 571 |
+
'processing_time': time.monotonic() - start_time
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
def _calculate_dynamic_threshold(self, evidence_analysis: Dict, complexity_analysis: Dict) -> float:
|
| 575 |
+
try:
|
| 576 |
+
base_threshold = 0.6
|
| 577 |
+
evidence_score = evidence_analysis.get('evidence_score', 0.5)
|
| 578 |
+
evidence_count = evidence_analysis.get('evidence_count', 0)
|
| 579 |
+
contradiction_penalty = evidence_analysis.get('quality_factors', {}).get('contradiction_penalty', 0)
|
| 580 |
+
|
| 581 |
+
complexity_score = complexity_analysis.get('overall_complexity', 0.5)
|
| 582 |
+
domain_complexity = complexity_analysis.get('complexity_factors', {}).get('domain_complexity', 0)
|
| 583 |
+
reasoning_complexity = complexity_analysis.get('complexity_factors', {}).get('reasoning_complexity', 0)
|
| 584 |
+
|
| 585 |
+
evidence_factor = max(0.0, 0.2 * (0.7 - evidence_score))
|
| 586 |
+
count_factor = max(0.0, 0.15 * (1 - min(1.0, evidence_count / 5)))
|
| 587 |
+
contradiction_factor = min(0.2, contradiction_penalty * 0.3)
|
| 588 |
+
complexity_factor = min(0.25, complexity_score * 0.3)
|
| 589 |
+
|
| 590 |
+
adjustment = evidence_factor + count_factor + contradiction_factor + complexity_factor
|
| 591 |
+
dynamic_threshold = base_threshold - adjustment
|
| 592 |
+
return max(0.3, min(0.8, dynamic_threshold))
|
| 593 |
+
|
| 594 |
+
except Exception as e:
|
| 595 |
+
logger.error(f"Dynamic threshold calculation failed: {str(e)}")
|
| 596 |
+
return 0.6
|
| 597 |
+
|
| 598 |
+
# --------------------------
|
| 599 |
+
# CORE VALIDATION PIPELINE
|
| 600 |
+
# --------------------------
|
| 601 |
+
async def validate_knowledge_claim(self, claim: UniversalClaim) -> Dict:
|
| 602 |
+
try:
|
| 603 |
+
# Apply domain constraints
|
| 604 |
+
enhanced_claim, constraint_violations = self._apply_domain_constraints(claim)
|
| 605 |
+
|
| 606 |
+
# Enhance with real-time data
|
| 607 |
+
enhanced_claim = await self._enrich_evidence_with_real_time_data(enhanced_claim)
|
| 608 |
+
|
| 609 |
+
# Parallel processing of analytical components
|
| 610 |
+
evidence_task = self._analyze_evidence_quality(enhanced_claim)
|
| 611 |
+
reasoning_task = self._perform_reasoning_analysis(enhanced_claim)
|
| 612 |
+
metacog_task = self._metacognitive_assessment(enhanced_claim)
|
| 613 |
+
mcp_task = self._get_mcp_consensus(enhanced_claim)
|
| 614 |
+
|
| 615 |
+
results = await asyncio.gather(
|
| 616 |
+
evidence_task, reasoning_task, metacog_task, mcp_task
|
| 617 |
+
)
|
| 618 |
+
evidence_analysis, reasoning_analysis, metacog_analysis, mcp_analysis = results
|
| 619 |
+
|
| 620 |
+
# Dynamic threshold calculation
|
| 621 |
+
dynamic_threshold = self._calculate_dynamic_threshold(
|
| 622 |
+
evidence_analysis, metacog_analysis
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
# Calculate overall validity
|
| 626 |
+
evidence_weight = 0.4
|
| 627 |
+
reasoning_weight = 0.3
|
| 628 |
+
mcp_weight = 0.2
|
| 629 |
+
metacog_weight = 0.1
|
| 630 |
+
|
| 631 |
+
evidence_score = evidence_analysis.get('evidence_score', 0.0)
|
| 632 |
+
reasoning_score = reasoning_analysis.get('bayesian', {}).get('posterior', 0.5) if 'bayesian' in reasoning_analysis else 0.5
|
| 633 |
+
mcp_score = mcp_analysis.get('consensus_score', 0.5)
|
| 634 |
+
metacog_score = metacog_analysis.get('overall_quality', 0.5)
|
| 635 |
+
|
| 636 |
+
overall_validity = (
|
| 637 |
+
evidence_weight * evidence_score +
|
| 638 |
+
reasoning_weight * reasoning_score +
|
| 639 |
+
mcp_weight * mcp_score +
|
| 640 |
+
metacog_weight * metacog_score
|
| 641 |
+
)
|
| 642 |
+
|
| 643 |
+
# Determine validation status
|
| 644 |
+
status = ValidationStatus.FAILURE
|
| 645 |
+
if overall_validity >= dynamic_threshold:
|
| 646 |
+
status = ValidationStatus.SUCCESS if overall_validity >= 0.8 else ValidationStatus.PARTIAL_SUCCESS
|
| 647 |
+
elif evidence_analysis.get('evidence_count', 0) < 3:
|
| 648 |
+
status = ValidationStatus.INSUFFICIENT_DATA
|
| 649 |
+
|
| 650 |
+
# Apply domain complexity adjustments
|
| 651 |
+
complexity_adjustment = 1.0
|
| 652 |
+
for domain in enhanced_claim.sub_domains:
|
| 653 |
+
if domain in self.domain_constraints:
|
| 654 |
+
constraint = self.domain_constraints[domain]
|
| 655 |
+
complexity_adjustment *= constraint.complexity_factor
|
| 656 |
+
overall_validity = min(1.0, overall_validity * complexity_adjustment)
|
| 657 |
+
|
| 658 |
+
# Prepare report
|
| 659 |
+
report = {
|
| 660 |
+
"claim_id": enhanced_claim.claim_id,
|
| 661 |
+
"status": status.value,
|
| 662 |
+
"overall_validity": overall_validity,
|
| 663 |
+
"dynamic_threshold": dynamic_threshold,
|
| 664 |
+
"evidence_analysis": evidence_analysis,
|
| 665 |
+
"reasoning_analysis": reasoning_analysis,
|
| 666 |
+
"metacognitive_analysis": metacog_analysis,
|
| 667 |
+
"mcp_analysis": mcp_analysis,
|
| 668 |
+
"domain_constraints": {
|
| 669 |
+
"constraint_violations": constraint_violations,
|
| 670 |
+
"constraints_applied": [d.value for d in enhanced_claim.sub_domains
|
| 671 |
+
if d in self.domain_constraints]
|
| 672 |
+
},
|
| 673 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
# Add to history
|
| 677 |
+
self.validation_history.append(report)
|
| 678 |
+
if len(self.validation_history) > self.max_history:
|
| 679 |
+
self.validation_history.pop(0)
|
| 680 |
+
|
| 681 |
+
return report
|
| 682 |
+
|
| 683 |
+
except Exception as e:
|
| 684 |
+
logger.exception(f"Validation failed: {str(e)}")
|
| 685 |
+
return await self._fallback_validation(claim, str(e))
|
| 686 |
+
|
| 687 |
+
async def _fallback_validation(self, claim: UniversalClaim, error: str) -> Dict:
|
| 688 |
+
try:
|
| 689 |
+
evidence_count = len(claim.evidence_chain)
|
| 690 |
+
evidence_score = np.mean([e.weighted_strength for e in claim.evidence_chain]) if evidence_count > 0 else 0.0
|
| 691 |
+
validity = min(0.9, max(0.1, evidence_score * 0.8))
|
| 692 |
+
|
| 693 |
+
return {
|
| 694 |
+
"claim_id": claim.claim_id,
|
| 695 |
+
"status": ValidationStatus.ERROR.value,
|
| 696 |
+
"fallback_validity": validity,
|
| 697 |
+
"evidence_count": evidence_count,
|
| 698 |
+
"error": error,
|
| 699 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 700 |
+
"recommendations": [
|
| 701 |
+
"System encountered an error - results are approximate",
|
| 702 |
+
"Retry validation after system maintenance"
|
| 703 |
+
]
|
| 704 |
+
}
|
| 705 |
+
except Exception as fallback_error:
|
| 706 |
+
logger.error(f"Fallback validation failed: {str(fallback_error)}")
|
| 707 |
+
return {
|
| 708 |
+
"claim_id": claim.claim_id,
|
| 709 |
+
"status": ValidationStatus.ERROR.value,
|
| 710 |
+
"error": f"Primary: {error}, Fallback: {str(fallback_error)}",
|
| 711 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
# --------------------------
|
| 715 |
+
# ADDITIONAL FUNCTIONALITY
|
| 716 |
+
# --------------------------
|
| 717 |
+
def export_validation_history(self, format: str = "json") -> Union[Dict, str]:
|
| 718 |
+
if format == "json":
|
| 719 |
+
return self.validation_history
|
| 720 |
+
elif format == "csv":
|
| 721 |
+
csv_lines = ["claim_id,status,validity,timestamp"]
|
| 722 |
+
for entry in self.validation_history:
|
| 723 |
+
csv_lines.append(
|
| 724 |
+
f"{entry['claim_id']},{entry['status']},{entry.get('overall_validity', 0.0)},{entry['timestamp']}"
|
| 725 |
+
)
|
| 726 |
+
return "\n".join(csv_lines)
|
| 727 |
+
else:
|
| 728 |
+
return str(self.validation_history)
|
| 729 |
+
|
| 730 |
+
def get_validation_statistics(self) -> Dict:
|
| 731 |
+
status_counts = {status.value: 0 for status in ValidationStatus}
|
| 732 |
+
validities = []
|
| 733 |
+
|
| 734 |
+
for entry in self.validation_history:
|
| 735 |
+
status_counts[entry["status"]] += 1
|
| 736 |
+
if "overall_validity" in entry:
|
| 737 |
+
validities.append(entry["overall_validity"])
|
| 738 |
+
|
| 739 |
+
return {
|
| 740 |
+
"total_validations": len(self.validation_history),
|
| 741 |
+
"status_distribution": status_counts,
|
| 742 |
+
"average_validity": np.mean(validities) if validities else 0.0,
|
| 743 |
+
"median_validity": np.median(validities) if validities else 0.0,
|
| 744 |
+
"last_validation": self.validation_history[-1] if self.validation_history else None
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
# --------------------------
|
| 748 |
+
# UI COMPONENT
|
| 749 |
+
# --------------------------
|
| 750 |
+
class AGIValidatorUI:
|
| 751 |
+
def __init__(self, validator: EnhancedAGIValidator):
|
| 752 |
+
self.validator = validator
|
| 753 |
+
self.app = FastAPI()
|
| 754 |
+
self._setup_routes()
|
| 755 |
+
|
| 756 |
+
def _setup_routes(self):
|
| 757 |
+
self.app.post("/validate")(self.validate_claim_endpoint)
|
| 758 |
+
self.app.get("/history")(self.get_history)
|
| 759 |
+
self.app.get("/stats")(self.get_statistics)
|
| 760 |
+
|
| 761 |
+
async def validate_claim_endpoint(self, claim_data: dict):
|
| 762 |
+
try:
|
| 763 |
+
claim = UniversalClaim(
|
| 764 |
+
claim_id=claim_data.get("claim_id", ""),
|
| 765 |
+
content=claim_data["content"],
|
| 766 |
+
evidence_chain=[
|
| 767 |
+
Evidence(**e) for e in claim_data.get("evidence_chain", [])
|
| 768 |
+
],
|
| 769 |
+
reasoning_modes=[ReasoningMode(m) for m in claim_data.get("reasoning_modes", [])],
|
| 770 |
+
sub_domains=[KnowledgeDomain(d) for d in claim_data.get("sub_domains", [])],
|
| 771 |
+
causal_mechanisms=claim_data.get("causal_mechanisms", []),
|
| 772 |
+
expected_validity=claim_data.get("expected_validity")
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
result = await self.validator.validate_knowledge_claim(claim)
|
| 776 |
+
return JSONResponse(content=result)
|
| 777 |
+
except Exception as e:
|
| 778 |
+
return JSONResponse(
|
| 779 |
+
status_code=400,
|
| 780 |
+
content={"error": str(e)}
|
| 781 |
+
)
|
| 782 |
+
|
| 783 |
+
async def get_history(self, format: str = "json", limit: int = 10):
|
| 784 |
+
history = self.validator.validation_history[-limit:]
|
| 785 |
+
if format == "json":
|
| 786 |
+
return history
|
| 787 |
+
else:
|
| 788 |
+
return PlainTextResponse(self.validator.export_validation_history(format))
|
| 789 |
+
|
| 790 |
+
async def get_statistics(self):
|
| 791 |
+
return self.validator.get_validation_statistics()
|
| 792 |
+
|
| 793 |
+
# --------------------------
|
| 794 |
+
# MAIN EXECUTION ◉⃤
|
| 795 |
+
# --------------------------
|
| 796 |
+
async def main():
|
| 797 |
+
# Initialize with real-time data sources
|
| 798 |
+
real_time_sources = [
|
| 799 |
+
RealTimeDataSource(
|
| 800 |
+
"ai_research_db",
|
| 801 |
+
"https://api.ai-research.org/v1/validate",
|
| 802 |
+
KnowledgeDomain.TECHNOLOGY,
|
| 803 |
+
refresh_interval=1800
|
| 804 |
+
),
|
| 805 |
+
RealTimeDataSource(
|
| 806 |
+
"climate_data",
|
| 807 |
+
"https://api.climate.gov/evidence",
|
| 808 |
+
KnowledgeDomain.SCIENCE,
|
| 809 |
+
priority=2
|
| 810 |
+
)
|
| 811 |
+
]
|
| 812 |
+
|
| 813 |
+
# Create enhanced validator
|
| 814 |
+
validator = EnhancedAGIValidator(
|
| 815 |
+
mcp_enabled=True,
|
| 816 |
+
real_time_sources=real_time_sources
|
| 817 |
+
)
|
| 818 |
+
|
| 819 |
+
# Create UI service
|
| 820 |
+
ui = AGIValidatorUI(validator)
|
| 821 |
+
uvicorn.run(ui.app, host="0.0.0.0", port=8000)
|
| 822 |
+
|
| 823 |
+
if __name__ == "__main__":
|
| 824 |
+
asyncio.run(main())
|