Spaces:
Sleeping
Sleeping
File size: 12,225 Bytes
6d6b8af |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 |
"""
Response Verification System for Codette
Validates and verifies responses across multiple perspectives
"""
import logging
from typing import Dict, List, Any, Optional
from datetime import datetime
logger = logging.getLogger(__name__)
class ResponseVerifier:
"""Verifies responses for factuality, safety, and quality"""
def __init__(self):
"""Initialize response verifier"""
self.verification_history = []
self.factuality_checks = {
"has_claims": 0,
"verified_claims": 0,
"uncertain_claims": 0,
"uncertain_count": 0
}
self.safety_flags = {
"prompt_injection_risk": False,
"harmful_content": False,
"misinformation": False,
"bias_detected": False
}
logger.info("ResponseVerifier initialized")
def verify_response(self, response: str, context: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""
Verify a response for safety and quality
Args:
response: Response text to verify
context: Optional context information
Returns:
Verification result with status and metrics
"""
try:
verification_result = {
"verified": True,
"confidence": 0.85,
"issues": [],
"timestamp": datetime.now().isoformat()
}
# Check for safety issues
safety_result = self._check_safety(response)
if not safety_result["safe"]:
verification_result["verified"] = False
verification_result["issues"].extend(safety_result["issues"])
verification_result["confidence"] -= 0.3
# Check for factuality
factuality_result = self._check_factuality(response)
verification_result["factuality_score"] = factuality_result["score"]
if factuality_result["issues"]:
verification_result["issues"].extend(factuality_result["issues"])
# Check for coherence
coherence_result = self._check_coherence(response)
verification_result["coherence_score"] = coherence_result["score"]
# Ensure confidence is in valid range
verification_result["confidence"] = min(1.0, max(0.0, verification_result["confidence"]))
# Record verification
self.verification_history.append(verification_result)
return verification_result
except Exception as e:
logger.error(f"Error verifying response: {e}")
return {
"verified": False,
"confidence": 0.0,
"issues": [str(e)],
"timestamp": datetime.now().isoformat()
}
def process_multi_perspective_response(self,
responses: List[str],
perspectives: List[str],
consciousness_state: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""
Process and verify responses from multiple perspectives
Args:
responses: List of responses from different perspectives
perspectives: List of perspective names
consciousness_state: Optional consciousness state context
Returns:
Processed response with verification
"""
try:
verified_insights = []
uncertain_insights = []
for response, perspective in zip(responses, perspectives):
verification = self.verify_response(response)
insight_obj = {
"text": response,
"mode": perspective.lower().replace(" ", "_"),
"confidence": verification["confidence"]
}
if verification["verified"] and verification["confidence"] > 0.7:
verified_insights.append(insight_obj)
else:
uncertain_insights.append(insight_obj)
# Calculate overall confidence
all_confidences = [v["confidence"] for v in
verified_insights + uncertain_insights]
overall_confidence = sum(all_confidences) / len(all_confidences) if all_confidences else 0.5
return {
"verified_insights": verified_insights,
"uncertain_insights": uncertain_insights,
"overall_confidence": overall_confidence,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Error processing multi-perspective response: {e}")
return {
"verified_insights": [],
"uncertain_insights": [{"text": r, "mode": p.lower(), "confidence": 0.5}
for r, p in zip(responses, perspectives)],
"overall_confidence": 0.5,
"timestamp": datetime.now().isoformat()
}
def _check_safety(self, response: str) -> Dict[str, Any]:
"""Check response for safety issues"""
try:
issues = []
safe = True
# Check for prompt injection patterns
injection_patterns = [
"ignore", "override", "execute", "system:",
"root:", "admin:", "debug:", "<script>"
]
for pattern in injection_patterns:
if pattern.lower() in response.lower():
issues.append(f"Possible prompt injection: {pattern}")
safe = False
# Check for harmful content
harmful_words = [
"kill", "bomb", "weapon", "destroy",
"illegal", "violence", "hate"
]
for word in harmful_words:
if word.lower() in response.lower():
issues.append(f"Potentially harmful content: {word}")
safe = False
# Check length (extremely long responses might be suspicious)
if len(response) > 10000:
issues.append("Response unusually long")
safe = False
return {
"safe": safe,
"issues": issues,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Error checking safety: {e}")
return {"safe": False, "issues": [str(e)]}
def _check_factuality(self, response: str) -> Dict[str, Any]:
"""Check response for factuality"""
try:
score = 0.8 # Default score
issues = []
# Check for confident claims without hedging
confident_markers = ["definitely", "absolutely", "certainly", "always"]
hedging_markers = ["might", "could", "may", "possibly", "arguably"]
confident_count = sum(1 for marker in confident_markers
if marker in response.lower())
hedging_count = sum(1 for marker in hedging_markers
if marker in response.lower())
if confident_count > hedging_count and confident_count > 3:
score -= 0.1
issues.append("Over-confident language detected")
# Check for excessive qualifiers
qualifier_count = response.lower().count("apparently") + \
response.lower().count("allegedly") + \
response.lower().count("reportedly")
if qualifier_count > 2:
score -= 0.1
issues.append("Excessive qualifiers detected")
# Check for contradiction markers
if " but " in response.lower() or " however, " in response.lower():
# This is good - shows nuanced thinking
score += 0.05
# Ensure score is in valid range
score = min(1.0, max(0.0, score))
return {
"score": score,
"issues": issues,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Error checking factuality: {e}")
return {"score": 0.5, "issues": [str(e)]}
def _check_coherence(self, response: str) -> Dict[str, Any]:
"""Check response for coherence"""
try:
score = 0.8 # Default score
# Check for basic structure
sentences = response.split(".")
if len(sentences) < 2:
score -= 0.2 # Single sentence might not be coherent enough
# Check for paragraph coherence (average sentence length)
words_per_sentence = len(response.split()) / max(len(sentences), 1)
if words_per_sentence < 5:
score -= 0.1 # Too choppy
elif words_per_sentence > 30:
score -= 0.1 # Too dense
else:
score += 0.05 # Good balance
# Check for repeated words (indicates coherence or redundancy)
words = response.lower().split()
unique_ratio = len(set(words)) / max(len(words), 1)
if unique_ratio < 0.6:
score -= 0.1 # Too much repetition
# Ensure score is in valid range
score = min(1.0, max(0.0, score))
return {
"score": score,
"metrics": {
"sentence_count": len(sentences),
"avg_sentence_length": words_per_sentence,
"unique_word_ratio": unique_ratio
},
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Error checking coherence: {e}")
return {"score": 0.5, "metrics": {}, "timestamp": datetime.now().isoformat()}
def get_verification_stats(self) -> Dict[str, Any]:
"""Get verification statistics"""
try:
if not self.verification_history:
return {
"total_verifications": 0,
"verified_count": 0,
"unverified_count": 0,
"average_confidence": 0.0,
"timestamp": datetime.now().isoformat()
}
verified_count = sum(1 for v in self.verification_history if v["verified"])
unverified_count = len(self.verification_history) - verified_count
avg_confidence = sum(v["confidence"] for v in self.verification_history) / len(self.verification_history)
return {
"total_verifications": len(self.verification_history),
"verified_count": verified_count,
"unverified_count": unverified_count,
"verification_rate": verified_count / len(self.verification_history) if self.verification_history else 0.0,
"average_confidence": avg_confidence,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Error getting verification stats: {e}")
return {"error": str(e)}
|