Spaces:
Sleeping
Sleeping
Update src/utils/response_verifier.py
Browse files- src/utils/response_verifier.py +168 -2
src/utils/response_verifier.py
CHANGED
|
@@ -1,6 +1,173 @@
|
|
|
|
|
|
|
|
| 1 |
from typing import Dict, Any, List, Optional
|
| 2 |
from ..knowledge_base.grounding_truth import GroundingTruth
|
| 3 |
-
from .response_templates import get_response_templates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import logging
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
|
@@ -127,7 +294,6 @@ class ResponseVerifier:
|
|
| 127 |
elif confidence >= 0.6:
|
| 128 |
return "It appears that"
|
| 129 |
elif confidence >= 0.4:
|
| 130 |
-
return "It seems possible that"
|
| 131 |
uncertain = self.response_templates.get_uncertain_prefix()
|
| 132 |
return uncertain.rstrip(',') # Remove trailing comma if present
|
| 133 |
elif confidence >= 0.2:
|
|
|
|
| 1 |
+
File: src\utils\response_verifier.py
|
| 2 |
+
````````python
|
| 3 |
from typing import Dict, Any, List, Optional
|
| 4 |
from ..knowledge_base.grounding_truth import GroundingTruth
|
| 5 |
+
from ..components.response_templates import get_response_templates
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
class ResponseVerifier:
|
| 11 |
+
"""Verifies responses using grounding truth while preserving multi-perspective analysis"""
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.grounding_truth = GroundingTruth()
|
| 15 |
+
self.response_templates = get_response_templates()
|
| 16 |
+
self.mode_confidence_thresholds = {
|
| 17 |
+
"scientific": 0.9,
|
| 18 |
+
"creative": 0.7,
|
| 19 |
+
"emotional": 0.6,
|
| 20 |
+
"quantum": 0.8,
|
| 21 |
+
"philosophical": 0.7
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def verify_insight(
|
| 25 |
+
self,
|
| 26 |
+
insight: str,
|
| 27 |
+
mode: str,
|
| 28 |
+
context: Optional[Dict[str, Any]] = None
|
| 29 |
+
) -> Dict[str, Any]:
|
| 30 |
+
"""
|
| 31 |
+
Verify a single insight from a specific perspective mode
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
insight: The insight to verify
|
| 35 |
+
mode: The perspective mode that generated it
|
| 36 |
+
context: Optional additional context
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
Dict containing verification results and confidence
|
| 40 |
+
"""
|
| 41 |
+
verification = self.grounding_truth.verify_statement(insight, context)
|
| 42 |
+
confidence_threshold = self.mode_confidence_thresholds.get(mode, 0.7)
|
| 43 |
+
|
| 44 |
+
# Adjust verification based on mode characteristics
|
| 45 |
+
if mode == "creative":
|
| 46 |
+
# Allow more speculative statements in creative mode
|
| 47 |
+
verification["confidence"] *= 1.2
|
| 48 |
+
elif mode == "quantum":
|
| 49 |
+
# Account for quantum uncertainty
|
| 50 |
+
verification["confidence"] = max(
|
| 51 |
+
verification["confidence"],
|
| 52 |
+
0.5 # Quantum superposition baseline
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"verified": verification["verified"] or verification["confidence"] >= confidence_threshold,
|
| 57 |
+
"confidence": verification["confidence"],
|
| 58 |
+
"mode": mode,
|
| 59 |
+
"original_insight": insight,
|
| 60 |
+
"requires_qualifier": verification["confidence"] < confidence_threshold
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
def process_multi_perspective_response(
|
| 64 |
+
self,
|
| 65 |
+
insights: List[str],
|
| 66 |
+
modes: List[str],
|
| 67 |
+
consciousness_state: Optional[Dict[str, Any]] = None
|
| 68 |
+
) -> Dict[str, Any]:
|
| 69 |
+
"""
|
| 70 |
+
Process and verify a multi-perspective response
|
| 71 |
+
|
| 72 |
+
Args:
|
| 73 |
+
insights: List of insights from different perspectives
|
| 74 |
+
modes: List of perspective modes that generated the insights
|
| 75 |
+
consciousness_state: Optional consciousness state info
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
Processed response with verification metadata
|
| 79 |
+
"""
|
| 80 |
+
verified_insights = []
|
| 81 |
+
uncertain_insights = []
|
| 82 |
+
|
| 83 |
+
for insight, mode in zip(insights, modes):
|
| 84 |
+
result = self.verify_insight(
|
| 85 |
+
insight,
|
| 86 |
+
mode,
|
| 87 |
+
{"consciousness": consciousness_state} if consciousness_state else None
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if result["verified"]:
|
| 91 |
+
verified_insights.append({
|
| 92 |
+
"text": result["original_insight"],
|
| 93 |
+
"mode": mode,
|
| 94 |
+
"confidence": result["confidence"]
|
| 95 |
+
})
|
| 96 |
+
else:
|
| 97 |
+
qualified_insight = self._add_qualifier(
|
| 98 |
+
result["original_insight"],
|
| 99 |
+
result["confidence"],
|
| 100 |
+
mode
|
| 101 |
+
)
|
| 102 |
+
uncertain_insights.append({
|
| 103 |
+
"text": qualified_insight,
|
| 104 |
+
"mode": mode,
|
| 105 |
+
"confidence": result["confidence"]
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
return {
|
| 109 |
+
"verified_insights": verified_insights,
|
| 110 |
+
"uncertain_insights": uncertain_insights,
|
| 111 |
+
"overall_confidence": self._calculate_overall_confidence(
|
| 112 |
+
verified_insights, uncertain_insights
|
| 113 |
+
)
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
def _add_qualifier(self, insight: str, confidence: float, mode: str) -> str:
|
| 117 |
+
"""Add appropriate qualifier based on confidence and mode"""
|
| 118 |
+
base_qualifier = self._get_base_qualifier(confidence)
|
| 119 |
+
mode_specific = self._get_mode_qualifier(mode)
|
| 120 |
+
|
| 121 |
+
if mode_specific:
|
| 122 |
+
return f"{base_qualifier} {insight} ({mode_specific})"
|
| 123 |
+
return f"{base_qualifier} {insight}"
|
| 124 |
+
|
| 125 |
+
def _get_base_qualifier(self, confidence: float) -> str:
|
| 126 |
+
"""Get base confidence qualifier"""
|
| 127 |
+
if confidence >= 0.8:
|
| 128 |
+
return "Evidence suggests that"
|
| 129 |
+
elif confidence >= 0.6:
|
| 130 |
+
return "It appears that"
|
| 131 |
+
elif confidence >= 0.4:
|
| 132 |
+
uncertain = self.response_templates.get_uncertain_prefix()
|
| 133 |
+
return uncertain.rstrip(',') # Remove trailing comma if present
|
| 134 |
+
elif confidence >= 0.2:
|
| 135 |
+
return "There is a speculation that"
|
| 136 |
+
else:
|
| 137 |
+
return "There is an unverified hypothesis that"
|
| 138 |
+
|
| 139 |
+
def _get_mode_qualifier(self, mode: str) -> Optional[str]:
|
| 140 |
+
"""Get mode-specific qualifier"""
|
| 141 |
+
qualifiers = {
|
| 142 |
+
"scientific": "based on available data",
|
| 143 |
+
"creative": "from a creative perspective",
|
| 144 |
+
"emotional": "from an emotional standpoint",
|
| 145 |
+
"quantum": "considering quantum possibilities",
|
| 146 |
+
"philosophical": "from a philosophical view"
|
| 147 |
+
}
|
| 148 |
+
return qualifiers.get(mode)
|
| 149 |
+
|
| 150 |
+
def _calculate_overall_confidence(
|
| 151 |
+
self,
|
| 152 |
+
verified: List[Dict[str, Any]],
|
| 153 |
+
uncertain: List[Dict[str, Any]]
|
| 154 |
+
) -> float:
|
| 155 |
+
"""Calculate overall response confidence"""
|
| 156 |
+
if not verified and not uncertain:
|
| 157 |
+
return 0.0
|
| 158 |
+
|
| 159 |
+
total_insights = len(verified) + len(uncertain)
|
| 160 |
+
confidence_sum = sum(v["confidence"] for v in verified)
|
| 161 |
+
confidence_sum += sum(u["confidence"] for u in uncertain)
|
| 162 |
+
|
| 163 |
+
return confidence_sum / total_insights
|
| 164 |
+
````````
|
| 165 |
+
|
| 166 |
+
# Response
|
| 167 |
+
````````python
|
| 168 |
+
from typing import Dict, Any, List, Optional
|
| 169 |
+
from ..knowledge_base.grounding_truth import GroundingTruth
|
| 170 |
+
from ..components.response_templates import get_response_templates
|
| 171 |
import logging
|
| 172 |
|
| 173 |
logger = logging.getLogger(__name__)
|
|
|
|
| 294 |
elif confidence >= 0.6:
|
| 295 |
return "It appears that"
|
| 296 |
elif confidence >= 0.4:
|
|
|
|
| 297 |
uncertain = self.response_templates.get_uncertain_prefix()
|
| 298 |
return uncertain.rstrip(',') # Remove trailing comma if present
|
| 299 |
elif confidence >= 0.2:
|