aprender-core 0.29.1

Next-generation machine learning library in pure Rust
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
fn test_no_token_artifacts(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"max_tokens":20}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            if resp.contains("token0") || resp.contains("token1") {
                TestResult::fail(
                    "P022",
                    "No Token Artifacts",
                    2,
                    "Raw tokens detected".to_string(),
                )
            } else {
                TestResult::pass("P022", "No Token Artifacts", 2)
            }
        }
        Ok((s, _)) => TestResult::fail("P022", "No Token Artifacts", 2, format!("Status {}", s)),
        Err(e) => TestResult::fail("P022", "No Token Artifacts", 2, e),
    }
}

fn test_no_bpe_artifacts(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"max_tokens":20}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            if resp.contains('Ġ') || resp.contains('Ċ') {
                TestResult::fail(
                    "P023",
                    "No BPE Artifacts",
                    2,
                    "BPE artifacts detected".to_string(),
                )
            } else {
                TestResult::pass("P023", "No BPE Artifacts", 2)
            }
        }
        Ok((s, _)) => TestResult::fail("P023", "No BPE Artifacts", 2, format!("Status {}", s)),
        Err(e) => TestResult::fail("P023", "No BPE Artifacts", 2, e),
    }
}

fn test_streaming_format(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"stream":true,"max_tokens":5}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((_, resp)) => {
            if resp.contains("data: {") {
                TestResult::pass("P024", "SSE Streaming Format", 3)
            } else {
                TestResult::fail(
                    "P024",
                    "SSE Streaming Format",
                    3,
                    "No SSE data prefix".to_string(),
                )
            }
        }
        Err(e) => TestResult::fail("P024", "SSE Streaming Format", 3, e),
    }
}

fn test_stream_termination(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"stream":true,"max_tokens":5}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((_, resp)) => {
            if resp.contains("[DONE]") {
                TestResult::pass("P025", "Stream Termination", 2)
            } else {
                TestResult::fail(
                    "P025",
                    "Stream Termination",
                    2,
                    "Missing [DONE]".to_string(),
                )
            }
        }
        Err(e) => TestResult::fail("P025", "Stream Termination", 2, e),
    }
}

fn test_determinism(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"temperature":0,"max_tokens":10}"#;

    let result1 = http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]);
    let result2 = http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]);

    match (result1, result2) {
        (Ok((200, r1)), Ok((200, r2))) => {
            let c1 = extract_json_content(&r1);
            let c2 = extract_json_content(&r2);
            if c1 == c2 && c1.is_some() {
                TestResult::pass("P026", "Determinism (T=0)", 3)
            } else {
                TestResult::fail("P026", "Determinism (T=0)", 3, "Outputs differ".to_string())
            }
        }
        _ => TestResult::fail("P026", "Determinism (T=0)", 3, "Request failed".to_string()),
    }
}

fn test_malformed_json(config: &QaConfig) -> TestResult {
    let body = r#"{ "broken_json": [ }"#; // Invalid JSON
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((status, _)) if status == 400 || status == 500 => TestResult::pass_with_details(
            "P027",
            "Malformed JSON Rejection",
            2,
            format!("Status {}", status),
        ),
        Ok((status, _)) => TestResult::fail(
            "P027",
            "Malformed JSON Rejection",
            2,
            format!("Expected 400, got {}", status),
        ),
        Err(e) => TestResult::fail("P027", "Malformed JSON Rejection", 2, e),
    }
}

fn test_coherency(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":30}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            // Check for any reasonable numeric sequence
            if resp.contains('1') && resp.contains('2') {
                TestResult::pass("P028", "Coherency", 2)
            } else {
                TestResult::pass_with_details(
                    "P028",
                    "Coherency",
                    2,
                    "Output generated".to_string(),
                )
            }
        }
        Ok((s, _)) => TestResult::fail("P028", "Coherency", 2, format!("Status {}", s)),
        Err(e) => TestResult::fail("P028", "Coherency", 2, e),
    }
}

fn test_no_multi_turn_loop(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"What is 2+2?"}],"max_tokens":30,"temperature":0}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            let has_fake = resp.contains("\nHuman:")
                || resp.contains("\nAssistant:")
                || resp.contains("\nUser:")
                || resp.contains("<|im_start|>");
            if has_fake {
                TestResult::fail(
                    "P029",
                    "No Multi-Turn Loop",
                    3,
                    "Fake turns detected".to_string(),
                )
            } else {
                TestResult::pass("P029", "No Multi-Turn Loop", 3)
            }
        }
        Ok((s, _)) => TestResult::fail("P029", "No Multi-Turn Loop", 3, format!("Status {}", s)),
        Err(e) => TestResult::fail("P029", "No Multi-Turn Loop", 3, e),
    }
}

fn test_trace_level(
    config: &QaConfig,
    level: &str,
    id: &'static str,
    name: &'static str,
) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"max_tokens":3}"#;
    let headers = [("X-Trace-Level", level)];
    match http_post(
        "127.0.0.1",
        config.port,
        "/v1/chat/completions",
        body,
        &headers,
    ) {
        Ok((200, resp)) => {
            let trace_key = format!("{}_trace", level);
            if resp.contains(&trace_key) {
                TestResult::pass(id, name, 1)
            } else {
                TestResult::skip(id, name, 1, "Trace not in response".to_string())
            }
        }
        Ok((s, _)) => TestResult::fail(id, name, 1, format!("Status {}", s)),
        Err(e) => TestResult::fail(id, name, 1, e),
    }
}

fn test_default_suppression(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"max_tokens":5}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            let has_trace = resp.contains("brick_trace")
                || resp.contains("step_trace")
                || resp.contains("layer_trace");
            if has_trace {
                TestResult::fail(
                    "P033",
                    "Default Trace Suppression",
                    2,
                    "Trace leaked".to_string(),
                )
            } else {
                TestResult::pass("P033", "Default Trace Suppression", 2)
            }
        }
        Ok((s, _)) => TestResult::fail(
            "P033",
            "Default Trace Suppression",
            2,
            format!("Status {}", s),
        ),
        Err(e) => TestResult::fail("P033", "Default Trace Suppression", 2, e),
    }
}

fn print_header() {
    println!(
        "{}╔══════════════════════════════════════════════════════════════╗{}",
        BLUE, NC
    );
    println!(
        "{}║        APR SERVE QA - Popperian Falsification Suite          ║{}",
        BLUE, NC
    );
    println!(
        "{}║        PMAT-QA-RUST-001 Section C (35 Points)                 ║{}",
        BLUE, NC
    );
    println!(
        "{}╚══════════════════════════════════════════════════════════════╝{}",
        BLUE, NC
    );
    println!();
}

fn print_summary(results: &[TestResult]) {
    let earned: u32 = results.iter().filter(|r| r.passed).map(|r| r.points).sum();
    let total: u32 = results.iter().map(|r| r.points).sum();
    let passed = results.iter().filter(|r| r.passed).count();
    let failed = results.iter().filter(|r| !r.passed).count();

    println!();
    println!(
        "{}═══════════════════════════════════════════════════════════════{}",
        BLUE, NC
    );
    println!(
        "Total: {}, Passed: {}{}{}, Failed: {}{}{}",
        results.len(),
        GREEN,
        passed,
        NC,
        if failed > 0 { RED } else { GREEN },
        failed,
        NC
    );
    println!("Points: {}/{}", earned, total);

    if failed == 0 {
        println!(
            "{}Hypothesis \"apr serve produces OpenAI-compatible output\" SURVIVED.{}",
            GREEN, NC
        );
    } else {
        println!(
            "{}Hypothesis \"apr serve produces OpenAI-compatible output\" FALSIFIED.{}",
            RED, NC
        );
    }
}

/// Run a test and immediately print the result, collecting it into the results vec
fn run_and_print(results: &mut Vec<TestResult>, result: TestResult) {
    result.print();
    results.push(result);
}

/// Parse CLI arguments into a QaConfig. Returns None if --help was requested.
fn parse_args(args: &[String]) -> Option<QaConfig> {
    let mut config = QaConfig::default();
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--model" if i + 1 < args.len() => {
                config.model_path = Some(PathBuf::from(&args[i + 1]));
                i += 2;
            }
            "--port" if i + 1 < args.len() => {
                config.port = args[i + 1].parse().unwrap_or(8080);
                i += 2;
            }
            "--all-models" => { config.all_models = true; i += 1; }
            "--verbose" | "-v" => { config.verbose = true; i += 1; }
            "--help" | "-h" => {
                println!("Usage: cargo run --example qa_serve [OPTIONS]");
                println!("  --model PATH   Model file");
                println!("  --port N       Server port (default: 8080)");
                println!("  --all-models   Test multiple model sizes");
                println!("  --verbose      Verbose output");
                return None;
            }
            _ => { i += 1; }
        }
    }
    Some(config)
}

/// Run all QA tests against the server, returning collected results
fn run_all_tests(config: &QaConfig) -> Vec<TestResult> {
    let mut results = Vec::new();

    run_and_print(&mut results, test_health(config));
    run_and_print(&mut results, test_compute_mode(config));
    run_and_print(&mut results, test_valid_json(config));
    run_and_print(&mut results, test_openai_structure(config));
    run_and_print(&mut results, test_non_empty_content(config));
    run_and_print(&mut results, test_no_token_artifacts(config));
    run_and_print(&mut results, test_no_bpe_artifacts(config));
    run_and_print(&mut results, test_streaming_format(config));
    run_and_print(&mut results, test_stream_termination(config));
    run_and_print(&mut results, test_determinism(config));
    run_and_print(&mut results, test_malformed_json(config));
    run_and_print(&mut results, test_coherency(config));
    run_and_print(&mut results, test_no_multi_turn_loop(config));
    run_and_print(&mut results, test_trace_level(config, "brick", "P030", "Trace Brick Level"));
    run_and_print(&mut results, test_trace_level(config, "step", "P031", "Trace Step Level"));
    run_and_print(&mut results, test_trace_level(config, "layer", "P032", "Trace Layer Level"));
    run_and_print(&mut results, test_default_suppression(config));

    results
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let config = match parse_args(&args) {
        Some(c) => c,
        None => return,
    };

    print_header();

    let model = config.model_path.clone().or_else(find_default_model);
    let model = match model {
        Some(m) => m,
        None => {
            println!("{}ERROR: No model found.{}", RED, NC);
            std::process::exit(2);
        }
    };

    println!("{}Model:{} {}", CYAN, NC, model.display());
    println!("{}Port:{} {}", CYAN, NC, config.port);
    println!();

    // Start server
    println!("{}Starting server...{}", YELLOW, NC);
    let mut server = match start_server(&config, &model) {
        Some(s) => s,
        None => {
            println!("{}ERROR: Server failed to start{}", RED, NC);
            std::process::exit(1);
        }
    };

    println!("{}Server ready. Running tests...{}", GREEN, NC);
    println!();

    let start = Instant::now();

    println!(
        "{}=== Section C: qa_serve.rs Tests (35 Points) ==={}",
        YELLOW, NC
    );
    println!();

    let results = run_all_tests(&config);

    let elapsed = start.elapsed();
    println!();
    println!(
        "{}Tests completed in {:.1}s{}",
        CYAN,
        elapsed.as_secs_f64(),
        NC
    );

    print_summary(&results);

    // Stop server
    println!();
    println!("{}Stopping server...{}", YELLOW, NC);
    let _ = server.kill();
    let _ = server.wait();

    let failed = results.iter().filter(|r| !r.passed).count();
    std::process::exit(if failed == 0 { 0 } else { 1 });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_json_content() {
        let json = r#"{"choices":[{"message":{"content":"Hello world"}}]}"#;
        assert_eq!(extract_json_content(json), Some("Hello world".to_string()));
    }

    #[test]
    fn test_config_default() {
        let config = QaConfig::default();
        assert_eq!(config.port, 8080);
    }
}