llm-optimizer-decision 0.1.1

Intelligent decision-making engine
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
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
459
460
461
462
463
464
465
466
467
468
//! Integration tests for model registry with all decision systems
//!
//! This test file demonstrates the complete integration of all LLM models
//! with Pareto optimization, A/B testing, and reinforcement learning.

use decision::{
    ModelRegistry, ObjectiveWeights, ParetoFrontier, Provider, ModelTier,
};

#[test]
fn test_all_requested_models_in_pareto_optimization() {
    let registry = ModelRegistry::new();

    // All requested models from the specification
    let requested_models = vec![
        "gpt-5",
        "gpt-4.5",
        "gpt-4.1",
        "gemini-2.5-pro",
        "claude-4-opus",
        "claude-sonnet-4.5",
        "llama-4-scout",
        "llama-4-maverick",
        "gemma-3",
        "mistral-large-2",
        "mixtral-8x22b",
        "qwen-3",
        "grok-4",
    ];

    // Verify all models exist
    for model_id in &requested_models {
        assert!(
            registry.get(model_id).is_some(),
            "Model {} not found in registry",
            model_id
        );
    }

    // Run Pareto optimization across all models
    let pareto_set = registry
        .find_pareto_optimal(
            &requested_models.iter().map(|s| s.to_string()).collect::<Vec<_>>(),
            1000,
            500,
        )
        .unwrap();

    println!("Pareto optimal set has {} models:", pareto_set.len());
    for candidate in &pareto_set {
        println!(
            "  - {}: quality={:.2}, cost=${:.4}, latency={:.0}ms",
            candidate.name,
            candidate.objectives.quality,
            candidate.objectives.cost,
            candidate.objectives.latency_p95
        );
    }

    assert!(!pareto_set.is_empty());
    assert!(pareto_set.len() <= requested_models.len());
}

#[test]
fn test_provider_diversity_in_pareto_set() {
    let registry = ModelRegistry::new();

    // Get flagship models from each provider
    let flagship_models: Vec<String> = registry
        .models_by_tier(ModelTier::Flagship)
        .iter()
        .map(|m| m.id.clone())
        .collect();

    assert!(
        flagship_models.len() >= 4,
        "Should have flagship models from multiple providers"
    );

    let pareto_set = registry
        .find_pareto_optimal(&flagship_models, 2000, 1000)
        .unwrap();

    // Flagship models often have different trade-offs, so multiple should be Pareto optimal
    assert!(!pareto_set.is_empty());
}

#[test]
fn test_quality_focused_selection() {
    let registry = ModelRegistry::new();

    let all_models: Vec<String> = vec![
        "gpt-5",
        "gpt-4.5",
        "claude-4-opus",
        "claude-sonnet-4.5",
        "gemini-2.5-pro",
        "grok-4",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let pareto_set = registry.find_pareto_optimal(&all_models, 1000, 500).unwrap();

    // Select with quality-focused weights
    let weights = ObjectiveWeights::quality_focused(1.0, 5000.0);
    let best = ParetoFrontier::select_optimal(&pareto_set, &weights).unwrap();

    println!("Quality-focused selection: {}", best.name);

    // Should select a high-quality model
    assert!(best.objectives.quality >= 0.92);
}

#[test]
fn test_cost_focused_selection() {
    let registry = ModelRegistry::new();

    let all_models: Vec<String> = vec![
        "claude-3-haiku",
        "gpt-3.5-turbo",
        "gemini-2.0-flash",
        "mistral-nemo",
        "llama-3.3-70b",
        "mixtral-8x22b",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let pareto_set = registry.find_pareto_optimal(&all_models, 1000, 500).unwrap();

    // Select with cost-focused weights
    let weights = ObjectiveWeights::cost_focused(1.0, 5000.0);
    let best = ParetoFrontier::select_optimal(&pareto_set, &weights).unwrap();

    println!("Cost-focused selection: {}", best.name);

    // Should select a low-cost model
    assert!(best.objectives.cost < 0.01);
}

#[test]
fn test_latency_focused_selection() {
    let registry = ModelRegistry::new();

    let all_models: Vec<String> = vec![
        "claude-3-haiku",
        "gpt-3.5-turbo",
        "gemini-2.0-flash",
        "claude-sonnet-4.5",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let pareto_set = registry.find_pareto_optimal(&all_models, 1000, 500).unwrap();

    // Select with latency-focused weights
    let weights = ObjectiveWeights::latency_focused(1.0, 5000.0);
    let best = ParetoFrontier::select_optimal(&pareto_set, &weights).unwrap();

    println!("Latency-focused selection: {}", best.name);

    // Should select a fast model
    assert!(best.objectives.latency_p95 < 1500.0);
}

#[test]
fn test_open_source_vs_proprietary() {
    let registry = ModelRegistry::new();

    // Open source models (free)
    let open_source: Vec<String> = vec![
        "llama-4-scout",
        "llama-3.3-70b",
        "mixtral-8x22b",
        "qwen-3",
        "gemma-3",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    // Proprietary models
    let proprietary: Vec<String> = vec![
        "gpt-5",
        "claude-4-opus",
        "gemini-2.5-pro",
        "grok-4",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let open_pareto = registry.find_pareto_optimal(&open_source, 1000, 500).unwrap();
    let prop_pareto = registry
        .find_pareto_optimal(&proprietary, 1000, 500)
        .unwrap();

    println!("Open source Pareto set: {} models", open_pareto.len());
    println!("Proprietary Pareto set: {} models", prop_pareto.len());

    // Open source models should have cost advantage
    let open_avg_cost: f64 = open_pareto.iter().map(|m| m.objectives.cost).sum::<f64>()
        / open_pareto.len() as f64;
    let prop_avg_cost: f64 = prop_pareto.iter().map(|m| m.objectives.cost).sum::<f64>()
        / prop_pareto.len() as f64;

    println!("Open source avg cost: ${:.4}", open_avg_cost);
    println!("Proprietary avg cost: ${:.4}", prop_avg_cost);

    assert!(open_avg_cost < prop_avg_cost);
}

#[test]
fn test_provider_comparison() {
    let registry = ModelRegistry::new();

    for provider in Provider::all() {
        let models = registry.models_by_provider(provider);
        println!("{}: {} models", provider.name(), models.len());

        assert!(
            !models.is_empty(),
            "Provider {} has no models",
            provider.name()
        );

        // Verify models have valid characteristics
        for model in models {
            assert!(model.quality_score >= 0.0 && model.quality_score <= 1.0);
            assert!(model.performance.latency_p95_ms > 0.0);
            assert!(model.performance.max_context_tokens > 0);
        }
    }
}

#[test]
fn test_real_world_scenario_blog_generation() {
    let registry = ModelRegistry::new();

    // Scenario: Blog post generation (1000 input, 2000 output)
    let input_tokens = 1000;
    let output_tokens = 2000;

    let candidates: Vec<String> = vec![
        "gpt-4-turbo",
        "claude-3.5-sonnet",
        "claude-sonnet-4.5",
        "gemini-1.5-pro",
        "mistral-large-2",
        "llama-3.3-70b",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let pareto_set = registry
        .find_pareto_optimal(&candidates, input_tokens, output_tokens)
        .unwrap();

    println!("\nBlog Generation - Pareto Optimal Models:");
    for candidate in &pareto_set {
        let model = registry.get(&candidate.name).unwrap();
        println!(
            "  {} ({}): quality={:.2}, cost=${:.4}, latency={:.0}ms",
            candidate.name,
            model.provider.name(),
            candidate.objectives.quality,
            candidate.objectives.cost,
            candidate.objectives.latency_p95
        );
    }

    // Balanced selection
    let weights = ObjectiveWeights::balanced(1.0, 5000.0);
    let best = ParetoFrontier::select_optimal(&pareto_set, &weights).unwrap();

    println!("\nBalanced choice: {}", best.name);
    assert!(!best.name.is_empty());
}

#[test]
fn test_real_world_scenario_code_generation() {
    let registry = ModelRegistry::new();

    // Scenario: Code generation (2000 input, 1000 output) - needs high quality
    let input_tokens = 2000;
    let output_tokens = 1000;

    let candidates: Vec<String> = vec![
        "gpt-4.5",
        "claude-sonnet-4.5",
        "gemini-2.0-flash",
        "qwen-2.5-72b",
        "deepseek-v3",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let pareto_set = registry
        .find_pareto_optimal(&candidates, input_tokens, output_tokens)
        .unwrap();

    println!("\nCode Generation - Pareto Optimal Models:");
    for candidate in &pareto_set {
        println!(
            "  {}: quality={:.2}, cost=${:.4}, latency={:.0}ms",
            candidate.name,
            candidate.objectives.quality,
            candidate.objectives.cost,
            candidate.objectives.latency_p95
        );
    }

    // Quality-focused selection for code
    let weights = ObjectiveWeights::quality_focused(1.0, 5000.0);
    let best = ParetoFrontier::select_optimal(&pareto_set, &weights).unwrap();

    println!("Quality-focused choice: {}", best.name);

    // Should select a high-quality model
    assert!(best.objectives.quality >= 0.85);
}

#[test]
fn test_real_world_scenario_chatbot() {
    let registry = ModelRegistry::new();

    // Scenario: Chatbot (500 input, 200 output) - needs low latency
    let input_tokens = 500;
    let output_tokens = 200;

    let candidates: Vec<String> = vec![
        "claude-3-haiku",
        "gpt-3.5-turbo",
        "gemini-2.0-flash",
        "mistral-nemo",
        "llama-4-maverick",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();

    let pareto_set = registry
        .find_pareto_optimal(&candidates, input_tokens, output_tokens)
        .unwrap();

    println!("\nChatbot - Pareto Optimal Models:");
    for candidate in &pareto_set {
        println!(
            "  {}: quality={:.2}, cost=${:.4}, latency={:.0}ms",
            candidate.name,
            candidate.objectives.quality,
            candidate.objectives.cost,
            candidate.objectives.latency_p95
        );
    }

    // Latency-focused selection for chatbot
    let weights = ObjectiveWeights::latency_focused(1.0, 5000.0);
    let best = ParetoFrontier::select_optimal(&pareto_set, &weights).unwrap();

    println!("Latency-focused choice: {}", best.name);

    // Should select a fast model
    assert!(best.objectives.latency_p95 < 2000.0);
}

#[test]
fn test_future_models_availability() {
    let registry = ModelRegistry::new();

    // Current models should be available
    assert!(registry.get("gpt-4-turbo").unwrap().available);
    assert!(registry.get("claude-3.5-sonnet").unwrap().available);
    assert!(registry.get("gemini-1.5-pro").unwrap().available);

    // Future models should not be available yet
    assert!(!registry.get("gpt-5").unwrap().available);
    assert!(!registry.get("claude-4-opus").unwrap().available);
    assert!(!registry.get("gemini-2.5-pro").unwrap().available);
    assert!(!registry.get("llama-4-scout").unwrap().available);
    assert!(!registry.get("grok-4").unwrap().available);
}

#[test]
fn test_cost_calculation_accuracy() {
    let registry = ModelRegistry::new();

    // Test GPT-4 Turbo pricing
    let gpt4 = registry.get("gpt-4-turbo").unwrap();
    let cost = gpt4.pricing.calculate_cost(1000, 1000);
    // $0.01 per 1k input + $0.03 per 1k output = $0.04
    assert!((cost - 0.04).abs() < 0.0001);

    // Test Claude Haiku (very cheap)
    let haiku = registry.get("claude-3-haiku").unwrap();
    let cost = haiku.pricing.calculate_cost(1000, 1000);
    // $0.00025 per 1k input + $0.00125 per 1k output = $0.0015
    assert!((cost - 0.0015).abs() < 0.0001);

    // Test open source (free)
    let llama = registry.get("llama-3.3-70b").unwrap();
    let cost = llama.pricing.calculate_cost(1000, 1000);
    assert_eq!(cost, 0.0);
}

#[test]
fn test_performance_tiers() {
    let registry = ModelRegistry::new();

    // Flagship models should have highest quality
    let flagship = registry.models_by_tier(ModelTier::Flagship);
    for model in flagship {
        assert!(
            model.quality_score >= 0.92,
            "{} quality {} too low for flagship",
            model.name,
            model.quality_score
        );
    }

    // Efficient models should have lower latency
    let efficient = registry.models_by_tier(ModelTier::Efficient);
    for model in efficient {
        assert!(
            model.performance.latency_p95_ms < 1500.0,
            "{} latency {} too high for efficient tier",
            model.name,
            model.performance.latency_p95_ms
        );
    }
}

#[test]
fn test_comprehensive_pareto_analysis() {
    let registry = ModelRegistry::new();

    // Get ALL models
    let all_model_ids: Vec<String> = registry
        .all_models()
        .iter()
        .map(|m| m.id.clone())
        .collect();

    println!("Total models in registry: {}", all_model_ids.len());

    let pareto_set = registry.find_pareto_optimal(&all_model_ids, 1000, 500).unwrap();

    println!("Pareto optimal models (out of {}):", all_model_ids.len());
    for candidate in &pareto_set {
        println!(
            "  {}: Q={:.2}, C=${:.4}, L={:.0}ms",
            candidate.name,
            candidate.objectives.quality,
            candidate.objectives.cost,
            candidate.objectives.latency_p95
        );
    }

    // Pareto set should be significantly smaller than total
    assert!(pareto_set.len() < all_model_ids.len());
    assert!(pareto_set.len() >= 5); // Should have at least several Pareto optimal models
}