-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_ai_auditing.cpp
More file actions
337 lines (283 loc) · 13.3 KB
/
example_ai_auditing.cpp
File metadata and controls
337 lines (283 loc) · 13.3 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
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: example_ai_auditing.cpp ║
Version: 0.0.34 ║
Last Modified: 2026-03-09 03:52:18 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 336 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 2a1fb0423 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
• a629043ab 2026-02-22 Audit: document gaps found - benchmarks and stale annotat... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
/**
* @file example_ai_auditing.cpp
* @brief Example demonstrating AI Decision Auditing and Explainability features
*
* This example shows how to:
* 1. Log AI decisions with full context
* 2. Generate human-readable explanations
* 3. Query audit logs
* 4. Handle human oversight and overrides
* 5. Export for compliance reporting
*/
#include "llm/ai_decision_auditor.h"
#include "llm/explanation_generator.h"
#include <iostream>
#include <memory>
using namespace themis::llm;
/**
* Example: Basic AI Decision Logging
*/
void example_basic_logging(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 1: Basic AI Decision Logging ===\n";
// Create an AI decision audit entry
AIDecisionAudit audit;
audit.user_id = "user_alice";
audit.session_id = "session_20240112";
audit.query = "What are the health benefits of green tea?";
// Model information
audit.model_name = "health-advisor-model";
audit.model_version = "2.1";
audit.model_params = json::object();
audit.model_params["temperature"] = 0.7;
audit.model_params["max_tokens"] = 500;
// Response
audit.response = "Green tea contains antioxidants called catechins that may "
"help reduce inflammation, support heart health, and boost metabolism. "
"It also contains caffeine and L-theanine which can improve focus.";
audit.confidence_score = 0.92f;
audit.token_count = 85;
audit.latency_ms = 245;
// Reasoning chain
audit.reasoning_steps = {
"Identified query as health-related question",
"Retrieved scientific literature on green tea benefits",
"Synthesized evidence-based response",
"Validated against medical knowledge base"
};
// Key factors
audit.key_factors = json::object();
audit.key_factors["query_category"] = "health_information";
audit.key_factors["evidence_quality"] = "peer_reviewed";
audit.key_factors["medical_disclaimer"] = true;
// Log the decision
auto stored = auditor.logDecision(audit);
std::cout << "✓ Decision logged with ID: " << stored.decision_id << "\n";
std::cout << " Confidence: " << (stored.confidence_score * 100) << "%\n";
std::cout << " Review required: " << (stored.requires_human_review ? "Yes" : "No") << "\n";
}
/**
* Example: Low Confidence Decision (Auto-flagged for Review)
*/
void example_low_confidence(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 2: Low Confidence Decision ===\n";
AIDecisionAudit audit;
audit.user_id = "user_bob";
audit.query = "What will the stock market do tomorrow?";
audit.model_name = "financial-advisor-model";
audit.model_version = "1.5";
audit.response = "Market predictions are highly uncertain. Consider diversification.";
audit.confidence_score = 0.45f; // Below 0.7 threshold - will be auto-flagged
audit.latency_ms = 180;
auto stored = auditor.logDecision(audit);
std::cout << "✓ Decision logged with ID: " << stored.decision_id << "\n";
std::cout << " Confidence: " << (stored.confidence_score * 100) << "%\n";
std::cout << " ⚠️ Auto-flagged for human review (confidence < 70%)\n";
}
/**
* Example: Generating Explanations
*/
void example_explanations(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 3: Generating Explanations ===\n";
// Create and log a decision
AIDecisionAudit audit;
audit.user_id = "user_charlie";
audit.query = "Is it safe to travel to Tokyo?";
audit.model_name = "travel-advisor-model";
audit.model_version = "3.0";
audit.response = "Tokyo is generally very safe for travelers. It has low crime rates "
"and excellent public transportation.";
audit.confidence_score = 0.88f;
audit.reasoning_steps = {
"Checked current travel advisories",
"Analyzed crime statistics",
"Reviewed recent traveler feedback",
"Considered public safety infrastructure"
};
audit.key_factors = json::object();
audit.key_factors["crime_rate"] = "low";
audit.key_factors["travel_advisory_level"] = "0";
audit.key_factors["public_safety_score"] = 9.2;
auto stored = auditor.logDecision(audit);
// Generate different explanation formats
ExplanationGenerator generator;
// User-friendly explanation
std::cout << "\n--- User-Friendly Explanation ---\n";
std::string user_friendly = generator.generateExplanation(
stored.query,
stored.response,
stored.reasoning_steps,
stored.key_factors,
ExplanationGenerator::Format::USER_FRIENDLY
);
std::cout << user_friendly << "\n";
// Compliance explanation (GDPR/EU AI Act)
std::cout << "\n--- Compliance Explanation (GDPR/EU AI Act) ---\n";
std::string compliance = generator.generateComplianceExplanation(
stored.query,
stored.response,
stored.model_name + " v" + stored.model_version,
stored.reasoning_steps,
stored.key_factors,
stored.confidence_score
);
std::cout << compliance << "\n";
}
/**
* Example: Querying Audit Logs
*/
void example_querying(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 4: Querying Audit Logs ===\n";
// Query 1: Find all decisions needing review
std::cout << "\nQuery 1: Decisions requiring human review\n";
AIDecisionAuditor::QueryFilter filter1;
filter1.requires_review = true;
filter1.limit = 10;
auto review_needed = auditor.queryAuditLog(filter1);
std::cout << "Found " << review_needed.size() << " decisions needing review\n";
for (const auto& decision : review_needed) {
std::cout << " - " << decision.decision_id
<< " (confidence: " << (decision.confidence_score * 100) << "%)\n";
}
// Query 2: Find low confidence decisions
std::cout << "\nQuery 2: Low confidence decisions (<80%)\n";
AIDecisionAuditor::QueryFilter filter2;
filter2.max_confidence = 0.8f;
filter2.limit = 10;
auto low_confidence = auditor.queryAuditLog(filter2);
std::cout << "Found " << low_confidence.size() << " low confidence decisions\n";
// Query 3: Find decisions for specific user
std::cout << "\nQuery 3: Decisions for user 'user_alice'\n";
AIDecisionAuditor::QueryFilter filter3;
filter3.user_id = "user_alice";
filter3.limit = 10;
auto user_decisions = auditor.queryAuditLog(filter3);
std::cout << "Found " << user_decisions.size() << " decisions for user_alice\n";
}
/**
* Example: Human Override
*/
void example_human_override(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 5: Human Override ===\n";
// Create a decision that will be overridden
AIDecisionAudit audit;
audit.user_id = "user_david";
audit.query = "What is 2+2?";
audit.model_name = "math-model";
audit.response = "5"; // Incorrect!
audit.confidence_score = 0.6f;
auto stored = auditor.logDecision(audit);
std::cout << "Original decision: " << stored.decision_id << "\n";
std::cout << " Response: " << stored.response << "\n";
std::cout << " Flagged for review: Yes\n";
// Human reviewer corrects the mistake
bool success = auditor.recordOverride(
stored.decision_id,
"Corrected mathematical error. Correct answer is 4.",
"reviewer_emma"
);
if (success) {
std::cout << "\n✓ Human override recorded\n";
std::cout << " Reviewer: reviewer_emma\n";
std::cout << " Corrected response: 4\n";
// Retrieve updated decision
auto updated = auditor.getDecision(stored.decision_id);
if (updated.has_value()) {
std::cout << " Review flag cleared: "
<< (!updated->requires_human_review ? "Yes" : "No") << "\n";
}
}
}
/**
* Example: Statistics and Reporting
*/
void example_statistics(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 6: Statistics and Reporting ===\n";
auto stats = auditor.getStats();
std::cout << "Audit Statistics:\n";
std::cout << " Total decisions: " << stats.total_decisions << "\n";
std::cout << " Flagged for review: " << stats.flagged_for_review << "\n";
std::cout << " Human overrides: " << stats.human_overrides << "\n";
std::cout << " Average confidence: " << (stats.avg_confidence * 100) << "%\n";
std::cout << " Average latency: " << stats.avg_latency_ms << "ms\n";
// Calculate review rate
if (stats.total_decisions > 0) {
float review_rate = (float)stats.flagged_for_review / stats.total_decisions * 100;
std::cout << " Review rate: " << std::fixed << std::setprecision(1)
<< review_rate << "%\n";
if (review_rate > 30) {
std::cout << " ⚠️ High review rate - consider improving model\n";
}
}
}
/**
* Example: Compliance Export
*/
void example_compliance_export(AIDecisionAuditor& auditor) {
std::cout << "\n=== Example 7: Compliance Export ===\n";
AIDecisionAuditor::QueryFilter filter;
// Export all decisions (in practice, would filter by date range)
std::string export_path = "data/compliance_report.json";
bool success = auditor.exportForCompliance(export_path, filter);
if (success) {
std::cout << "✓ Compliance report exported to: " << export_path << "\n";
std::cout << " Format: JSON\n";
std::cout << " Contains: All decisions with full audit trail\n";
std::cout << " Use case: GDPR Article 22 compliance, EU AI Act reporting\n";
}
}
/**
* Main example function
*/
int main() {
std::cout << "====================================\n";
std::cout << "AI Decision Auditing Example\n";
std::cout << "====================================\n";
try {
// Note: In real usage, you would initialize RocksDB
// For this example, we show the API usage patterns
// AIDecisionAuditor auditor(db, nullptr);
// Run examples
// example_basic_logging(auditor);
// example_low_confidence(auditor);
// example_explanations(auditor);
// example_querying(auditor);
// example_human_override(auditor);
// example_statistics(auditor);
// example_compliance_export(auditor);
std::cout << "\n====================================\n";
std::cout << "Example completed successfully!\n";
std::cout << "====================================\n";
std::cout << "\nNote: This example demonstrates API usage.\n";
std::cout << "In production:\n";
std::cout << "1. Initialize RocksDB instance\n";
std::cout << "2. Create AIDecisionAuditor with DB\n";
std::cout << "3. Optionally provide PKI client for signing\n";
std::cout << "4. Configure via ai_audit_config.yaml\n";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
return 0;
}