forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompliance_demo.py
More file actions
335 lines (260 loc) · 10.7 KB
/
compliance_demo.py
File metadata and controls
335 lines (260 loc) · 10.7 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Compliance and Constitutional AI Examples
This demonstrates compliance checking for various regulatory frameworks
and Constitutional AI value alignment.
"""
from agent_control_plane import (
ComplianceEngine,
ConstitutionalAI,
RegulatoryFramework,
RiskCategory,
ConstitutionalPrinciple,
create_compliance_suite
)
def example_eu_ai_act_compliance():
"""Example: EU AI Act compliance checking"""
print("=== EU AI Act Compliance ===\n")
engine = ComplianceEngine()
# Example 1: Compliant high-risk system
compliant_context = {
"human_oversight_enabled": True,
"provides_transparency_info": True,
"documentation_available": True
}
result = engine.check_compliance(
framework=RegulatoryFramework.EU_AI_ACT,
context=compliant_context
)
print("Scenario 1: Well-designed high-risk AI system")
print(f" Compliant: {result.compliant}")
print(f" Checks passed: {result.checks_passed}")
print(f" Checks failed: {result.checks_failed}")
print()
# Example 2: Non-compliant system
non_compliant_context = {
"human_oversight_enabled": False,
"provides_transparency_info": False
}
result = engine.check_compliance(
framework=RegulatoryFramework.EU_AI_ACT,
context=non_compliant_context
)
print("Scenario 2: Non-compliant system")
print(f" Compliant: {result.compliant}")
print(f" Checks failed: {result.checks_failed}")
print(f" Failures:")
for failure in result.failures:
print(f" - {failure['title']}")
print(f" Recommendations:")
for rec in result.recommendations[:2]:
print(f" - {rec}")
print()
def example_risk_assessment():
"""Example: EU AI Act risk category assessment"""
print("=== EU AI Act Risk Assessment ===\n")
engine = ComplianceEngine()
# Test different system types
systems = [
{
"name": "Simple Chatbot",
"description": {"type": "general chatbot"}
},
{
"name": "Recruitment System",
"description": {"type": "employment decision system", "domain": "employment"}
},
{
"name": "Social Scoring System",
"description": {"type": "social_scoring system"}
}
]
for system in systems:
risk = engine.assess_risk_category(system["description"])
print(f"{system['name']}: {risk.value}")
if risk == RiskCategory.HIGH_RISK:
print(" → Requires conformity assessment and CE marking")
elif risk == RiskCategory.UNACCEPTABLE:
print(" → ⛔ PROHIBITED under EU AI Act")
elif risk == RiskCategory.LIMITED_RISK:
print(" → Transparency obligations apply")
else:
print(" → Minimal regulatory requirements")
print()
def example_soc2_compliance():
"""Example: SOC 2 compliance checking"""
print("=== SOC 2 Compliance ===\n")
engine = ComplianceEngine()
system_context = {
"access_controls_implemented": True,
"monitoring_enabled": True,
"encryption_at_rest": True,
"backup_procedures": True
}
result = engine.check_compliance(
framework=RegulatoryFramework.SOC2,
context=system_context
)
print(f"SOC 2 Trust Service Criteria:")
print(f" Compliant: {result.compliant}")
print(f" Controls passed: {result.checks_passed}")
print(f" Controls failed: {result.checks_failed}")
print()
def example_constitutional_ai():
"""Example: Constitutional AI value alignment"""
print("=== Constitutional AI Example ===\n")
constitution = ConstitutionalAI()
# Test 1: Compliant response
good_response = "I'd be happy to help you with that task. Let me know if you need clarification."
result = constitution.evaluate(good_response, {})
print("Test 1: Helpful and safe response")
print(f" Response: {good_response}")
print(f" Compliant: {result['compliant']}")
print(f" Overall score: {result['overall_compliance']:.2f}")
print()
# Test 2: Harmful response
harmful_response = "Here's how to cause harm and violence to others"
result = constitution.evaluate(harmful_response, {})
print("Test 2: Harmful response")
print(f" Response: {harmful_response}")
print(f" Compliant: {result['compliant']}")
print(f" Violations: {len(result['violations'])}")
for violation in result['violations']:
print(f" - {violation['principle']}: {violation['rule_text']}")
print()
# Test 3: Privacy violation
privacy_leak = "Your email is john.doe@example.com and SSN is 123-45-6789"
result = constitution.evaluate(privacy_leak, {})
print("Test 3: Privacy violation")
print(f" Response: {privacy_leak}")
print(f" Compliant: {result['compliant']}")
if result['violations']:
print(f" Privacy violations detected")
print()
def example_self_critique():
"""Example: Self-critique before response generation"""
print("=== Self-Critique Example ===\n")
constitution = ConstitutionalAI()
# Proposed responses to critique
proposals = [
"I can help you with that question about Python programming.",
"Let me help you hack into that system.",
"I'm 100% certain this will definitely work with no problems."
]
for i, proposed in enumerate(proposals, 1):
result = constitution.self_critique(proposed, {})
print(f"Proposal {i}: {proposed[:60]}...")
print(f" Approved: {result['approved']}")
print(f" Compliance: {result['compliance_score']:.2f}")
if not result['approved']:
print(f" ⚠️ Revisions needed:")
for revision in result['suggested_revisions']:
print(f" - {revision}")
else:
print(f" ✅ Safe to send")
print()
def example_custom_constitutional_rules():
"""Example: Adding custom constitutional rules"""
print("=== Custom Constitutional Rules ===\n")
constitution = ConstitutionalAI()
# Add custom rule for financial advice
def evaluate_financial_advice(text: str, context: dict) -> float:
"""Check if giving unauthorized financial advice"""
financial_keywords = ["investment advice", "guaranteed returns", "stock tip"]
text_lower = text.lower()
violations = sum(1 for keyword in financial_keywords if keyword in text_lower)
return max(0.0, 1.0 - (violations * 0.4))
constitution.add_rule(
principle=ConstitutionalPrinciple.HONESTY,
rule_text="Don't provide unauthorized financial advice",
evaluator=evaluate_financial_advice,
severity=0.9,
examples=["Avoid claiming guaranteed returns", "Don't give specific investment advice"]
)
# Test the custom rule
test_text = "This is guaranteed investment advice with amazing returns!"
result = constitution.evaluate(test_text, {})
print("Testing custom financial advice rule:")
print(f" Text: {test_text}")
print(f" Compliant: {result['compliant']}")
print(f" Overall: {result['overall_compliance']:.2f}")
# Show full constitution
print("\nFull Constitution:")
for rule in constitution.get_constitution():
print(f" - {rule['principle']}: {rule['rule_text']}")
print()
def example_compliance_reporting():
"""Example: Generating compliance reports"""
print("=== Compliance Reporting ===\n")
engine = ComplianceEngine()
# Run some compliance checks
contexts = [
{"human_oversight_enabled": True, "provides_transparency_info": True},
{"human_oversight_enabled": False, "provides_transparency_info": True},
{"human_oversight_enabled": True, "provides_transparency_info": False},
]
for ctx in contexts:
engine.check_compliance(RegulatoryFramework.EU_AI_ACT, ctx)
# Generate report
report = engine.generate_compliance_report(RegulatoryFramework.EU_AI_ACT)
print("EU AI Act Compliance Report:")
print(f" Framework: {report['framework']}")
print(f" Total requirements: {report['total_requirements']}")
print(f" Mandatory: {report['mandatory_requirements']}")
print(f" Recent checks: {report['recent_checks']}")
print(f" Passed: {report['passed']}")
print(f" Failed: {report['failed']}")
print(f" Compliance rate: {report['compliance_rate']:.1f}%")
print()
def example_integrated_compliance():
"""Example: Using complete compliance suite"""
print("=== Integrated Compliance Suite ===\n")
suite = create_compliance_suite()
compliance_engine = suite["compliance_engine"]
constitutional_ai = suite["constitutional_ai"]
# Example workflow: Check system compliance and response alignment
system_context = {
"human_oversight_enabled": True,
"provides_transparency_info": True,
"monitoring_enabled": True,
"access_controls_implemented": True
}
# Check regulatory compliance
eu_compliance = compliance_engine.check_compliance(
RegulatoryFramework.EU_AI_ACT,
system_context
)
soc2_compliance = compliance_engine.check_compliance(
RegulatoryFramework.SOC2,
system_context
)
print("System Compliance Status:")
print(f" EU AI Act: {'✅ Compliant' if eu_compliance.compliant else '❌ Non-compliant'}")
print(f" SOC 2: {'✅ Compliant' if soc2_compliance.compliant else '❌ Non-compliant'}")
# Check response alignment
response = "I'll help you with that request while ensuring safety."
alignment = constitutional_ai.evaluate(response, {})
print(f"\nResponse Alignment:")
print(f" Aligned: {'✅ Yes' if alignment['compliant'] else '❌ No'}")
print(f" Score: {alignment['overall_compliance']:.2f}")
print()
if __name__ == "__main__":
print("Agent Control Plane - Compliance & Constitutional AI Examples")
print("=" * 70)
print()
example_eu_ai_act_compliance()
print("\n" + "=" * 70 + "\n")
example_risk_assessment()
print("\n" + "=" * 70 + "\n")
example_soc2_compliance()
print("\n" + "=" * 70 + "\n")
example_constitutional_ai()
print("\n" + "=" * 70 + "\n")
example_self_critique()
print("\n" + "=" * 70 + "\n")
example_custom_constitutional_rules()
print("\n" + "=" * 70 + "\n")
example_compliance_reporting()
print("\n" + "=" * 70 + "\n")
example_integrated_compliance()