policyai 0.4.0

PolicyAI provides a mechanism for unstructured, composable policies that transform unstructured text into structured outputs.
Documentation
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
//! Generate regression analysis reports from PolicyAI evaluation data.
//!
//! This binary reads evaluation reports and generates comprehensive regression analysis
//! using confusion matrices and metrics to compare PolicyAI performance against baselines.

use std::fs::File;
use std::io::{self, BufRead, BufReader, Read};

use arrrg::CommandLine;
use policyai::analysis::{ConfusionMatrix, FieldMatchAccuracyMatrix, RegressionAnalysis};
use policyai::data::EvaluationReport;

#[derive(Clone, Default, Debug, Eq, PartialEq, arrrg_derive::CommandLine)]
struct Args {
    #[arrrg(flag, "Print detailed metrics for each field")]
    verbose: bool,
    #[arrrg(optional, "Output format (json, csv, text)")]
    format: Option<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (args, free) = Args::from_command_line_relaxed(
        "USAGE: policyai-regression-report [OPTIONS] [input_file...]",
    );

    let reports = if free.is_empty() {
        read_from_stdin()?
    } else {
        read_from_files(&free)?
    };

    if reports.is_empty() {
        eprintln!("No evaluation reports found in input");
        return Ok(());
    }

    let mut analysis = RegressionAnalysis::new();
    let mut accuracy_matrix = FieldMatchAccuracyMatrix::new();

    for report in &reports {
        analysis.add_report(&report.metrics);

        let expected_field_count = report
            .input
            .expected
            .as_ref()
            .and_then(|v| v.as_object())
            .map(|obj| obj.len())
            .unwrap_or(0);

        accuracy_matrix.add_report(&report.metrics, expected_field_count);
    }

    match args.format.as_deref().unwrap_or("text") {
        "json" => print_json(&analysis, &accuracy_matrix, &reports)?,
        "csv" => print_csv(&analysis, &accuracy_matrix)?,
        "text" => print_text(&analysis, &accuracy_matrix, &reports, args.verbose)?,
        _ => print_text(&analysis, &accuracy_matrix, &reports, args.verbose)?,
    }

    Ok(())
}

fn print_json(
    analysis: &RegressionAnalysis,
    accuracy_matrix: &FieldMatchAccuracyMatrix,
    _reports: &[EvaluationReport],
) -> Result<(), Box<dyn std::error::Error>> {
    let output = serde_json::json!({
        "summary": {
            "total_reports": analysis.total_reports,
            "policyai": {
                "avg_fields_matched": analysis.policyai_avg_fields_matched(),
                "total_wrong_values": analysis.policyai_total_wrong_values,
                "total_missing_fields": analysis.policyai_total_missing_fields,
                "total_extra_fields": analysis.policyai_total_extra_fields,
                "error_rate": analysis.policyai_error_rate(),
                "avg_duration_ms": analysis.policyai_avg_duration_ms(),
            },
            "baseline": {
                "avg_fields_matched": analysis.baseline_avg_fields_matched(),
                "total_wrong_values": analysis.baseline_total_wrong_values,
                "total_missing_fields": analysis.baseline_total_missing_fields,
                "total_extra_fields": analysis.baseline_total_extra_fields,
                "error_rate": analysis.baseline_error_rate(),
                "avg_duration_ms": analysis.baseline_avg_duration_ms(),
            },
            "comparison": {
                "fields_matched_improvement": analysis.policyai_avg_fields_matched() - analysis.baseline_avg_fields_matched(),
                "speed_ratio": if analysis.policyai_avg_duration_ms() > 0.0 {
                    analysis.baseline_avg_duration_ms() / analysis.policyai_avg_duration_ms()
                } else {
                    0.0
                },
                "error_rate_difference": analysis.policyai_error_rate() - analysis.baseline_error_rate(),
            },
            "field_match_accuracy": {
                "confusion_matrix": {
                    "true_positive": accuracy_matrix.confusion_matrix.true_positive,
                    "false_positive": accuracy_matrix.confusion_matrix.false_positive,
                    "true_negative": accuracy_matrix.confusion_matrix.true_negative,
                    "false_negative": accuracy_matrix.confusion_matrix.false_negative,
                },
                "metrics": {
                    "precision": accuracy_matrix.confusion_matrix.precision(),
                    "recall": accuracy_matrix.confusion_matrix.recall(),
                    "f1_score": accuracy_matrix.confusion_matrix.f1_score(),
                    "accuracy": accuracy_matrix.confusion_matrix.accuracy(),
                }
            }
        }
    });
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

fn print_csv(
    analysis: &RegressionAnalysis,
    accuracy_matrix: &FieldMatchAccuracyMatrix,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("metric,policyai_total,baseline_total,policyai_avg,baseline_avg,improvement");
    println!(
        "fields_matched,{},{},{:.4},{:.4},{:.4}",
        analysis.policyai_total_fields_matched,
        analysis.baseline_total_fields_matched,
        analysis.policyai_avg_fields_matched(),
        analysis.baseline_avg_fields_matched(),
        analysis.policyai_avg_fields_matched() - analysis.baseline_avg_fields_matched()
    );
    println!(
        "wrong_values,{},{},,,",
        analysis.policyai_total_wrong_values, analysis.baseline_total_wrong_values
    );
    println!(
        "missing_fields,{},{},,,",
        analysis.policyai_total_missing_fields, analysis.baseline_total_missing_fields
    );
    println!(
        "extra_fields,{},{},,,",
        analysis.policyai_total_extra_fields, analysis.baseline_total_extra_fields
    );
    println!(
        "errors,{},{},{:.4},{:.4},{:.4}",
        analysis.policyai_errors,
        analysis.baseline_errors,
        analysis.policyai_error_rate(),
        analysis.baseline_error_rate(),
        analysis.policyai_error_rate() - analysis.baseline_error_rate()
    );
    println!(
        "duration_ms,{},{},{:.2},{:.2},{:.2}",
        analysis.policyai_total_duration_ms,
        analysis.baseline_total_duration_ms,
        analysis.policyai_avg_duration_ms(),
        analysis.baseline_avg_duration_ms(),
        if analysis.policyai_avg_duration_ms() > 0.0 {
            analysis.baseline_avg_duration_ms() / analysis.policyai_avg_duration_ms()
        } else {
            0.0
        }
    );

    println!("\nfield_match_accuracy_matrix,value");
    println!(
        "true_positive,{}",
        accuracy_matrix.confusion_matrix.true_positive
    );
    println!(
        "false_positive,{}",
        accuracy_matrix.confusion_matrix.false_positive
    );
    println!(
        "true_negative,{}",
        accuracy_matrix.confusion_matrix.true_negative
    );
    println!(
        "false_negative,{}",
        accuracy_matrix.confusion_matrix.false_negative
    );
    println!(
        "precision,{:.4}",
        accuracy_matrix.confusion_matrix.precision()
    );
    println!("recall,{:.4}", accuracy_matrix.confusion_matrix.recall());
    println!(
        "f1_score,{:.4}",
        accuracy_matrix.confusion_matrix.f1_score()
    );
    println!(
        "accuracy,{:.4}",
        accuracy_matrix.confusion_matrix.accuracy()
    );

    Ok(())
}

fn print_text(
    analysis: &RegressionAnalysis,
    accuracy_matrix: &FieldMatchAccuracyMatrix,
    _reports: &[EvaluationReport],
    verbose: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("PolicyAI Regression Analysis Report");
    println!("===================================");
    println!("Total evaluation reports: {}", analysis.total_reports);
    println!();

    println!("Performance Comparison:");
    println!("----------------------");

    println!("Fields Matched:");
    println!(
        "  PolicyAI avg: {:.2}",
        analysis.policyai_avg_fields_matched()
    );
    println!(
        "  Baseline avg: {:.2}",
        analysis.baseline_avg_fields_matched()
    );
    println!(
        "  Improvement:  {:.2}",
        analysis.policyai_avg_fields_matched() - analysis.baseline_avg_fields_matched()
    );
    println!();

    println!("Error Rates:");
    println!(
        "  PolicyAI: {:.1}% ({} errors)",
        analysis.policyai_error_rate() * 100.0,
        analysis.policyai_errors
    );
    println!(
        "  Baseline: {:.1}% ({} errors)",
        analysis.baseline_error_rate() * 100.0,
        analysis.baseline_errors
    );
    println!(
        "  Difference: {:.1} percentage points",
        (analysis.policyai_error_rate() - analysis.baseline_error_rate()) * 100.0
    );
    println!();

    println!("Performance:");
    println!(
        "  PolicyAI avg duration: {:.2} ms",
        analysis.policyai_avg_duration_ms()
    );
    println!(
        "  Baseline avg duration: {:.2} ms",
        analysis.baseline_avg_duration_ms()
    );
    if analysis.policyai_avg_duration_ms() > 0.0 {
        let speed_ratio = analysis.baseline_avg_duration_ms() / analysis.policyai_avg_duration_ms();
        println!("  Speed ratio (baseline/policyai): {:.2}x", speed_ratio);
    }
    println!();

    println!("Field Quality:");
    println!("Wrong Values:");
    println!("  PolicyAI total: {}", analysis.policyai_total_wrong_values);
    println!("  Baseline total: {}", analysis.baseline_total_wrong_values);
    println!();

    println!("Missing Fields:");
    println!(
        "  PolicyAI total: {}",
        analysis.policyai_total_missing_fields
    );
    println!(
        "  Baseline total: {}",
        analysis.baseline_total_missing_fields
    );
    println!();

    println!("Extra Fields:");
    println!("  PolicyAI total: {}", analysis.policyai_total_extra_fields);
    println!("  Baseline total: {}", analysis.baseline_total_extra_fields);
    println!();

    // Display confusion matrix for field matching accuracy
    print_confusion_matrix_text(
        "Field Match Accuracy (PolicyAI vs Baseline)",
        &accuracy_matrix.confusion_matrix,
    );

    if verbose {
        println!("Additional Details:");
        println!("------------------");
        println!("Total Duration:");
        println!(
            "  PolicyAI total: {} ms",
            analysis.policyai_total_duration_ms
        );
        println!(
            "  Baseline total: {} ms",
            analysis.baseline_total_duration_ms
        );
        println!();
    }

    Ok(())
}

fn print_confusion_matrix_text(name: &str, matrix: &ConfusionMatrix) {
    println!("{}:", name);

    // Print confusion matrix in tabular format
    let tp = matrix.true_positive;
    let fp = matrix.false_positive;
    let tn = matrix.true_negative;
    let fn_val = matrix.false_negative;

    // Calculate column widths for alignment
    let values = [tp, fp, tn, fn_val];
    let max_val = values.iter().max().unwrap();
    let val_width = format!("{}", max_val).len().max(4);

    println!("  Confusion Matrix:");
    println!("                     │ PolicyAI");
    println!(
        "                     │ {:>width$} {:>width$}",
        "Correct",
        "Wrong",
        width = val_width + 8
    );
    println!(
        "    ─────────────────┼{:─<width$}─{:─<width$}──",
        "",
        "",
        width = val_width + 8
    );
    let total = tp + fp + tn + fn_val;
    println!(
        "    Baseline Correct │ {:>width$} {:>width$}",
        format!("{} ({:.1}%)", tp, 100.0 * tp as f64 / total as f64),
        format!("{} ({:.1}%)", fn_val, 100.0 * fn_val as f64 / total as f64),
        width = val_width + 8 // Add space for percentage
    );
    println!(
        "               Wrong │ {:>width$} {:>width$}",
        format!("{} ({:.1}%)", fp, 100.0 * fp as f64 / total as f64),
        format!("{} ({:.1}%)", tn, 100.0 * tn as f64 / total as f64),
        width = val_width + 8
    );
    println!();

    // Print metrics
    println!("  Metrics:");
    println!(
        "    Precision: {:.4} (when PolicyAI says correct, how often is it right)",
        matrix.precision()
    );
    println!(
        "    Recall:    {:.4} (when baseline is correct, how often does PolicyAI get it right)",
        matrix.recall()
    );
    println!("    F1 Score:  {:.4}", matrix.f1_score());
    println!(
        "    Accuracy:  {:.4} (overall agreement rate)",
        matrix.accuracy()
    );

    println!();
}

fn read_from_stdin() -> Result<Vec<EvaluationReport>, Box<dyn std::error::Error>> {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input)?;

    let reports: Vec<EvaluationReport> = input
        .lines()
        .filter(|line| !line.trim().is_empty())
        .map(serde_json::from_str)
        .collect::<Result<Vec<_>, _>>()?;

    Ok(reports)
}

fn read_from_files(files: &[String]) -> Result<Vec<EvaluationReport>, Box<dyn std::error::Error>> {
    let mut reports = Vec::new();

    for file_path in files {
        let file = File::open(file_path)?;
        let reader = BufReader::new(file);

        for line in reader.lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            let report: EvaluationReport = match serde_json::from_str(&line) {
                Ok(report) => report,
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to parse line in {file_path} as EvaluationReport: {e}"
                    );
                    continue;
                }
            };

            reports.push(report);
        }
    }

    Ok(reports)
}