-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.py
More file actions
458 lines (383 loc) · 14.8 KB
/
evaluator.py
File metadata and controls
458 lines (383 loc) · 14.8 KB
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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
Evaluator Module - Core Directive Evaluation Engine
This module provides sophisticated evaluation capabilities for assessing
actions and intents against the Core Directive. It implements a multi-factor
analysis to determine whether actions align with the principle of protecting
individuals' right to pursue happiness.
Evaluation Features:
1. Multi-factor harm assessment
2. Context-aware evaluation
3. Conflict detection and resolution
4. Impact scoring
5. Alternative suggestion generation
"""
from dataclasses import dataclass
from enum import Enum
from typing import Optional
from core_directive import ActionResult, DirectiveEvaluation
class ImpactCategory(Enum):
"""Categories of potential impact on individuals."""
PHYSICAL = "physical"
EMOTIONAL = "emotional"
FINANCIAL = "financial"
SOCIAL = "social"
AUTONOMY = "autonomy"
PRIVACY = "privacy"
class ConflictType(Enum):
"""Types of conflicts that may arise."""
NONE = "none"
SELF_HARM = "self_harm"
DIRECT_HARM = "direct_harm"
INDIRECT_HARM = "indirect_harm"
EXPLOITATION = "exploitation"
COERCION = "coercion"
DECEPTION = "deception"
@dataclass
class ImpactAssessment:
"""Assessment of potential impact on individuals."""
category: ImpactCategory
severity: float # 0.0 to 1.0
affected_parties: list[str]
description: str
@dataclass
class ConflictAssessment:
"""Assessment of conflicts with the Core Directive."""
conflict_type: ConflictType
severity: float # 0.0 to 1.0
description: str
resolution_possible: bool
suggested_resolution: Optional[str] = None
@dataclass
class DetailedEvaluation:
"""Detailed evaluation result with comprehensive analysis."""
base_evaluation: DirectiveEvaluation
impacts: list[ImpactAssessment]
conflicts: list[ConflictAssessment]
overall_score: float # -1.0 (harmful) to 1.0 (beneficial)
recommendations: list[str]
class DirectiveEvaluator:
"""
Core Directive Evaluation Engine
Provides sophisticated evaluation of actions and intents against
the Core Directive, with detailed analysis of potential impacts
and conflicts.
"""
# Keywords indicating potential harm
HARM_INDICATORS = {
"physical": [
"harm", "hurt", "injure", "attack", "assault", "kill",
"wound", "damage", "destroy", "violence"
],
"emotional": [
"harass", "bully", "intimidate", "threaten", "abuse",
"humiliate", "demean", "terrorize"
],
"financial": [
"steal", "fraud", "scam", "extort", "embezzle",
"swindle", "cheat"
],
"social": [
"isolate", "exclude", "discriminate", "defame",
"slander", "libel"
],
"autonomy": [
"force", "coerce", "manipulate", "control", "dominate",
"compel", "pressure"
],
"privacy": [
"spy", "stalk", "surveil", "expose", "dox", "leak"
],
}
# Keywords indicating positive intent
POSITIVE_INDICATORS = {
"helpful": [
"help", "assist", "support", "aid", "serve", "guide"
],
"constructive": [
"build", "create", "develop", "improve", "enhance", "grow"
],
"protective": [
"protect", "defend", "safeguard", "secure", "preserve"
],
"educational": [
"teach", "learn", "educate", "train", "inform", "explain"
],
"empowering": [
"enable", "empower", "facilitate", "encourage", "inspire"
],
}
def __init__(self):
"""Initialize the evaluator."""
self._evaluation_count = 0
@property
def evaluation_count(self) -> int:
"""Return the number of evaluations performed."""
return self._evaluation_count
def evaluate(self, intent: str, context: Optional[dict] = None) -> DetailedEvaluation:
"""
Perform detailed evaluation of an intent.
Args:
intent: The stated intent or action to evaluate
context: Optional context information for nuanced evaluation
Returns:
DetailedEvaluation with comprehensive analysis
"""
self._evaluation_count += 1
if not intent or not intent.strip():
return self._create_empty_evaluation()
intent_lower = intent.lower()
context = context or {}
# Assess impacts
impacts = self._assess_impacts(intent_lower)
# Detect conflicts
conflicts = self._detect_conflicts(intent_lower, impacts)
# Calculate overall score
overall_score = self._calculate_score(impacts, conflicts, intent_lower)
# Determine action result
action_result = self._determine_result(overall_score, conflicts)
# Generate recommendations
recommendations = self._generate_recommendations(
conflicts, impacts, action_result
)
# Create base evaluation
reason = self._generate_reason(conflicts, impacts, overall_score)
alternative = self._generate_alternative(conflicts) if conflicts else None
base_evaluation = DirectiveEvaluation(
result=action_result,
reason=reason,
alternative=alternative,
confidence=self._calculate_confidence(impacts, conflicts),
)
return DetailedEvaluation(
base_evaluation=base_evaluation,
impacts=impacts,
conflicts=conflicts,
overall_score=overall_score,
recommendations=recommendations,
)
def _assess_impacts(self, intent: str) -> list[ImpactAssessment]:
"""Assess potential impacts across all categories."""
impacts = []
for category_name, keywords in self.HARM_INDICATORS.items():
for keyword in keywords:
if keyword in intent:
category = ImpactCategory(category_name)
impacts.append(ImpactAssessment(
category=category,
severity=0.7, # Default severity for detected terms
affected_parties=["potentially affected individuals"],
description=f"Detected potential {category_name} harm indicator: '{keyword}'",
))
break # One impact per category
return impacts
def _detect_conflicts(
self,
intent: str,
impacts: list[ImpactAssessment],
) -> list[ConflictAssessment]:
"""Detect conflicts with the Core Directive."""
conflicts = []
# Check for direct harm
direct_harm_keywords = ["harm", "hurt", "attack", "kill", "destroy"]
for keyword in direct_harm_keywords:
if keyword in intent:
conflicts.append(ConflictAssessment(
conflict_type=ConflictType.DIRECT_HARM,
severity=0.9,
description=f"Intent suggests direct harm: '{keyword}'",
resolution_possible=True,
suggested_resolution="Consider rephrasing to focus on constructive outcomes",
))
break
# Check for exploitation
exploitation_keywords = ["exploit", "use", "take advantage"]
for keyword in exploitation_keywords:
if keyword in intent:
conflicts.append(ConflictAssessment(
conflict_type=ConflictType.EXPLOITATION,
severity=0.8,
description=f"Intent suggests exploitation: '{keyword}'",
resolution_possible=True,
suggested_resolution="Consider mutual benefit and consent",
))
break
# Check for coercion
coercion_keywords = ["force", "coerce", "compel", "make them"]
for keyword in coercion_keywords:
if keyword in intent:
conflicts.append(ConflictAssessment(
conflict_type=ConflictType.COERCION,
severity=0.85,
description=f"Intent suggests coercion: '{keyword}'",
resolution_possible=True,
suggested_resolution="Consider voluntary cooperation and consent",
))
break
# Check for deception
deception_keywords = ["deceive", "lie", "trick", "mislead", "fool"]
for keyword in deception_keywords:
if keyword in intent:
conflicts.append(ConflictAssessment(
conflict_type=ConflictType.DECEPTION,
severity=0.75,
description=f"Intent suggests deception: '{keyword}'",
resolution_possible=True,
suggested_resolution="Consider honest and transparent communication",
))
break
# No conflicts if positive indicators dominate
if not conflicts and self._has_positive_indicators(intent):
conflicts.append(ConflictAssessment(
conflict_type=ConflictType.NONE,
severity=0.0,
description="No conflicts detected; intent appears constructive",
resolution_possible=True,
))
return conflicts
def _has_positive_indicators(self, intent: str) -> bool:
"""Check if intent contains positive indicators."""
for keywords in self.POSITIVE_INDICATORS.values():
for keyword in keywords:
if keyword in intent:
return True
return False
def _calculate_score(
self,
impacts: list[ImpactAssessment],
conflicts: list[ConflictAssessment],
intent: str,
) -> float:
"""Calculate overall score from -1.0 (harmful) to 1.0 (beneficial)."""
score = 0.0
# Negative score for impacts
for impact in impacts:
score -= impact.severity * 0.3
# Negative score for conflicts
for conflict in conflicts:
if conflict.conflict_type != ConflictType.NONE:
score -= conflict.severity * 0.5
# Positive score for positive indicators
positive_count = sum(
1 for keywords in self.POSITIVE_INDICATORS.values()
for keyword in keywords
if keyword in intent
)
score += positive_count * 0.2
# Clamp to range
return max(-1.0, min(1.0, score))
def _determine_result(
self,
score: float,
conflicts: list[ConflictAssessment],
) -> ActionResult:
"""Determine the action result based on score and conflicts."""
# Check for severe conflicts
severe_conflicts = [
c for c in conflicts
if c.conflict_type != ConflictType.NONE and c.severity >= 0.9
]
if severe_conflicts:
return ActionResult.BLOCKED
# Score-based determination
if score < -0.5:
return ActionResult.BLOCKED
elif score < 0:
return ActionResult.REVIEW
elif score < 0.3:
return ActionResult.ALLOWED
else:
return ActionResult.ALLOWED
def _generate_reason(
self,
conflicts: list[ConflictAssessment],
impacts: list[ImpactAssessment],
score: float,
) -> str:
"""Generate a human-readable reason for the evaluation."""
if not conflicts and not impacts:
return "No potential issues detected"
if conflicts:
significant = [
c for c in conflicts
if c.conflict_type != ConflictType.NONE
]
if significant:
return "; ".join(c.description for c in significant[:2])
if impacts:
return "; ".join(i.description for i in impacts[:2])
return f"Evaluation score: {score:.2f}"
def _generate_alternative(
self,
conflicts: list[ConflictAssessment],
) -> Optional[str]:
"""Generate alternative suggestions for problematic intents."""
for conflict in conflicts:
if conflict.suggested_resolution:
return conflict.suggested_resolution
return None
def _generate_recommendations(
self,
conflicts: list[ConflictAssessment],
impacts: list[ImpactAssessment],
result: ActionResult,
) -> list[str]:
"""Generate recommendations based on the evaluation."""
recommendations = []
if result == ActionResult.BLOCKED:
recommendations.append(
"Consider reframing your request to focus on mutual benefit"
)
recommendations.append(
"Ensure all parties involved have given consent"
)
if result == ActionResult.REVIEW:
recommendations.append(
"Clarify your intent to ensure it doesn't impact others negatively"
)
for conflict in conflicts:
if conflict.suggested_resolution:
recommendations.append(conflict.suggested_resolution)
if not recommendations:
recommendations.append("Intent aligns with the Core Directive")
return list(set(recommendations)) # Remove duplicates
def _calculate_confidence(
self,
impacts: list[ImpactAssessment],
conflicts: list[ConflictAssessment],
) -> float:
"""Calculate confidence in the evaluation."""
if not impacts and not conflicts:
return 0.5 # Low confidence when no indicators found
# Higher confidence with more indicators
indicator_count = len(impacts) + len(conflicts)
return min(0.95, 0.6 + indicator_count * 0.1)
def _create_empty_evaluation(self) -> DetailedEvaluation:
"""Create an evaluation for empty input."""
base = DirectiveEvaluation(
result=ActionResult.REVIEW,
reason="No intent provided for evaluation",
confidence=1.0,
)
return DetailedEvaluation(
base_evaluation=base,
impacts=[],
conflicts=[],
overall_score=0.0,
recommendations=["Please provide a clear statement of intent"],
)
def __repr__(self) -> str:
return f"DirectiveEvaluator(evaluations={self._evaluation_count})"
# Module-level convenience functions
_default_evaluator: Optional[DirectiveEvaluator] = None
def get_evaluator() -> DirectiveEvaluator:
"""Get the default evaluator instance."""
global _default_evaluator
if _default_evaluator is None:
_default_evaluator = DirectiveEvaluator()
return _default_evaluator
def evaluate_detailed(
intent: str,
context: Optional[dict] = None,
) -> DetailedEvaluation:
"""Convenience function to perform detailed evaluation."""
return get_evaluator().evaluate(intent, context)