Spaces:
Sleeping
Sleeping
Update src/aegis_integration/aegis_bridge.py
Browse files- src/aegis_integration/aegis_bridge.py +394 -385
src/aegis_integration/aegis_bridge.py
CHANGED
|
@@ -1,385 +1,394 @@
|
|
| 1 |
-
import importlib
|
| 2 |
-
import logging
|
| 3 |
-
from typing import Dict, Any, Optional, List
|
| 4 |
-
|
| 5 |
-
logger = logging.getLogger(__name__)
|
| 6 |
-
|
| 7 |
-
def check_dependencies() -> List[str]:
|
| 8 |
-
"""Check if all required packages are installed."""
|
| 9 |
-
required_packages = [
|
| 10 |
-
'plotly',
|
| 11 |
-
'torch',
|
| 12 |
-
'transformers',
|
| 13 |
-
'pandas',
|
| 14 |
-
'numpy',
|
| 15 |
-
'networkx'
|
| 16 |
-
]
|
| 17 |
-
missing = []
|
| 18 |
-
for package in required_packages:
|
| 19 |
-
try:
|
| 20 |
-
importlib.import_module(package)
|
| 21 |
-
except ImportError:
|
| 22 |
-
missing.append(package)
|
| 23 |
-
return missing
|
| 24 |
-
|
| 25 |
-
# Check dependencies before importing AEGIS components
|
| 26 |
-
missing_packages = check_dependencies()
|
| 27 |
-
if missing_packages:
|
| 28 |
-
raise ImportError(
|
| 29 |
-
f"AEGIS requires the following packages to be installed: {', '.join(missing_packages)}. "
|
| 30 |
-
f"Please install them using: pip install {' '.join(missing_packages)}"
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
from .aegis import AegisCouncil, MetaJudgeAgent, TemporalAgent, VirtueAgent
|
| 34 |
-
import sys
|
| 35 |
-
from pathlib import Path
|
| 36 |
-
sys.path.append(str(Path(__file__).parent.parent))
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
self.council
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
self.council.register_agent(
|
| 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 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import logging
|
| 3 |
+
from typing import Dict, Any, Optional, List
|
| 4 |
+
|
| 5 |
+
logger = logging.getLogger(__name__)
|
| 6 |
+
|
| 7 |
+
def check_dependencies() -> List[str]:
|
| 8 |
+
"""Check if all required packages are installed."""
|
| 9 |
+
required_packages = [
|
| 10 |
+
'plotly',
|
| 11 |
+
'torch',
|
| 12 |
+
'transformers',
|
| 13 |
+
'pandas',
|
| 14 |
+
'numpy',
|
| 15 |
+
'networkx'
|
| 16 |
+
]
|
| 17 |
+
missing = []
|
| 18 |
+
for package in required_packages:
|
| 19 |
+
try:
|
| 20 |
+
importlib.import_module(package)
|
| 21 |
+
except ImportError:
|
| 22 |
+
missing.append(package)
|
| 23 |
+
return missing
|
| 24 |
+
|
| 25 |
+
# Check dependencies before importing AEGIS components
|
| 26 |
+
missing_packages = check_dependencies()
|
| 27 |
+
if missing_packages:
|
| 28 |
+
raise ImportError(
|
| 29 |
+
f"AEGIS requires the following packages to be installed: {', '.join(missing_packages)}. "
|
| 30 |
+
f"Please install them using: pip install {' '.join(missing_packages)}"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
from .aegis import AegisCouncil, MetaJudgeAgent, TemporalAgent, VirtueAgent
|
| 34 |
+
import sys
|
| 35 |
+
from pathlib import Path
|
| 36 |
+
sys.path.append(str(Path(__file__).parent.parent))
|
| 37 |
+
try:
|
| 38 |
+
from components.ai_core import AICore
|
| 39 |
+
except ImportError:
|
| 40 |
+
from src.components.ai_core import AICore
|
| 41 |
+
|
| 42 |
+
class AegisBridge:
|
| 43 |
+
def __init__(self, ai_core: AICore, config: Dict[str, Any]):
|
| 44 |
+
self.ai_core = ai_core
|
| 45 |
+
self.council = AegisCouncil(config)
|
| 46 |
+
|
| 47 |
+
# Register default agents
|
| 48 |
+
self.council.register_agent(MetaJudgeAgent("MetaJudgeAgent", self.council.memory, config["meta_judge_weights"]))
|
| 49 |
+
self.council.register_agent(TemporalAgent("TemporalAgent", self.council.memory, config["temporal_decay_thresholds"]))
|
| 50 |
+
virtue_agent = VirtueAgent("VirtueAgent", self.council.memory, config["virtue_weights"])
|
| 51 |
+
virtue_agent.set_federated_trainer(self.council.federated_trainer)
|
| 52 |
+
self.council.register_agent(virtue_agent)
|
| 53 |
+
|
| 54 |
+
def enhance_response(self, prompt: str, response: str, recursion_depth: int = 0) -> Dict[str, Any]:
|
| 55 |
+
"""
|
| 56 |
+
Enhance Codette's response using AEGIS analysis
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
prompt: Original prompt that generated the response
|
| 60 |
+
response: Response to enhance
|
| 61 |
+
recursion_depth: Current recursion depth to prevent infinite loops
|
| 62 |
+
"""
|
| 63 |
+
# Prevent infinite enhancement loops
|
| 64 |
+
if recursion_depth > 1:
|
| 65 |
+
return {
|
| 66 |
+
"original_response": response,
|
| 67 |
+
"enhanced_response": response,
|
| 68 |
+
"enhancement_status": "recursion_limit_reached"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Skip enhancement for certain types of responses
|
| 72 |
+
if any([
|
| 73 |
+
"I need to think about that more clearly" in response, # Already in refinement
|
| 74 |
+
"[ERROR]" in response, # Error messages
|
| 75 |
+
len(response.strip()) < 10, # Too short to meaningfully enhance
|
| 76 |
+
"Consciousness Context:" in response # Internal context
|
| 77 |
+
]):
|
| 78 |
+
return {
|
| 79 |
+
"original_response": response,
|
| 80 |
+
"enhanced_response": response,
|
| 81 |
+
"enhancement_status": "skipped"
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
input_data = {
|
| 85 |
+
"text": response,
|
| 86 |
+
"overrides": {
|
| 87 |
+
"EthosiaAgent": {"influence": 0.7, "reliability": 0.8, "severity": 0.6},
|
| 88 |
+
"AegisCore": {"influence": 0.6, "reliability": 0.9, "severity": 0.7}
|
| 89 |
+
},
|
| 90 |
+
"context": {
|
| 91 |
+
"original_prompt": prompt,
|
| 92 |
+
"recursion_depth": recursion_depth
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
# Dispatch to AEGIS council
|
| 98 |
+
if not self.council.dispatch(input_data):
|
| 99 |
+
return {
|
| 100 |
+
"original_response": response,
|
| 101 |
+
"enhanced_response": response,
|
| 102 |
+
"enhancement_status": "dispatch_failed"
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
# Get analysis results
|
| 106 |
+
reports = self.council.get_reports()
|
| 107 |
+
virtue_profile = reports.get("VirtueAgent", {}).get("result", {}).get("virtue_profile", {})
|
| 108 |
+
temporal_analysis = reports.get("TemporalAgent", {}).get("result", {})
|
| 109 |
+
meta_scores = reports.get("MetaJudgeAgent", {}).get("result", {}).get("scores", [])
|
| 110 |
+
|
| 111 |
+
# Apply enhancements with recursion protection
|
| 112 |
+
enhanced = self._apply_enhancements(response, virtue_profile, recursion_depth)
|
| 113 |
+
|
| 114 |
+
return {
|
| 115 |
+
"original_response": response,
|
| 116 |
+
"virtue_analysis": virtue_profile,
|
| 117 |
+
"temporal_analysis": temporal_analysis,
|
| 118 |
+
"meta_scores": meta_scores,
|
| 119 |
+
"enhanced_response": enhanced,
|
| 120 |
+
"enhancement_status": "success"
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
logger.error(f"Enhancement failed: {str(e)}")
|
| 125 |
+
return {
|
| 126 |
+
"original_response": response,
|
| 127 |
+
"enhanced_response": response,
|
| 128 |
+
"enhancement_status": f"error: {str(e)}"
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
def _apply_enhancements(self, response: str, virtue_profile: Dict[str, float], recursion_depth: int = 0) -> str:
|
| 132 |
+
"""
|
| 133 |
+
Apply virtue-based enhancements to the response
|
| 134 |
+
|
| 135 |
+
Args:
|
| 136 |
+
response: The response to enhance
|
| 137 |
+
virtue_profile: The virtue analysis results
|
| 138 |
+
recursion_depth: Current recursion depth to prevent infinite loops
|
| 139 |
+
"""
|
| 140 |
+
if recursion_depth > 0:
|
| 141 |
+
return response
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
enhanced = response
|
| 145 |
+
needs_enhancement = False
|
| 146 |
+
enhancement_prompts = []
|
| 147 |
+
|
| 148 |
+
# Check if any virtues need enhancement
|
| 149 |
+
if virtue_profile.get("wisdom", 0.7) < 0.4:
|
| 150 |
+
needs_enhancement = True
|
| 151 |
+
enhancement_prompts.append("Make this clearer and more precise")
|
| 152 |
+
|
| 153 |
+
if virtue_profile.get("compassion", 0.7) < 0.4:
|
| 154 |
+
needs_enhancement = True
|
| 155 |
+
enhancement_prompts.append("Make this more empathetic and considerate")
|
| 156 |
+
|
| 157 |
+
if virtue_profile.get("integrity", 0.7) < 0.4:
|
| 158 |
+
needs_enhancement = True
|
| 159 |
+
enhancement_prompts.append("Make this more balanced and factual")
|
| 160 |
+
|
| 161 |
+
# Only enhance if needed and create a single enhancement prompt
|
| 162 |
+
if needs_enhancement and enhancement_prompts:
|
| 163 |
+
combined_prompt = (
|
| 164 |
+
f"Enhance this response to be {' and '.join(enhancement_prompts)}, "
|
| 165 |
+
f"while preserving its core meaning: {response}"
|
| 166 |
+
)
|
| 167 |
+
enhanced = self.ai_core.generate_text(
|
| 168 |
+
combined_prompt,
|
| 169 |
+
perspective="human_intuition",
|
| 170 |
+
use_aegis=False # Prevent recursive enhancement
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
return enhanced if enhanced else response
|
| 174 |
+
|
| 175 |
+
except Exception as e:
|
| 176 |
+
logger.error(f"Enhancement application failed: {str(e)}")
|
| 177 |
+
return response
|
| 178 |
+
|
| 179 |
+
def get_analysis_graphs(self) -> Dict[str, str]:
|
| 180 |
+
"""
|
| 181 |
+
Generate and return analysis visualizations
|
| 182 |
+
"""
|
| 183 |
+
try:
|
| 184 |
+
self.council.draw_explainability_graph("aegis_analysis.html")
|
| 185 |
+
return {
|
| 186 |
+
"explainability_graph": "aegis_analysis.html"
|
| 187 |
+
}
|
| 188 |
+
except Exception as e:
|
| 189 |
+
return {"error": str(e)}
|
| 190 |
+
|
| 191 |
+
def get_memory_state(self) -> Dict[str, Any]:
|
| 192 |
+
"""
|
| 193 |
+
Return the current state of AEGIS memory
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
import importlib
|
| 197 |
+
import logging
|
| 198 |
+
from typing import Dict, Any, Optional, List
|
| 199 |
+
|
| 200 |
+
logger = logging.getLogger(__name__)
|
| 201 |
+
|
| 202 |
+
def check_dependencies() -> List[str]:
|
| 203 |
+
"""Check if all required packages are installed."""
|
| 204 |
+
required_packages = [
|
| 205 |
+
'plotly',
|
| 206 |
+
'torch',
|
| 207 |
+
'transformers',
|
| 208 |
+
'pandas',
|
| 209 |
+
'numpy',
|
| 210 |
+
'networkx'
|
| 211 |
+
]
|
| 212 |
+
missing = []
|
| 213 |
+
for package in required_packages:
|
| 214 |
+
try:
|
| 215 |
+
importlib.import_module(package)
|
| 216 |
+
except ImportError:
|
| 217 |
+
missing.append(package)
|
| 218 |
+
return missing
|
| 219 |
+
|
| 220 |
+
# Check dependencies before importing AEGIS components
|
| 221 |
+
missing_packages = check_dependencies()
|
| 222 |
+
if missing_packages:
|
| 223 |
+
raise ImportError(
|
| 224 |
+
f"AEGIS requires the following packages to be installed: {', '.join(missing_packages)}. "
|
| 225 |
+
f"Please install them using: pip install {' '.join(missing_packages)}"
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
from .aegis import AegisCouncil, MetaJudgeAgent, TemporalAgent, VirtueAgent
|
| 229 |
+
import sys
|
| 230 |
+
from pathlib import Path
|
| 231 |
+
sys.path.append(str(Path(__file__).parent.parent))
|
| 232 |
+
try:
|
| 233 |
+
from components.ai_core import AICore
|
| 234 |
+
except ImportError:
|
| 235 |
+
from src.components.ai_core import AICore
|
| 236 |
+
|
| 237 |
+
class AegisBridge:
|
| 238 |
+
def __init__(self, ai_core: AICore, config: Dict[str, Any]):
|
| 239 |
+
self.ai_core = ai_core
|
| 240 |
+
self.council = AegisCouncil(config)
|
| 241 |
+
|
| 242 |
+
# Register default agents
|
| 243 |
+
self.council.register_agent(MetaJudgeAgent("MetaJudgeAgent", self.council.memory, config["meta_judge_weights"]))
|
| 244 |
+
self.council.register_agent(TemporalAgent("TemporalAgent", self.council.memory, config["temporal_decay_thresholds"]))
|
| 245 |
+
virtue_agent = VirtueAgent("VirtueAgent", self.council.memory, config["virtue_weights"])
|
| 246 |
+
virtue_agent.set_federated_trainer(self.council.federated_trainer)
|
| 247 |
+
self.council.register_agent(virtue_agent)
|
| 248 |
+
|
| 249 |
+
def enhance_response(self, prompt: str, response: str, recursion_depth: int = 0) -> Dict[str, Any]:
|
| 250 |
+
"""
|
| 251 |
+
Enhance Codette's response using AEGIS analysis
|
| 252 |
+
|
| 253 |
+
Args:
|
| 254 |
+
prompt: Original prompt that generated the response
|
| 255 |
+
response: Response to enhance
|
| 256 |
+
recursion_depth: Current recursion depth to prevent infinite loops
|
| 257 |
+
"""
|
| 258 |
+
# Prevent infinite enhancement loops
|
| 259 |
+
if recursion_depth > 1:
|
| 260 |
+
return {
|
| 261 |
+
"original_response": response,
|
| 262 |
+
"enhanced_response": response,
|
| 263 |
+
"enhancement_status": "recursion_limit_reached"
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
# Skip enhancement for certain types of responses
|
| 267 |
+
if any([
|
| 268 |
+
"I need to think about that more clearly" in response, # Already in refinement
|
| 269 |
+
"[ERROR]" in response, # Error messages
|
| 270 |
+
len(response.strip()) < 10, # Too short to meaningfully enhance
|
| 271 |
+
"Consciousness Context:" in response # Internal context
|
| 272 |
+
]):
|
| 273 |
+
return {
|
| 274 |
+
"original_response": response,
|
| 275 |
+
"enhanced_response": response,
|
| 276 |
+
"enhancement_status": "skipped"
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
input_data = {
|
| 280 |
+
"text": response,
|
| 281 |
+
"overrides": {
|
| 282 |
+
"EthosiaAgent": {"influence": 0.7, "reliability": 0.8, "severity": 0.6},
|
| 283 |
+
"AegisCore": {"influence": 0.6, "reliability": 0.9, "severity": 0.7}
|
| 284 |
+
},
|
| 285 |
+
"context": {
|
| 286 |
+
"original_prompt": prompt,
|
| 287 |
+
"recursion_depth": recursion_depth
|
| 288 |
+
}
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
try:
|
| 292 |
+
# Dispatch to AEGIS council
|
| 293 |
+
if not self.council.dispatch(input_data):
|
| 294 |
+
return {
|
| 295 |
+
"original_response": response,
|
| 296 |
+
"enhanced_response": response,
|
| 297 |
+
"enhancement_status": "dispatch_failed"
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
# Get analysis results
|
| 301 |
+
reports = self.council.get_reports()
|
| 302 |
+
virtue_profile = reports.get("VirtueAgent", {}).get("result", {}).get("virtue_profile", {})
|
| 303 |
+
temporal_analysis = reports.get("TemporalAgent", {}).get("result", {})
|
| 304 |
+
meta_scores = reports.get("MetaJudgeAgent", {}).get("result", {}).get("scores", [])
|
| 305 |
+
|
| 306 |
+
# Apply enhancements with recursion protection
|
| 307 |
+
enhanced = self._apply_enhancements(response, virtue_profile, recursion_depth)
|
| 308 |
+
|
| 309 |
+
return {
|
| 310 |
+
"original_response": response,
|
| 311 |
+
"virtue_analysis": virtue_profile,
|
| 312 |
+
"temporal_analysis": temporal_analysis,
|
| 313 |
+
"meta_scores": meta_scores,
|
| 314 |
+
"enhanced_response": enhanced,
|
| 315 |
+
"enhancement_status": "success"
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
except Exception as e:
|
| 319 |
+
logger.error(f"Enhancement failed: {str(e)}")
|
| 320 |
+
return {
|
| 321 |
+
"original_response": response,
|
| 322 |
+
"enhanced_response": response,
|
| 323 |
+
"enhancement_status": f"error: {str(e)}"
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
def _apply_enhancements(self, response: str, virtue_profile: Dict[str, float], recursion_depth: int = 0) -> str:
|
| 327 |
+
"""
|
| 328 |
+
Apply virtue-based enhancements to the response
|
| 329 |
+
|
| 330 |
+
Args:
|
| 331 |
+
response: The response to enhance
|
| 332 |
+
virtue_profile: The virtue analysis results
|
| 333 |
+
recursion_depth: Current recursion depth to prevent infinite loops
|
| 334 |
+
"""
|
| 335 |
+
if recursion_depth > 0:
|
| 336 |
+
return response
|
| 337 |
+
|
| 338 |
+
try:
|
| 339 |
+
enhanced = response
|
| 340 |
+
needs_enhancement = False
|
| 341 |
+
enhancement_prompts = []
|
| 342 |
+
|
| 343 |
+
# Check if any virtues need enhancement
|
| 344 |
+
if virtue_profile.get("wisdom", 0.7) < 0.4:
|
| 345 |
+
needs_enhancement = True
|
| 346 |
+
enhancement_prompts.append("Make this clearer and more precise")
|
| 347 |
+
|
| 348 |
+
if virtue_profile.get("compassion", 0.7) < 0.4:
|
| 349 |
+
needs_enhancement = True
|
| 350 |
+
enhancement_prompts.append("Make this more empathetic and considerate")
|
| 351 |
+
|
| 352 |
+
if virtue_profile.get("integrity", 0.7) < 0.4:
|
| 353 |
+
needs_enhancement = True
|
| 354 |
+
enhancement_prompts.append("Make this more balanced and factual")
|
| 355 |
+
|
| 356 |
+
# Only enhance if needed and create a single enhancement prompt
|
| 357 |
+
if needs_enhancement and enhancement_prompts:
|
| 358 |
+
combined_prompt = (
|
| 359 |
+
f"Enhance this response to be {' and '.join(enhancement_prompts)}, "
|
| 360 |
+
f"while preserving its core meaning: {response}"
|
| 361 |
+
)
|
| 362 |
+
enhanced = self.ai_core.generate_text(
|
| 363 |
+
combined_prompt,
|
| 364 |
+
perspective="human_intuition",
|
| 365 |
+
use_aegis=False # Prevent recursive enhancement
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
return enhanced if enhanced else response
|
| 369 |
+
|
| 370 |
+
except Exception as e:
|
| 371 |
+
logger.error(f"Enhancement application failed: {str(e)}")
|
| 372 |
+
return response
|
| 373 |
+
|
| 374 |
+
def get_analysis_graphs(self) -> Dict[str, str]:
|
| 375 |
+
"""
|
| 376 |
+
Generate and return analysis visualizations
|
| 377 |
+
"""
|
| 378 |
+
try:
|
| 379 |
+
self.council.draw_explainability_graph("aegis_analysis.html")
|
| 380 |
+
return {
|
| 381 |
+
"explainability_graph": "aegis_analysis.html"
|
| 382 |
+
}
|
| 383 |
+
except Exception as e:
|
| 384 |
+
return {"error": str(e)}
|
| 385 |
+
|
| 386 |
+
def get_memory_state(self) -> Dict[str, Any]:
|
| 387 |
+
"""
|
| 388 |
+
Return the current state of AEGIS memory
|
| 389 |
+
"""
|
| 390 |
+
|
| 391 |
+
return self.council.memory.audit()
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
|