graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
//! Benchmarking system for GraphRAG quality improvements
//!
//! This module provides comprehensive benchmarking tools to measure:
//! - Accuracy improvements from new features
//! - Token usage and cost reduction
//! - Latency and throughput
//! - Quality metrics (F1, Exact Match, BLEU)

use serde::{Deserialize, Serialize};
use std::time::Instant;

/// Benchmark results for a single query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryBenchmark {
    /// The query text
    pub query: String,

    /// Ground truth answer (if available)
    pub ground_truth: Option<String>,

    /// Generated answer
    pub generated_answer: String,

    /// Latency measurements
    pub latency: LatencyMetrics,

    /// Token usage
    pub tokens: TokenMetrics,

    /// Quality scores
    pub quality: QualityMetrics,

    /// Feature flags used
    pub features_enabled: Vec<String>,
}

/// Latency breakdown by pipeline stage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatencyMetrics {
    /// Total end-to-end latency
    pub total_ms: u64,

    /// Retrieval latency
    pub retrieval_ms: u64,

    /// Reranking latency (if enabled)
    pub reranking_ms: Option<u64>,

    /// Generation latency
    pub generation_ms: u64,

    /// Other processing time
    pub other_ms: u64,
}

/// Token usage tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenMetrics {
    /// Input tokens to LLM
    pub input_tokens: usize,

    /// Output tokens from LLM
    pub output_tokens: usize,

    /// Total tokens
    pub total_tokens: usize,

    /// Estimated cost (USD)
    pub estimated_cost_usd: f64,
}

/// Quality metrics for answer evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityMetrics {
    /// Exact match with ground truth (0.0 or 1.0)
    pub exact_match: f32,

    /// F1 score (token overlap)
    pub f1_score: f32,

    /// BLEU score (n-gram similarity)
    pub bleu_score: Option<f32>,

    /// ROUGE-L score (longest common subsequence)
    pub rouge_l: Option<f32>,

    /// Semantic similarity (if embeddings available)
    pub semantic_similarity: Option<f32>,
}

/// Dataset for benchmarking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkDataset {
    /// Dataset name (e.g., "HotpotQA", "MuSiQue")
    pub name: String,

    /// List of queries with ground truth
    pub queries: Vec<BenchmarkQuery>,
}

/// A single query with ground truth for evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkQuery {
    /// Question text
    pub question: String,

    /// Ground truth answer
    pub answer: String,

    /// Supporting documents (if applicable)
    pub context: Option<Vec<String>>,

    /// Query difficulty (easy, medium, hard)
    pub difficulty: Option<String>,

    /// Query type (factual, multi-hop, reasoning)
    pub query_type: Option<String>,
}

/// Configuration for benchmark runs
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Enable LightRAG dual-level retrieval
    pub enable_lightrag: bool,

    /// Enable Leiden community detection
    pub enable_leiden: bool,

    /// Enable cross-encoder reranking
    pub enable_cross_encoder: bool,

    /// Enable HippoRAG PPR
    pub enable_hipporag: bool,

    /// Enable semantic chunking
    pub enable_semantic_chunking: bool,

    /// Number of retrieval candidates
    pub top_k: usize,

    /// LLM pricing (USD per 1K tokens)
    pub input_token_price: f64,
    /// Output token pricing (USD per 1K tokens)
    pub output_token_price: f64,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            enable_lightrag: false,
            enable_leiden: false,
            enable_cross_encoder: false,
            enable_hipporag: false,
            enable_semantic_chunking: false,
            top_k: 10,
            input_token_price: 0.0001,  // Example: $0.10 per 1M tokens
            output_token_price: 0.0003, // Example: $0.30 per 1M tokens
        }
    }
}

/// Aggregate benchmark results across multiple queries
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkSummary {
    /// Configuration used
    pub config_name: String,

    /// Number of queries evaluated
    pub total_queries: usize,

    /// Average metrics
    pub avg_latency_ms: f64,
    /// Average retrieval latency in milliseconds
    pub avg_retrieval_ms: f64,
    /// Average reranking latency in milliseconds
    pub avg_reranking_ms: f64,
    /// Average generation latency in milliseconds
    pub avg_generation_ms: f64,

    /// Token statistics
    /// Total input tokens across all queries
    pub total_input_tokens: usize,
    /// Total output tokens across all queries
    pub total_output_tokens: usize,
    /// Total cost in USD
    pub total_cost_usd: f64,
    /// Average tokens per query
    pub avg_tokens_per_query: f64,

    /// Quality statistics
    /// Average exact match score
    pub avg_exact_match: f64,
    /// Average F1 score
    pub avg_f1_score: f64,
    /// Average BLEU score
    pub avg_bleu_score: f64,
    /// Average ROUGE-L score
    pub avg_rouge_l: f64,

    /// Features enabled
    pub features: Vec<String>,

    /// Per-query results
    pub query_results: Vec<QueryBenchmark>,
}

type RetrievalFn = Box<dyn Fn(&str) -> Vec<String> + Send + Sync>;
type RerankerFn = Box<dyn Fn(&[String]) -> Vec<String> + Send + Sync>;
type LlmFn = Box<dyn Fn(&str, &[String]) -> String + Send + Sync>;

/// Main benchmarking coordinator
pub struct BenchmarkRunner {
    config: BenchmarkConfig,
    /// Optional retrieval system for actual benchmarking
    retrieval_fn: Option<RetrievalFn>,
    /// Optional reranker function
    reranker_fn: Option<RerankerFn>,
    /// Optional LLM generation function
    llm_fn: Option<LlmFn>,
}

impl BenchmarkRunner {
    /// Create a new benchmark runner with simulation mode
    pub fn new(config: BenchmarkConfig) -> Self {
        Self {
            config,
            retrieval_fn: None,
            reranker_fn: None,
            llm_fn: None,
        }
    }

    /// Set a custom retrieval function for actual benchmarking
    ///
    /// # Example
    /// ```no_run
    /// # use graphrag_core::monitoring::benchmark::{BenchmarkRunner, BenchmarkConfig};
    /// let mut runner = BenchmarkRunner::new(BenchmarkConfig::default());
    /// runner.with_retrieval(|query| {
    ///     // Your retrieval implementation
    ///     vec!["doc1".to_string(), "doc2".to_string()]
    /// });
    /// ```
    pub fn with_retrieval<F>(&mut self, f: F) -> &mut Self
    where
        F: Fn(&str) -> Vec<String> + Send + Sync + 'static,
    {
        self.retrieval_fn = Some(Box::new(f));
        self
    }

    /// Set a custom reranker function
    ///
    /// # Example
    /// ```no_run
    /// # use graphrag_core::monitoring::benchmark::{BenchmarkRunner, BenchmarkConfig};
    /// let mut runner = BenchmarkRunner::new(BenchmarkConfig::default());
    /// runner.with_reranker(|docs| {
    ///     // Your reranking implementation
    ///     docs.to_vec()
    /// });
    /// ```
    pub fn with_reranker<F>(&mut self, f: F) -> &mut Self
    where
        F: Fn(&[String]) -> Vec<String> + Send + Sync + 'static,
    {
        self.reranker_fn = Some(Box::new(f));
        self
    }

    /// Set a custom LLM generation function
    ///
    /// # Example
    /// ```no_run
    /// # use graphrag_core::monitoring::benchmark::{BenchmarkRunner, BenchmarkConfig};
    /// let mut runner = BenchmarkRunner::new(BenchmarkConfig::default());
    /// runner.with_llm(|query, context| {
    ///     // Your LLM implementation
    ///     format!("Generated answer for: {}", query)
    /// });
    /// ```
    pub fn with_llm<F>(&mut self, f: F) -> &mut Self
    where
        F: Fn(&str, &[String]) -> String + Send + Sync + 'static,
    {
        self.llm_fn = Some(Box::new(f));
        self
    }

    /// Run benchmark on a dataset
    pub fn run_dataset(&mut self, dataset: &BenchmarkDataset) -> BenchmarkSummary {
        println!("📊 Running benchmark on dataset: {}", dataset.name);
        println!("📋 Queries: {}", dataset.queries.len());

        let mut results = Vec::new();

        for (i, query) in dataset.queries.iter().enumerate() {
            println!(
                "  [{}/{}] Processing: {}...",
                i + 1,
                dataset.queries.len(),
                &query.question.chars().take(50).collect::<String>()
            );

            let result = self.benchmark_query(query);
            results.push(result);
        }

        self.compute_summary(dataset.name.clone(), results)
    }

    /// Benchmark a single query
    fn benchmark_query(&self, query: &BenchmarkQuery) -> QueryBenchmark {
        let start = Instant::now();

        // Retrieval phase
        let retrieval_start = Instant::now();
        let retrieved_docs = if let Some(ref retrieval_fn) = self.retrieval_fn {
            // Call actual retrieval system
            retrieval_fn(&query.question)
        } else {
            // Simulation mode: return empty results
            vec![]
        };
        let retrieval_time = retrieval_start.elapsed();

        // Reranking phase (if enabled)
        let (reranked_docs, reranking_time) = if self.config.enable_cross_encoder {
            let reranking_start = Instant::now();
            let reranked = if let Some(ref reranker_fn) = self.reranker_fn {
                // Call actual cross-encoder reranking
                reranker_fn(&retrieved_docs)
            } else {
                // Simulation mode: no reranking
                retrieved_docs.clone()
            };
            (reranked, Some(reranking_start.elapsed()))
        } else {
            (retrieved_docs.clone(), None)
        };

        // Generation phase
        let generation_start = Instant::now();
        let generated_answer = if let Some(ref llm_fn) = self.llm_fn {
            // Call actual LLM generation with context
            llm_fn(&query.question, &reranked_docs)
        } else {
            // Simulation mode: generate placeholder
            format!("Generated answer for: {}", query.question)
        };
        let generation_time = generation_start.elapsed();

        let total_time = start.elapsed();

        // Calculate token usage (estimated)
        let estimated_input_tokens = if self.config.enable_lightrag {
            200 // LightRAG optimization: much lower
        } else {
            2000 // Traditional GraphRAG: ~10x more
        };

        let estimated_output_tokens = 100;

        let tokens = TokenMetrics {
            input_tokens: estimated_input_tokens,
            output_tokens: estimated_output_tokens,
            total_tokens: estimated_input_tokens + estimated_output_tokens,
            estimated_cost_usd: (estimated_input_tokens as f64 / 1000.0
                * self.config.input_token_price)
                + (estimated_output_tokens as f64 / 1000.0 * self.config.output_token_price),
        };

        // Calculate quality metrics
        let quality = self.calculate_quality_metrics(&generated_answer, &query.answer);

        // Collect enabled features
        let mut features = Vec::new();
        if self.config.enable_lightrag {
            features.push("LightRAG".to_string());
        }
        if self.config.enable_leiden {
            features.push("Leiden".to_string());
        }
        if self.config.enable_cross_encoder {
            features.push("Cross-Encoder".to_string());
        }
        if self.config.enable_hipporag {
            features.push("HippoRAG PPR".to_string());
        }
        if self.config.enable_semantic_chunking {
            features.push("Semantic Chunking".to_string());
        }

        QueryBenchmark {
            query: query.question.clone(),
            ground_truth: Some(query.answer.clone()),
            generated_answer,
            latency: LatencyMetrics {
                total_ms: total_time.as_millis() as u64,
                retrieval_ms: retrieval_time.as_millis() as u64,
                reranking_ms: reranking_time.map(|d| d.as_millis() as u64),
                generation_ms: generation_time.as_millis() as u64,
                other_ms: 0,
            },
            tokens,
            quality,
            features_enabled: features,
        }
    }

    /// Calculate quality metrics
    fn calculate_quality_metrics(&self, generated: &str, ground_truth: &str) -> QualityMetrics {
        // Exact match
        let exact_match = if generated.trim().eq_ignore_ascii_case(ground_truth.trim()) {
            1.0
        } else {
            0.0
        };

        // F1 score (token overlap)
        let f1_score = self.calculate_f1_score(generated, ground_truth);

        // BLEU score (n-gram overlap with brevity penalty)
        let bleu_score = Some(self.calculate_bleu_score(generated, ground_truth));

        // ROUGE-L score (Longest Common Subsequence F-score)
        let rouge_l = Some(self.calculate_rouge_l(generated, ground_truth));

        QualityMetrics {
            exact_match,
            f1_score,
            bleu_score,
            rouge_l,
            semantic_similarity: None,
        }
    }

    /// Calculate F1 score based on token overlap
    fn calculate_f1_score(&self, generated: &str, ground_truth: &str) -> f32 {
        let gen_tokens: Vec<String> = generated
            .to_lowercase()
            .split_whitespace()
            .map(|s| s.to_string())
            .collect();

        let gt_tokens: Vec<String> = ground_truth
            .to_lowercase()
            .split_whitespace()
            .map(|s| s.to_string())
            .collect();

        if gen_tokens.is_empty() || gt_tokens.is_empty() {
            return 0.0;
        }

        // Calculate overlap
        let mut common = 0;
        for token in &gen_tokens {
            if gt_tokens.contains(token) {
                common += 1;
            }
        }

        if common == 0 {
            return 0.0;
        }

        let precision = common as f32 / gen_tokens.len() as f32;
        let recall = common as f32 / gt_tokens.len() as f32;

        2.0 * (precision * recall) / (precision + recall)
    }

    /// Calculate BLEU score (BiLingual Evaluation Understudy)
    ///
    /// BLEU score measures n-gram overlap between generated and reference text,
    /// with a brevity penalty for overly short outputs.
    ///
    /// Formula: BLEU = BP * exp(1/N * sum(log(P_n)))
    /// where P_n is the precision for n-grams and BP is the brevity penalty.
    fn calculate_bleu_score(&self, candidate: &str, reference: &str) -> f32 {
        // Tokenize candidate and reference
        let candidate_tokens: Vec<&str> = candidate.split_whitespace().collect();
        let reference_tokens: Vec<&str> = reference.split_whitespace().collect();

        if candidate_tokens.is_empty() || reference_tokens.is_empty() {
            return 0.0;
        }

        // Calculate n-gram precisions (n=1 to 4)
        let max_n = 4;
        let mut log_precision_sum = 0.0;
        let mut valid_n_grams = 0;

        for n in 1..=max_n {
            let precision = self.calculate_ngram_precision(&candidate_tokens, &reference_tokens, n);

            if precision > 0.0 {
                log_precision_sum += precision.ln();
                valid_n_grams += 1;
            } else {
                // If any n-gram precision is 0, BLEU score is 0
                return 0.0;
            }
        }

        // Calculate brevity penalty
        let candidate_len = candidate_tokens.len() as f32;
        let reference_len = reference_tokens.len() as f32;

        let brevity_penalty = if candidate_len >= reference_len {
            1.0
        } else {
            (1.0 - reference_len / candidate_len).exp()
        };

        // Final BLEU score: BP * exp(1/N * sum(log(P_n)))
        let bleu = brevity_penalty * (log_precision_sum / valid_n_grams as f32).exp();

        // Clamp to [0, 1] range
        bleu.clamp(0.0, 1.0)
    }

    /// Calculate precision for n-grams with clipping
    fn calculate_ngram_precision(&self, candidate: &[&str], reference: &[&str], n: usize) -> f32 {
        if candidate.len() < n || reference.len() < n {
            return 0.0;
        }

        // Extract n-grams from candidate
        let candidate_ngrams = self.extract_ngrams(candidate, n);

        // Extract n-grams from reference and count frequencies
        let reference_ngrams = self.extract_ngrams(reference, n);
        let mut reference_counts = std::collections::HashMap::new();
        for ngram in &reference_ngrams {
            *reference_counts.entry(ngram).or_insert(0) += 1;
        }

        // Count clipped matches (clip to max count in reference)
        let mut clipped_matches = 0;
        let mut candidate_counts = std::collections::HashMap::new();

        for ngram in &candidate_ngrams {
            let candidate_count = candidate_counts.entry(ngram).or_insert(0);
            *candidate_count += 1;

            if let Some(&ref_count) = reference_counts.get(&ngram) {
                if *candidate_count <= ref_count {
                    clipped_matches += 1;
                }
            }
        }

        // Precision = clipped_matches / total_candidate_ngrams
        if candidate_ngrams.is_empty() {
            0.0
        } else {
            clipped_matches as f32 / candidate_ngrams.len() as f32
        }
    }

    /// Extract all n-grams from a token sequence
    fn extract_ngrams(&self, tokens: &[&str], n: usize) -> Vec<Vec<String>> {
        if tokens.len() < n {
            return Vec::new();
        }

        tokens
            .windows(n)
            .map(|window| window.iter().map(|&s| s.to_string()).collect())
            .collect()
    }

    /// Calculate ROUGE-L score (Recall-Oriented Understudy for Gisting Evaluation - Longest Common Subsequence)
    ///
    /// ROUGE-L measures the similarity between candidate and reference text using
    /// the Longest Common Subsequence (LCS) to compute precision, recall, and F-score.
    ///
    /// Formula: F = ((1 + β²) * precision * recall) / (β² * precision + recall)
    /// where β controls the importance of recall (typically β=1.2)
    fn calculate_rouge_l(&self, candidate: &str, reference: &str) -> f32 {
        // Tokenize candidate and reference
        let candidate_tokens: Vec<&str> = candidate.split_whitespace().collect();
        let reference_tokens: Vec<&str> = reference.split_whitespace().collect();

        if candidate_tokens.is_empty() || reference_tokens.is_empty() {
            return 0.0;
        }

        // Calculate LCS length
        let lcs_length = self.lcs_length(&candidate_tokens, &reference_tokens);

        if lcs_length == 0 {
            return 0.0;
        }

        // Calculate precision and recall
        let precision = lcs_length as f32 / candidate_tokens.len() as f32;
        let recall = lcs_length as f32 / reference_tokens.len() as f32;

        // Calculate F-score with β=1.2 (slightly favors recall)
        let beta = 1.2;
        let beta_squared = beta * beta;

        let f_score =
            ((1.0 + beta_squared) * precision * recall) / (beta_squared * precision + recall);

        // Clamp to [0, 1] range
        f_score.clamp(0.0, 1.0)
    }

    /// Calculate the length of the Longest Common Subsequence (LCS) using dynamic programming
    ///
    /// LCS is the longest sequence of tokens that appear in both texts in the same order
    /// (but not necessarily consecutively).
    ///
    /// Time complexity: O(m * n) where m and n are the lengths of the two sequences
    fn lcs_length(&self, seq1: &[&str], seq2: &[&str]) -> usize {
        let m = seq1.len();
        let n = seq2.len();

        if m == 0 || n == 0 {
            return 0;
        }

        // Create DP table: dp[i][j] = LCS length of seq1[0..i] and seq2[0..j]
        let mut dp = vec![vec![0; n + 1]; m + 1];

        // Fill the DP table
        for i in 1..=m {
            for j in 1..=n {
                if seq1[i - 1] == seq2[j - 1] {
                    // Characters match: extend LCS by 1
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    // Characters don't match: take max of excluding either character
                    dp[i][j] = dp[i - 1][j].max(dp[i][j - 1]);
                }
            }
        }

        dp[m][n]
    }

    /// Compute aggregate summary
    fn compute_summary(
        &self,
        config_name: String,
        results: Vec<QueryBenchmark>,
    ) -> BenchmarkSummary {
        let total = results.len();

        if total == 0 {
            return BenchmarkSummary {
                config_name,
                total_queries: 0,
                avg_latency_ms: 0.0,
                avg_retrieval_ms: 0.0,
                avg_reranking_ms: 0.0,
                avg_generation_ms: 0.0,
                total_input_tokens: 0,
                total_output_tokens: 0,
                total_cost_usd: 0.0,
                avg_tokens_per_query: 0.0,
                avg_exact_match: 0.0,
                avg_f1_score: 0.0,
                avg_bleu_score: 0.0,
                avg_rouge_l: 0.0,
                features: Vec::new(),
                query_results: results,
            };
        }

        let avg_latency_ms = results
            .iter()
            .map(|r| r.latency.total_ms as f64)
            .sum::<f64>()
            / total as f64;
        let avg_retrieval_ms = results
            .iter()
            .map(|r| r.latency.retrieval_ms as f64)
            .sum::<f64>()
            / total as f64;
        let avg_reranking_ms = results
            .iter()
            .filter_map(|r| r.latency.reranking_ms)
            .map(|ms| ms as f64)
            .sum::<f64>()
            / total as f64;
        let avg_generation_ms = results
            .iter()
            .map(|r| r.latency.generation_ms as f64)
            .sum::<f64>()
            / total as f64;

        let total_input_tokens: usize = results.iter().map(|r| r.tokens.input_tokens).sum();
        let total_output_tokens: usize = results.iter().map(|r| r.tokens.output_tokens).sum();
        let total_cost_usd: f64 = results.iter().map(|r| r.tokens.estimated_cost_usd).sum();

        let avg_exact_match = results
            .iter()
            .map(|r| r.quality.exact_match as f64)
            .sum::<f64>()
            / total as f64;
        let avg_f1_score = results
            .iter()
            .map(|r| r.quality.f1_score as f64)
            .sum::<f64>()
            / total as f64;

        // Calculate average BLEU score (only count queries where BLEU was computed)
        let bleu_scores: Vec<f64> = results
            .iter()
            .filter_map(|r| r.quality.bleu_score.map(|s| s as f64))
            .collect();
        let avg_bleu_score = if !bleu_scores.is_empty() {
            bleu_scores.iter().sum::<f64>() / bleu_scores.len() as f64
        } else {
            0.0
        };

        // Calculate average ROUGE-L score (only count queries where ROUGE-L was computed)
        let rouge_scores: Vec<f64> = results
            .iter()
            .filter_map(|r| r.quality.rouge_l.map(|s| s as f64))
            .collect();
        let avg_rouge_l = if !rouge_scores.is_empty() {
            rouge_scores.iter().sum::<f64>() / rouge_scores.len() as f64
        } else {
            0.0
        };

        let features = if !results.is_empty() {
            results[0].features_enabled.clone()
        } else {
            Vec::new()
        };

        BenchmarkSummary {
            config_name,
            total_queries: total,
            avg_latency_ms,
            avg_retrieval_ms,
            avg_reranking_ms,
            avg_generation_ms,
            total_input_tokens,
            total_output_tokens,
            total_cost_usd,
            avg_tokens_per_query: (total_input_tokens + total_output_tokens) as f64 / total as f64,
            avg_exact_match,
            avg_f1_score,
            avg_bleu_score,
            avg_rouge_l,
            features,
            query_results: results,
        }
    }

    /// Print summary results
    pub fn print_summary(&self, summary: &BenchmarkSummary) {
        println!("\n📊 Benchmark Results: {}", summary.config_name);
        println!("{}", "=".repeat(60));

        println!("\n🎯 Quality Metrics:");
        println!("  Exact Match:  {:.1}%", summary.avg_exact_match * 100.0);
        println!("  F1 Score:     {:.3}", summary.avg_f1_score);
        if summary.avg_bleu_score > 0.0 {
            println!("  BLEU Score:   {:.3}", summary.avg_bleu_score);
        }
        if summary.avg_rouge_l > 0.0 {
            println!("  ROUGE-L:      {:.3}", summary.avg_rouge_l);
        }

        println!("\n⏱️  Latency Metrics (avg):");
        println!("  Total:        {:.1} ms", summary.avg_latency_ms);
        println!("  Retrieval:    {:.1} ms", summary.avg_retrieval_ms);
        if summary.avg_reranking_ms > 0.0 {
            println!("  Reranking:    {:.1} ms", summary.avg_reranking_ms);
        }
        println!("  Generation:   {:.1} ms", summary.avg_generation_ms);

        println!("\n💰 Token & Cost Metrics:");
        println!("  Input tokens:  {}", summary.total_input_tokens);
        println!("  Output tokens: {}", summary.total_output_tokens);
        println!("  Total cost:    ${:.4}", summary.total_cost_usd);
        println!("  Avg tokens/query: {:.0}", summary.avg_tokens_per_query);

        println!("\n✨ Features Enabled:");
        for feature in &summary.features {
            println!("{}", feature);
        }

        println!("\n{}", "=".repeat(60));
    }

    /// Compare two benchmark summaries
    pub fn compare_summaries(&self, baseline: &BenchmarkSummary, improved: &BenchmarkSummary) {
        println!("\n📈 Benchmark Comparison");
        println!("{}", "=".repeat(60));

        println!("\nConfiguration:");
        println!("  Baseline: {}", baseline.config_name);
        println!("  Improved: {}", improved.config_name);

        println!("\n🎯 Quality Improvements:");
        let em_improvement = ((improved.avg_exact_match - baseline.avg_exact_match)
            / baseline.avg_exact_match)
            * 100.0;
        let f1_improvement =
            ((improved.avg_f1_score - baseline.avg_f1_score) / baseline.avg_f1_score) * 100.0;
        println!("  Exact Match:  {:+.1}%", em_improvement);
        println!("  F1 Score:     {:+.1}%", f1_improvement);

        println!("\n💰 Cost Savings:");
        let token_reduction = ((baseline.total_input_tokens - improved.total_input_tokens) as f64
            / baseline.total_input_tokens as f64)
            * 100.0;
        let cost_savings =
            ((baseline.total_cost_usd - improved.total_cost_usd) / baseline.total_cost_usd) * 100.0;
        println!(
            "  Token reduction: {:.1}% ({}{} tokens)",
            token_reduction, baseline.total_input_tokens, improved.total_input_tokens
        );
        println!(
            "  Cost savings:    {:.1}% (${:.4} → ${:.4})",
            cost_savings, baseline.total_cost_usd, improved.total_cost_usd
        );

        println!("\n⏱️  Latency Changes:");
        let latency_change =
            ((improved.avg_latency_ms - baseline.avg_latency_ms) / baseline.avg_latency_ms) * 100.0;
        println!(
            "  Total latency: {:+.1}% ({:.1}ms → {:.1}ms)",
            latency_change, baseline.avg_latency_ms, improved.avg_latency_ms
        );

        println!("\n{}", "=".repeat(60));
    }
}

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

    #[test]
    fn test_f1_score_calculation() {
        let _runner = BenchmarkRunner::new(BenchmarkConfig::default());

        // Perfect match
        let f1 = _runner.calculate_f1_score("hello world", "hello world");
        assert!((f1 - 1.0).abs() < 0.001);

        // Partial overlap
        let f1 = _runner.calculate_f1_score("hello world", "hello there");
        assert!(f1 > 0.0 && f1 < 1.0);

        // No overlap
        let f1 = _runner.calculate_f1_score("foo bar", "baz qux");
        assert_eq!(f1, 0.0);
    }

    #[test]
    fn test_benchmark_summary() {
        let dataset = BenchmarkDataset {
            name: "Test".to_string(),
            queries: vec![BenchmarkQuery {
                question: "What is 2+2?".to_string(),
                answer: "4".to_string(),
                context: None,
                difficulty: None,
                query_type: None,
            }],
        };

        let mut runner = BenchmarkRunner::new(BenchmarkConfig::default());
        let summary = runner.run_dataset(&dataset);

        assert_eq!(summary.total_queries, 1);
        assert!(summary.avg_latency_ms >= 0.0);
    }
}