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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
//! Answer generation from retrieval results.
//!
//! Synthesises natural-language answers by feeding retrieved chunks and graph context
//! to a [`LanguageModel`], over the `GenerationParams` knobs.

use crate::{
    core::traits::{GenerationParams, LanguageModel, ModelInfo},
    retrieval::{ResultType, SearchResult},
    summarization::QueryResult,
    text::TextProcessor,
    GraphRAGError, Result,
};
use std::collections::{HashMap, HashSet};

// Async implementation module
pub mod async_mock_llm;

/// Mock LLM interface for testing without external dependencies
pub trait LLMInterface: Send + Sync {
    /// Generate a response based on the given prompt
    fn generate_response(&self, prompt: &str) -> Result<String>;
    /// Generate a summary of the content with a maximum length
    fn generate_summary(&self, content: &str, max_length: usize) -> Result<String>;
    /// Extract key points from the content, returning the specified number of points
    fn extract_key_points(&self, content: &str, num_points: usize) -> Result<Vec<String>>;
}

/// Simple mock LLM implementation for testing
pub struct MockLLM {
    response_templates: HashMap<String, String>,
    text_processor: TextProcessor,
}

impl MockLLM {
    /// Create a new MockLLM with default response templates
    pub fn new() -> Result<Self> {
        let mut templates = HashMap::new();

        // Default response templates
        templates.insert(
            "default".to_string(),
            "Based on the provided context, here is what I found: {context}".to_string(),
        );
        templates.insert(
            "not_found".to_string(),
            "I could not find specific information about this in the provided context.".to_string(),
        );
        templates.insert(
            "insufficient_context".to_string(),
            "The available context is insufficient to provide a complete answer.".to_string(),
        );

        let text_processor = TextProcessor::new(1000, 100)?;

        Ok(Self {
            response_templates: templates,
            text_processor,
        })
    }

    /// Create a new MockLLM with custom response templates
    pub fn with_templates(templates: HashMap<String, String>) -> Result<Self> {
        let text_processor = TextProcessor::new(1000, 100)?;

        Ok(Self {
            response_templates: templates,
            text_processor,
        })
    }

    /// Generate extractive answer from context with improved relevance scoring
    fn generate_extractive_answer(&self, context: &str, query: &str) -> Result<String> {
        let sentences = self.text_processor.extract_sentences(context);
        if sentences.is_empty() {
            return Ok("No relevant context found.".to_string());
        }

        // Enhanced scoring with partial word matching and named entity recognition
        let query_lower = query.to_lowercase();
        let query_words: Vec<&str> = query_lower
            .split_whitespace()
            .filter(|w| w.len() > 2) // Filter out short words
            .collect();

        if query_words.is_empty() {
            return Ok("Query too short or contains no meaningful words.".to_string());
        }

        let mut sentence_scores: Vec<(usize, f32)> = sentences
            .iter()
            .enumerate()
            .map(|(i, sentence)| {
                let sentence_lower = sentence.to_lowercase();
                let mut total_score = 0.0;
                let mut matches = 0;

                for word in &query_words {
                    // Exact word match (highest score)
                    if sentence_lower.contains(word) {
                        total_score += 2.0;
                        matches += 1;
                    }
                    // Partial match for longer words
                    else if word.len() > 4 {
                        for sentence_word in sentence_lower.split_whitespace() {
                            if sentence_word.contains(word) || word.contains(sentence_word) {
                                total_score += 1.0;
                                matches += 1;
                                break;
                            }
                        }
                    } else {
                        // Short words (4 chars or less) with no exact match are skipped
                    }
                }

                // Boost score for sentences with multiple matches
                let coverage_bonus = (matches as f32 / query_words.len() as f32) * 0.5;
                let final_score = total_score + coverage_bonus;

                (i, final_score)
            })
            .collect();

        // Sort by relevance
        sentence_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Select top sentences with a minimum relevance threshold
        let mut answer_sentences = Vec::new();
        for (idx, score) in sentence_scores.iter().take(5) {
            if *score > 0.5 {
                // Higher threshold for better quality
                answer_sentences.push(format!(
                    "{} (relevance: {:.1})",
                    sentences[*idx].trim(),
                    score
                ));
            }
        }

        if answer_sentences.is_empty() {
            // If no high-quality matches, provide the best available with lower threshold
            for (idx, score) in sentence_scores.iter().take(2) {
                if *score > 0.0 {
                    answer_sentences.push(format!(
                        "{} (low confidence: {:.1})",
                        sentences[*idx].trim(),
                        score
                    ));
                }
            }
        }

        if answer_sentences.is_empty() {
            Ok("No directly relevant information found in the context.".to_string())
        } else {
            Ok(answer_sentences.join("\n\n"))
        }
    }

    /// Generate smart contextual answer
    fn generate_smart_answer(&self, context: &str, question: &str) -> Result<String> {
        // First try extractive approach
        let extractive_result = self.generate_extractive_answer(context, question)?;

        // If extractive failed, generate a contextual response
        if extractive_result.contains("No relevant") || extractive_result.contains("No directly") {
            return self.generate_contextual_response(context, question);
        }

        Ok(extractive_result)
    }

    /// Generate contextual response when direct extraction fails
    fn generate_contextual_response(&self, context: &str, question: &str) -> Result<String> {
        let question_lower = question.to_lowercase();
        let context_lower = context.to_lowercase();

        // Pattern matching for common question types
        if question_lower.contains("who") && question_lower.contains("friend") {
            // Look for character names and relationships
            let names = self.extract_character_names(&context_lower);
            if !names.is_empty() {
                return Ok(format!("Based on the context, the main characters mentioned include: {}. These appear to be friends and companions in the story.", names.join(", ")));
            }
        }

        if question_lower.contains("what")
            && (question_lower.contains("adventure") || question_lower.contains("happen"))
        {
            let events = self.extract_key_events(&context_lower);
            if !events.is_empty() {
                return Ok(format!(
                    "The context describes several events: {}",
                    events.join(", ")
                ));
            }
        }

        if question_lower.contains("where") {
            let locations = self.extract_locations(&context_lower);
            if !locations.is_empty() {
                return Ok(format!(
                    "The story takes place in locations such as: {}",
                    locations.join(", ")
                ));
            }
        }

        // Fallback: provide a summary of the context
        let summary = self.generate_summary(context, 150)?;
        Ok(format!("Based on the available context: {summary}"))
    }

    /// Generate response for direct questions
    fn generate_question_response(&self, question: &str) -> Result<String> {
        let question_lower = question.to_lowercase();

        if question_lower.contains("entity") && question_lower.contains("friend") {
            return Ok("Entity Name's main friends include Second Entity, Friend Entity, and Companion Entity. These characters share many relationships throughout the story.".to_string());
        }

        if question_lower.contains("guardian") {
            return Ok("Guardian Entity is Entity Name's guardian who raised them. They are known for their caring but strict nature.".to_string());
        }

        if question_lower.contains("activity") && question_lower.contains("main") {
            return Ok("The main activity episode is one of the most famous events, where they cleverly convince other characters to participate in the main activity.".to_string());
        }

        Ok(
            "I need more specific context to provide a detailed answer to this question."
                .to_string(),
        )
    }

    /// Extract character names from text
    fn extract_character_names(&self, text: &str) -> Vec<String> {
        let common_names = [
            "entity",
            "second",
            "third",
            "fourth",
            "fifth",
            "sixth",
            "guardian",
            "companion",
            "friend",
            "character",
        ];
        let mut found_names = Vec::new();

        for name in &common_names {
            if text.contains(name) {
                found_names.push(name.to_string());
            }
        }

        found_names
    }

    /// Extract key events/actions from text
    fn extract_key_events(&self, text: &str) -> Vec<String> {
        let event_keywords = [
            "activity",
            "discovery",
            "location",
            "place",
            "action",
            "building",
            "structure",
            "area",
            "water",
        ];
        let mut found_events = Vec::new();

        for event in &event_keywords {
            if text.contains(event) {
                found_events.push(format!("events involving {event}"));
            }
        }

        found_events
    }

    /// Extract locations from text
    fn extract_locations(&self, text: &str) -> Vec<String> {
        let locations = [
            "settlement",
            "waterway",
            "river",
            "cavern",
            "landmass",
            "town",
            "building",
            "institution",
            "dwelling",
        ];
        let mut found_locations = Vec::new();

        for location in &locations {
            if text.contains(location) {
                found_locations.push(location.to_string());
            }
        }

        found_locations
    }
}

impl Default for MockLLM {
    fn default() -> Self {
        Self::new().expect("MockLLM default construction infallible")
    }
}

impl LLMInterface for MockLLM {
    fn generate_response(&self, prompt: &str) -> Result<String> {
        // Debug: Log the prompt to understand what's being sent (uncomment for debugging)
        // println!("DEBUG MockLLM received prompt: {}", &prompt[..prompt.len().min(200)]);

        // Enhanced pattern matching for more intelligent mock responses
        let prompt_lower = prompt.to_lowercase();

        // Handle Q&A format prompts
        if prompt_lower.contains("context:") && prompt_lower.contains("question:") {
            if let Some(context_start) = prompt.find("Context:") {
                let context_section = &prompt[context_start + 8..];
                if let Some(question_start) = context_section.find("Question:") {
                    let context = context_section[..question_start].trim();
                    let question_section = context_section[question_start + 9..].trim();

                    return self.generate_smart_answer(context, question_section);
                }
            }
        }

        // Handle direct questions about specific topics
        if prompt_lower.contains("who")
            || prompt_lower.contains("what")
            || prompt_lower.contains("where")
            || prompt_lower.contains("when")
            || prompt_lower.contains("how")
            || prompt_lower.contains("why")
        {
            return self.generate_question_response(prompt);
        }

        // Fallback to template
        Ok(self
            .response_templates
            .get("default")
            .unwrap_or(&"I cannot provide a response based on the given prompt.".to_string())
            .replace("{context}", &prompt[..prompt.len().min(200)]))
    }

    fn generate_summary(&self, content: &str, max_length: usize) -> Result<String> {
        let sentences = self.text_processor.extract_sentences(content);
        if sentences.is_empty() {
            return Ok(String::new());
        }

        let mut summary = String::new();
        for sentence in sentences.iter().take(3) {
            if summary.len() + sentence.len() > max_length {
                break;
            }
            if !summary.is_empty() {
                summary.push(' ');
            }
            summary.push_str(sentence);
        }

        Ok(summary)
    }

    fn extract_key_points(&self, content: &str, num_points: usize) -> Result<Vec<String>> {
        let keywords = self
            .text_processor
            .extract_keywords(content, num_points * 2);
        let sentences = self.text_processor.extract_sentences(content);

        let mut key_points = Vec::new();
        for keyword in keywords.iter().take(num_points) {
            // Find a sentence containing this keyword
            if let Some(sentence) = sentences
                .iter()
                .find(|s| s.to_lowercase().contains(&keyword.to_lowercase()))
            {
                key_points.push(sentence.clone());
            } else {
                key_points.push(format!("Key concept: {keyword}"));
            }
        }

        Ok(key_points)
    }
}

impl LanguageModel for MockLLM {
    type Error = GraphRAGError;

    fn complete(&self, prompt: &str) -> Result<String> {
        self.generate_response(prompt)
    }

    fn complete_with_params(&self, prompt: &str, _params: GenerationParams) -> Result<String> {
        // For mock LLM, we ignore parameters and just use the basic complete
        self.complete(prompt)
    }

    fn is_available(&self) -> bool {
        true
    }

    fn model_info(&self) -> ModelInfo {
        ModelInfo {
            name: "MockLLM".to_string(),
            version: Some("1.0.0".to_string()),
            max_context_length: Some(4096),
            supports_streaming: false,
        }
    }
}

/// Template system for constructing context-aware prompts
#[derive(Debug, Clone)]
pub struct PromptTemplate {
    template: String,
    variables: HashSet<String>,
}

impl PromptTemplate {
    /// Create a new prompt template with variable extraction
    pub fn new(template: String) -> Self {
        let variables = Self::extract_variables(&template);
        Self {
            template,
            variables,
        }
    }

    /// Extract variable names from template (e.g., {context}, {question})
    fn extract_variables(template: &str) -> HashSet<String> {
        let mut variables = HashSet::new();
        let mut chars = template.chars().peekable();

        while let Some(ch) = chars.next() {
            if ch == '{' {
                let mut var_name = String::new();
                while let Some(&next_ch) = chars.peek() {
                    if next_ch == '}' {
                        chars.next(); // consume '}'
                        break;
                    }
                    var_name.push(chars.next().expect("checked above"));
                }
                if !var_name.is_empty() {
                    variables.insert(var_name);
                }
            }
        }

        variables
    }

    /// Fill template with provided values
    pub fn fill(&self, values: &HashMap<String, String>) -> Result<String> {
        let mut result = self.template.clone();

        for (key, value) in values {
            let placeholder = format!("{{{key}}}");
            result = result.replace(&placeholder, value);
        }

        // Check for unfilled variables
        for var in &self.variables {
            let placeholder = format!("{{{var}}}");
            if result.contains(&placeholder) {
                return Err(GraphRAGError::Generation {
                    message: format!("Template variable '{var}' not provided"),
                });
            }
        }

        Ok(result)
    }

    /// Get the set of required variables for this template
    pub fn required_variables(&self) -> &HashSet<String> {
        &self.variables
    }
}

/// Context information assembled from search results
#[derive(Debug, Clone)]
pub struct AnswerContext {
    /// Primary search result chunks with high relevance scores
    pub primary_chunks: Vec<SearchResult>,
    /// Supporting search result chunks with moderate relevance scores
    pub supporting_chunks: Vec<SearchResult>,
    /// Hierarchical summaries from the knowledge graph
    pub hierarchical_summaries: Vec<QueryResult>,
    /// List of entities mentioned in the context
    pub entities: Vec<String>,
    /// Overall confidence score for the context quality
    pub confidence_score: f32,
    /// Total count of sources used in this context
    pub source_count: usize,
}

impl AnswerContext {
    /// Create a new empty answer context
    pub fn new() -> Self {
        Self {
            primary_chunks: Vec::new(),
            supporting_chunks: Vec::new(),
            hierarchical_summaries: Vec::new(),
            entities: Vec::new(),
            confidence_score: 0.0,
            source_count: 0,
        }
    }

    /// Combine all content into a single text block
    pub fn get_combined_content(&self) -> String {
        let mut content = String::new();

        // Add primary chunks first
        for chunk in &self.primary_chunks {
            if !content.is_empty() {
                content.push_str("\n\n");
            }
            content.push_str(&chunk.content);
        }

        // Add supporting chunks
        for chunk in &self.supporting_chunks {
            if !content.is_empty() {
                content.push_str("\n\n");
            }
            content.push_str(&chunk.content);
        }

        // Add hierarchical summaries
        for summary in &self.hierarchical_summaries {
            if !content.is_empty() {
                content.push_str("\n\n");
            }
            content.push_str(&summary.summary);
        }

        content
    }

    /// Get source attribution information
    pub fn get_sources(&self) -> Vec<SourceAttribution> {
        let mut sources = Vec::new();
        let mut source_id = 1;

        for chunk in &self.primary_chunks {
            sources.push(SourceAttribution {
                id: source_id,
                content_type: "chunk".to_string(),
                source_id: chunk.id.clone(),
                confidence: chunk.score,
                snippet: Self::truncate_content(&chunk.content, 100),
            });
            source_id += 1;
        }

        for chunk in &self.supporting_chunks {
            sources.push(SourceAttribution {
                id: source_id,
                content_type: "supporting_chunk".to_string(),
                source_id: chunk.id.clone(),
                confidence: chunk.score,
                snippet: Self::truncate_content(&chunk.content, 100),
            });
            source_id += 1;
        }

        for summary in &self.hierarchical_summaries {
            sources.push(SourceAttribution {
                id: source_id,
                content_type: "summary".to_string(),
                source_id: summary.node_id.0.clone(),
                confidence: summary.score,
                snippet: Self::truncate_content(&summary.summary, 100),
            });
            source_id += 1;
        }

        sources
    }

    fn truncate_content(content: &str, max_len: usize) -> String {
        if content.len() <= max_len {
            content.to_string()
        } else {
            format!("{}...", &content[..max_len])
        }
    }
}

impl Default for AnswerContext {
    fn default() -> Self {
        Self::new()
    }
}

/// Source attribution for generated answers
#[derive(Debug, Clone)]
pub struct SourceAttribution {
    /// Unique identifier for this source
    pub id: usize,
    /// Type of content (chunk, supporting_chunk, summary)
    pub content_type: String,
    /// Identifier of the source document or chunk
    pub source_id: String,
    /// Confidence score for this source
    pub confidence: f32,
    /// Short snippet of the source content
    pub snippet: String,
}

/// Different modes for answer generation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnswerMode {
    /// Extract relevant sentences from context
    Extractive,
    /// Generate new text based on context
    Abstractive,
    /// Combine extraction and generation
    Hybrid,
}

/// Configuration for answer generation
#[derive(Debug, Clone)]
pub struct GenerationConfig {
    /// Mode for answer generation (extractive, abstractive, or hybrid)
    pub mode: AnswerMode,
    /// Maximum length of the generated answer in characters
    pub max_answer_length: usize,
    /// Minimum confidence threshold for accepting results
    pub min_confidence_threshold: f32,
    /// Maximum number of sources to include in the context
    pub max_sources: usize,
    /// Whether to include source citations in the answer
    pub include_citations: bool,
    /// Whether to include confidence scores in the answer
    pub include_confidence_score: bool,
}

impl Default for GenerationConfig {
    fn default() -> Self {
        Self {
            mode: AnswerMode::Hybrid,
            max_answer_length: 500,
            min_confidence_threshold: 0.3,
            max_sources: 10,
            include_citations: true,
            include_confidence_score: true,
        }
    }
}

/// Generated answer with metadata
#[derive(Debug, Clone)]
pub struct GeneratedAnswer {
    /// The generated answer text
    pub answer_text: String,
    /// Overall confidence score for this answer
    pub confidence_score: f32,
    /// List of source attributions used to generate the answer
    pub sources: Vec<SourceAttribution>,
    /// Entities mentioned in the answer
    pub entities_mentioned: Vec<String>,
    /// The generation mode used to produce this answer
    pub mode_used: AnswerMode,
    /// Quality score of the context used for generation
    pub context_quality: f32,
}

impl GeneratedAnswer {
    /// Format the answer with citations
    pub fn format_with_citations(&self) -> String {
        let mut formatted = self.answer_text.clone();

        if !self.sources.is_empty() {
            formatted.push_str("\n\nSources:");
            for source in &self.sources {
                formatted.push_str(&format!(
                    "\n[{}] {} (confidence: {:.2}) - {}",
                    source.id, source.content_type, source.confidence, source.snippet
                ));
            }
        }

        if self.confidence_score > 0.0 {
            formatted.push_str(&format!(
                "\n\nOverall confidence: {:.2}",
                self.confidence_score
            ));
        }

        formatted
    }

    /// Get a quality assessment of the answer
    pub fn get_quality_assessment(&self) -> String {
        let confidence_level = if self.confidence_score >= 0.8 {
            "High"
        } else if self.confidence_score >= 0.5 {
            "Medium"
        } else {
            "Low"
        };

        let source_quality = if self.sources.len() >= 3 {
            "Well-sourced"
        } else if !self.sources.is_empty() {
            "Moderately sourced"
        } else {
            "Poorly sourced"
        };

        format!(
            "Confidence: {} | Sources: {} | Context Quality: {:.2}",
            confidence_level, source_quality, self.context_quality
        )
    }
}

/// Main answer generator that orchestrates the response generation process
pub struct AnswerGenerator {
    llm: Box<dyn LLMInterface>,
    config: GenerationConfig,
    prompt_templates: HashMap<String, PromptTemplate>,
}

impl AnswerGenerator {
    /// Create a new answer generator with the provided LLM and configuration
    pub fn new(llm: Box<dyn LLMInterface>, config: GenerationConfig) -> Result<Self> {
        let mut prompt_templates = HashMap::new();

        // Default prompt templates
        prompt_templates.insert("qa".to_string(), PromptTemplate::new(
            "Context:\n{context}\n\nQuestion: {question}\n\nBased on the provided context, please answer the question. If the context doesn't contain enough information, please say so.".to_string()
        ));

        prompt_templates.insert(
            "summary".to_string(),
            PromptTemplate::new(
                "Please provide a summary of the following content:\n\n{content}\n\nSummary:"
                    .to_string(),
            ),
        );

        prompt_templates.insert("extractive".to_string(), PromptTemplate::new(
            "Extract the most relevant information from the following context to answer the question.\n\nContext: {context}\n\nQuestion: {question}\n\nRelevant information:".to_string()
        ));

        Ok(Self {
            llm,
            config,
            prompt_templates,
        })
    }

    /// Create a new answer generator with custom prompt templates
    pub fn with_custom_templates(
        llm: Box<dyn LLMInterface>,
        config: GenerationConfig,
        templates: HashMap<String, PromptTemplate>,
    ) -> Result<Self> {
        Ok(Self {
            llm,
            config,
            prompt_templates: templates,
        })
    }

    /// Generate an answer from search results
    pub fn generate_answer(
        &self,
        query: &str,
        search_results: Vec<SearchResult>,
        hierarchical_results: Vec<QueryResult>,
    ) -> Result<GeneratedAnswer> {
        // Assemble context from results
        let context = self.assemble_context(search_results, hierarchical_results)?;

        // Check if we have sufficient context
        if context.confidence_score < self.config.min_confidence_threshold {
            return Ok(GeneratedAnswer {
                answer_text: "Insufficient information available to answer this question."
                    .to_string(),
                confidence_score: context.confidence_score,
                sources: context.get_sources(),
                entities_mentioned: context.entities.clone(),
                mode_used: self.config.mode.clone(),
                context_quality: context.confidence_score,
            });
        }

        // Generate answer based on mode
        let answer_text = match self.config.mode {
            AnswerMode::Extractive => self.generate_extractive_answer(query, &context)?,
            AnswerMode::Abstractive => self.generate_abstractive_answer(query, &context)?,
            AnswerMode::Hybrid => self.generate_hybrid_answer(query, &context)?,
        };

        // Calculate final confidence score
        let final_confidence = self.calculate_answer_confidence(&answer_text, &context);

        Ok(GeneratedAnswer {
            answer_text,
            confidence_score: final_confidence,
            sources: context.get_sources(),
            entities_mentioned: context.entities,
            mode_used: self.config.mode.clone(),
            context_quality: context.confidence_score,
        })
    }

    /// Assemble context from search results
    fn assemble_context(
        &self,
        search_results: Vec<SearchResult>,
        hierarchical_results: Vec<QueryResult>,
    ) -> Result<AnswerContext> {
        let mut context = AnswerContext::new();

        // Separate results by type and quality
        let mut primary_chunks = Vec::new();
        let mut supporting_chunks = Vec::new();
        let mut all_entities = HashSet::new();

        for result in search_results {
            // Collect entities
            all_entities.extend(result.entities.iter().cloned());

            // Categorize by score and type
            if result.score >= 0.7
                && matches!(result.result_type, ResultType::Chunk | ResultType::Entity)
            {
                primary_chunks.push(result);
            } else if result.score >= 0.3 {
                supporting_chunks.push(result);
            } else {
                // Results with score < 0.3 are ignored
            }
        }

        // Limit results
        primary_chunks.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        supporting_chunks.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        primary_chunks.truncate(self.config.max_sources / 2);
        supporting_chunks.truncate(self.config.max_sources / 2);

        let mut hierarchical_summaries = hierarchical_results;
        hierarchical_summaries.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        hierarchical_summaries.truncate(3);

        // Calculate confidence based on result quality and quantity
        let avg_primary_score = if primary_chunks.is_empty() {
            0.0
        } else {
            primary_chunks.iter().map(|r| r.score).sum::<f32>() / primary_chunks.len() as f32
        };

        let avg_supporting_score = if supporting_chunks.is_empty() {
            0.0
        } else {
            supporting_chunks.iter().map(|r| r.score).sum::<f32>() / supporting_chunks.len() as f32
        };

        let avg_hierarchical_score = if hierarchical_summaries.is_empty() {
            0.0
        } else {
            hierarchical_summaries.iter().map(|r| r.score).sum::<f32>()
                / hierarchical_summaries.len() as f32
        };

        let confidence_score =
            (avg_primary_score * 0.5 + avg_supporting_score * 0.3 + avg_hierarchical_score * 0.2)
                .min(1.0);

        context.primary_chunks = primary_chunks;
        context.supporting_chunks = supporting_chunks;
        context.hierarchical_summaries = hierarchical_summaries;
        context.entities = all_entities.into_iter().collect();
        context.confidence_score = confidence_score;
        context.source_count = context.primary_chunks.len()
            + context.supporting_chunks.len()
            + context.hierarchical_summaries.len();

        Ok(context)
    }

    /// Generate extractive answer by selecting relevant sentences
    fn generate_extractive_answer(&self, query: &str, context: &AnswerContext) -> Result<String> {
        let combined_content = context.get_combined_content();

        if combined_content.is_empty() {
            return Ok("No relevant content found.".to_string());
        }

        // Use the LLM's extractive capabilities or fallback to simple extraction
        let template =
            self.prompt_templates
                .get("extractive")
                .ok_or_else(|| GraphRAGError::Generation {
                    message: "Extractive template not found".to_string(),
                })?;

        let mut values = HashMap::new();
        values.insert("context".to_string(), combined_content);
        values.insert("question".to_string(), query.to_string());

        let prompt = template.fill(&values)?;
        let response = self.llm.generate_response(&prompt)?;

        // Truncate if too long
        if response.len() > self.config.max_answer_length {
            Ok(format!(
                "{}...",
                &response[..self.config.max_answer_length - 3]
            ))
        } else {
            Ok(response)
        }
    }

    /// Generate abstractive answer using LLM
    fn generate_abstractive_answer(&self, query: &str, context: &AnswerContext) -> Result<String> {
        let combined_content = context.get_combined_content();

        if combined_content.is_empty() {
            return Ok("No relevant content found.".to_string());
        }

        let template =
            self.prompt_templates
                .get("qa")
                .ok_or_else(|| GraphRAGError::Generation {
                    message: "QA template not found".to_string(),
                })?;

        let mut values = HashMap::new();
        values.insert("context".to_string(), combined_content);
        values.insert("question".to_string(), query.to_string());

        let prompt = template.fill(&values)?;
        let response = self.llm.generate_response(&prompt)?;

        // Truncate if too long
        if response.len() > self.config.max_answer_length {
            Ok(format!(
                "{}...",
                &response[..self.config.max_answer_length - 3]
            ))
        } else {
            Ok(response)
        }
    }

    /// Generate hybrid answer combining extraction and generation
    fn generate_hybrid_answer(&self, query: &str, context: &AnswerContext) -> Result<String> {
        // First try extractive approach
        let extractive_answer = self.generate_extractive_answer(query, context)?;

        // If extractive answer is too short or generic, try abstractive
        if extractive_answer.len() < 50 || extractive_answer.contains("No relevant") {
            return self.generate_abstractive_answer(query, context);
        }

        // For hybrid, we return the extractive answer but could enhance it
        Ok(extractive_answer)
    }

    /// Calculate confidence score for the generated answer
    fn calculate_answer_confidence(&self, answer: &str, context: &AnswerContext) -> f32 {
        // Base confidence from context
        let mut confidence = context.confidence_score;

        // Adjust based on answer length and content
        if answer.len() < 20 {
            confidence *= 0.7; // Penalize very short answers
        }

        if answer.contains("No relevant") || answer.contains("insufficient") {
            confidence *= 0.5; // Penalize negative responses
        }

        // Boost confidence if answer mentions entities from context
        let answer_lower = answer.to_lowercase();
        let entity_mentions = context
            .entities
            .iter()
            .filter(|entity| answer_lower.contains(&entity.to_lowercase()))
            .count();

        if entity_mentions > 0 {
            confidence += (entity_mentions as f32 * 0.1).min(0.2);
        }

        confidence.min(1.0)
    }

    /// Add a custom prompt template
    pub fn add_template(&mut self, name: String, template: PromptTemplate) {
        self.prompt_templates.insert(name, template);
    }

    /// Update generation configuration
    pub fn update_config(&mut self, new_config: GenerationConfig) {
        self.config = new_config;
    }

    /// Get statistics about the generator
    pub fn get_statistics(&self) -> GeneratorStatistics {
        GeneratorStatistics {
            template_count: self.prompt_templates.len(),
            config: self.config.clone(),
            available_templates: self.prompt_templates.keys().cloned().collect(),
        }
    }
}

/// Statistics about the answer generator
#[derive(Debug)]
pub struct GeneratorStatistics {
    /// Number of prompt templates registered
    pub template_count: usize,
    /// Current generation configuration
    pub config: GenerationConfig,
    /// List of available template names
    pub available_templates: Vec<String>,
}

impl GeneratorStatistics {
    /// Print statistics about the answer generator to stdout
    pub fn print(&self) {
        println!("Answer Generator Statistics:");
        println!("  Mode: {:?}", self.config.mode);
        println!("  Max answer length: {}", self.config.max_answer_length);
        println!(
            "  Min confidence threshold: {:.2}",
            self.config.min_confidence_threshold
        );
        println!("  Max sources: {}", self.config.max_sources);
        println!("  Include citations: {}", self.config.include_citations);
        println!(
            "  Include confidence: {}",
            self.config.include_confidence_score
        );
        println!("  Available templates: {}", self.available_templates.len());
        for template in &self.available_templates {
            println!("    - {template}");
        }
    }
}

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

    #[test]
    fn test_prompt_template() {
        let template = PromptTemplate::new("Hello {name}, how are you?".to_string());
        assert!(template.variables.contains("name"));

        let mut values = HashMap::new();
        values.insert("name".to_string(), "World".to_string());

        let filled = template.fill(&values).unwrap();
        assert_eq!(filled, "Hello World, how are you?");
    }

    #[test]
    fn test_answer_context() {
        let context = AnswerContext::new();
        assert_eq!(context.confidence_score, 0.0);
        assert_eq!(context.source_count, 0);

        let content = context.get_combined_content();
        assert!(content.is_empty());
    }
}