File size: 31,595 Bytes
192b2d2
 
afad319
192b2d2
afad319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
 
 
 
afad319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
afad319
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
afad319
 
 
 
 
192b2d2
afad319
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
 
afad319
 
192b2d2
 
afad319
192b2d2
 
 
afad319
 
 
 
 
192b2d2
 
 
afad319
192b2d2
afad319
 
 
 
 
 
 
 
 
 
192b2d2
 
afad319
 
 
 
 
 
 
 
 
192b2d2
 
afad319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
afad319
 
 
 
 
 
 
 
 
192b2d2
afad319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
afad319
192b2d2
 
afad319
192b2d2
 
 
 
afad319
 
192b2d2
 
afad319
192b2d2
 
 
afad319
 
 
 
192b2d2
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
 
 
192b2d2
 
 
 
afad319
192b2d2
 
 
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
afad319
 
192b2d2
 
 
 
afad319
 
 
 
 
192b2d2
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
192b2d2
 
 
afad319
192b2d2
 
 
 
 
 
 
afad319
192b2d2
 
 
afad319
192b2d2
afad319
192b2d2
 
 
 
 
 
 
 
 
afad319
192b2d2
 
 
afad319
192b2d2
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
 
192b2d2
 
 
afad319
192b2d2
 
 
afad319
192b2d2
 
 
afad319
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
192b2d2
 
afad319
192b2d2
 
 
 
afad319
192b2d2
 
 
 
 
 
afad319
192b2d2
afad319
192b2d2
afad319
192b2d2
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
afad319
192b2d2
 
 
 
 
 
afad319
192b2d2
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
afad319
 
 
 
 
 
192b2d2
afad319
 
192b2d2
 
afad319
192b2d2
 
afad319
192b2d2
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
 
 
afad319
192b2d2
 
 
 
afad319
192b2d2
 
 
 
 
afad319
192b2d2
afad319
 
192b2d2
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
afad319
192b2d2
afad319
192b2d2
 
 
 
 
afad319
192b2d2
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
192b2d2
afad319
 
 
 
 
 
 
 
 
192b2d2
 
afad319
 
 
 
192b2d2
 
afad319
192b2d2
 
 
afad319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
afad319
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
afad319
192b2d2
 
afad319
192b2d2
 
afad319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
192b2d2
 
 
 
 
 
 
 
 
 
 
 
afad319
 
 
 
 
 
 
 
192b2d2
 
 
 
 
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
#!/usr/bin/env python3
"""
# Simplified RAG System for Hugging Face Spaces

This module provides a comprehensive Retrieval-Augmented Generation (RAG) system using:
- **FAISS** for efficient vector storage and similarity search
- **BM25** for sparse retrieval and keyword matching
- **Hybrid Search** combining both dense and sparse methods
- **Qwen 2.5 1.5B** for intelligent response generation
- **Thread Safety** for concurrent document loading

## Architecture Overview

The RAG system follows a modular design with these key components:

1. **Document Processing**: PDF extraction and intelligent chunking
2. **Vector Storage**: FAISS index for high-dimensional embeddings
3. **Sparse Retrieval**: BM25 for keyword-based search
4. **Hybrid Search**: Combines dense and sparse methods for optimal results
5. **Response Generation**: LLM-based answer synthesis with context
6. **Thread Safety**: Concurrent document loading with proper locking

## Key Features

- πŸ” **Multi-Method Search**: Hybrid, dense, and sparse retrieval options
- πŸ“Š **Performance Metrics**: Confidence scores and response times
- πŸ”’ **Thread Safety**: Safe concurrent document loading
- πŸ’Ύ **Persistence**: Automatic index saving and loading
- 🎯 **Smart Fallbacks**: Graceful model loading with alternatives
- πŸ“ˆ **Scalable**: Efficient handling of large document collections

## Usage Example

```python
# Initialize the RAG system
rag = SimpleRAGSystem()

# Add documents
rag.add_document("document.pdf", "Document Name")

# Query the system
response = rag.query("What is the main topic?", method="hybrid", top_k=5)
print(response.answer)
```
"""

import os
import pickle
import json
import time
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
import numpy as np
import torch
from loguru import logger
import threading

# Import required libraries for AI/ML functionality
from sentence_transformers import SentenceTransformer
from rank_bm25 import BM25Okapi
import faiss
from transformers import AutoTokenizer, AutoModelForCausalLM

# Import guard rail system
from guard_rails import GuardRailSystem, GuardRailConfig, GuardRailResult

# Import HF Spaces configuration
try:
    from hf_spaces_config import get_hf_config, is_hf_spaces

    HF_SPACES_AVAILABLE = True
except ImportError:
    HF_SPACES_AVAILABLE = False
    logger.warning("HF Spaces configuration not available")


# =============================================================================
# DATA STRUCTURES
# =============================================================================


@dataclass
class DocumentChunk:
    """
    Represents a document chunk with metadata

    Attributes:
        text: The actual text content of the chunk
        doc_id: Unique identifier for the source document
        filename: Name of the source file
        chunk_id: Unique identifier for this specific chunk
        chunk_size: Target size used for chunking
    """

    text: str
    doc_id: str
    filename: str
    chunk_id: str
    chunk_size: int


@dataclass
class SearchResult:
    """
    Represents a search result with scoring information

    Attributes:
        text: The retrieved text content
        score: Combined relevance score
        doc_id: Source document identifier
        filename: Source file name
        search_method: Method used for retrieval (dense/sparse/hybrid)
        dense_score: Vector similarity score (if applicable)
        sparse_score: Keyword matching score (if applicable)
    """

    text: str
    score: float
    doc_id: str
    filename: str
    search_method: str
    dense_score: Optional[float] = None
    sparse_score: Optional[float] = None


@dataclass
class RAGResponse:
    """
    Represents a complete RAG system response

    Attributes:
        answer: Generated answer text
        confidence: Confidence score for the response
        search_results: List of retrieved documents
        method_used: Search method that was used
        response_time: Time taken to generate response
        query: Original user query
    """

    answer: str
    confidence: float
    search_results: List[SearchResult]
    method_used: str
    response_time: float
    query: str


# =============================================================================
# MAIN RAG SYSTEM CLASS
# =============================================================================


class SimpleRAGSystem:
    """
    Simplified RAG system for Hugging Face Spaces

    This class provides a complete RAG implementation with:
    - Document ingestion and processing
    - Vector and sparse search capabilities
    - Response generation using language models
    - Thread-safe concurrent operations
    - Persistent storage and retrieval
    """

    def __init__(
        self,
        embedding_model: str = "all-MiniLM-L6-v2",
        generative_model: str = "Qwen/Qwen2.5-1.5B-Instruct",
        chunk_sizes: List[int] = None,
        vector_store_path: str = "./vector_store",
        enable_guard_rails: bool = True,
        guard_rail_config: GuardRailConfig = None,
    ):
        """
        Initialize the RAG system with specified models and configuration

        Args:
            embedding_model: Sentence transformer model for embeddings
            generative_model: Language model for response generation
            chunk_sizes: List of chunk sizes for document processing
            vector_store_path: Path for storing FAISS index and metadata
            enable_guard_rails: Whether to enable guard rail system
            guard_rail_config: Configuration for guard rail system
        """
        self.embedding_model = embedding_model
        self.generative_model = generative_model
        self.chunk_sizes = chunk_sizes or [100, 400]  # Default chunk sizes
        self.vector_store_path = vector_store_path
        self.enable_guard_rails = enable_guard_rails

        # Initialize core components
        self.embedder = None  # Sentence transformer for embeddings
        self.tokenizer = None  # Tokenizer for language model
        self.model = None  # Language model for generation
        self.faiss_index = None  # FAISS index for vector search
        self.bm25 = None  # BM25 for sparse search
        self.documents = []  # List of processed documents
        self.chunks = []  # List of document chunks
        self._lock = threading.Lock()  # Thread safety for concurrent loading

        # Initialize guard rail system
        if self.enable_guard_rails:
            self.guard_rails = GuardRailSystem(guard_rail_config)
            logger.info("Guard rail system enabled")
        else:
            self.guard_rails = None
            logger.info("Guard rail system disabled")

        # Create vector store directory for persistence
        os.makedirs(vector_store_path, exist_ok=True)

        # Set up HF Spaces configuration if available
        if HF_SPACES_AVAILABLE:
            try:
                hf_config = get_hf_config()
                if is_hf_spaces():
                    logger.info(
                        "🌐 HF Spaces environment detected - using optimized configuration"
                    )
                    # Cache directories are automatically set up by hf_config
                else:
                    logger.info("πŸ’» Local development environment detected")
            except Exception as e:
                logger.warning(f"HF Spaces configuration failed: {e}")

        # Load or initialize system components
        self._load_models()
        self._load_or_create_index()

        logger.info("Simple RAG system initialized successfully!")

    def _load_models(self):
        """
        Load embedding and generative models with fallback handling

        This method:
        1. Loads the sentence transformer for embeddings
        2. Attempts to load the primary language model (Qwen)
        3. Falls back to distilgpt2 if primary model fails
        4. Configures tokenizers and model settings
        """
        try:
            # Get cache directory from HF Spaces config if available
            cache_dir = None
            if HF_SPACES_AVAILABLE:
                try:
                    hf_config = get_hf_config()
                    cache_dir = hf_config.cache_dirs.get("transformers_cache")
                    if cache_dir:
                        logger.info(f"Using HF Spaces cache directory: {cache_dir}")
                except Exception as e:
                    logger.warning(f"Could not get HF Spaces cache directory: {e}")

            # Load embedding model for document vectorization
            if cache_dir:
                self.embedder = SentenceTransformer(
                    self.embedding_model, cache_folder=cache_dir
                )
            else:
                self.embedder = SentenceTransformer(self.embedding_model)
            self.vector_size = self.embedder.get_sentence_embedding_dimension()

            # Load generative model with fallback strategy
            model_loaded = False

            # Try loading Qwen model first (primary choice)
            try:
                self.tokenizer = AutoTokenizer.from_pretrained(
                    self.generative_model,
                    trust_remote_code=True,
                    padding_side="left",  # Important for generation
                    cache_dir=cache_dir,
                )

                # Load model with explicit CPU configuration for deployment compatibility
                self.model = AutoModelForCausalLM.from_pretrained(
                    self.generative_model,
                    trust_remote_code=True,
                    torch_dtype=torch.float32,  # Use float32 for CPU compatibility
                    device_map=None,  # Let PyTorch handle device placement
                    low_cpu_mem_usage=False,  # Disable for better compatibility
                    cache_dir=cache_dir,
                )

                # Move to CPU explicitly for deployment environments
                self.model = self.model.to("cpu")
                model_loaded = True

            except Exception as e:
                logger.warning(f"Failed to load Qwen model: {e}")

            # Fallback to distilgpt2 if Qwen fails
            if not model_loaded:
                logger.info("Falling back to distilgpt2...")
                self.generative_model = "distilgpt2"
                try:
                    self.tokenizer = AutoTokenizer.from_pretrained(
                        self.generative_model,
                        trust_remote_code=True,
                        padding_side="left",
                    )
                    self.model = AutoModelForCausalLM.from_pretrained(
                        self.generative_model,
                        trust_remote_code=True,
                    )
                    # Ensure fallback model is also on CPU
                    self.model = self.model.to("cpu")
                    model_loaded = True
                except Exception as e:
                    logger.error(f"Failed to load distilgpt2: {e}")
                    raise Exception("Could not load any generative model")

            # Configure tokenizer settings for generation
            if self.tokenizer.pad_token is None:
                self.tokenizer.pad_token = self.tokenizer.eos_token
                self.tokenizer.pad_token_id = self.tokenizer.eos_token_id

            logger.info(f"βœ… Models loaded successfully")
            logger.info(f"   - Embedding: {self.embedding_model}")
            logger.info(f"   - Generative: {self.generative_model}")

        except Exception as e:
            logger.error(f"❌ Failed to load models: {e}")
            raise

    def _load_or_create_index(self):
        """
        Load existing FAISS index or create a new one

        This method:
        1. Checks for existing index files
        2. Loads existing index and metadata if available
        3. Creates new index if none exists
        4. Rebuilds BM25 index from loaded chunks
        """
        faiss_path = os.path.join(self.vector_store_path, "faiss_index.bin")
        metadata_path = os.path.join(self.vector_store_path, "metadata.pkl")

        if os.path.exists(faiss_path) and os.path.exists(metadata_path):
            # Load existing index and metadata
            try:
                self.faiss_index = faiss.read_index(faiss_path)
                with open(metadata_path, "rb") as f:
                    metadata = pickle.load(f)
                    self.documents = metadata.get("documents", [])
                    self.chunks = metadata.get("chunks", [])

                # Rebuild BM25 index from loaded chunks
                if self.chunks:
                    texts = [chunk.text for chunk in self.chunks]
                    tokenized_texts = [text.lower().split() for text in texts]
                    self.bm25 = BM25Okapi(tokenized_texts)

                logger.info(f"βœ… Loaded existing index with {len(self.chunks)} chunks")
            except Exception as e:
                logger.warning(f"Failed to load existing index: {e}")
                self._create_new_index()
        else:
            self._create_new_index()

    def _create_new_index(self):
        """Create new FAISS index with appropriate configuration"""
        vector_size = self.embedder.get_sentence_embedding_dimension()
        # Use Inner Product for cosine similarity (normalized vectors)
        self.faiss_index = faiss.IndexFlatIP(vector_size)
        self.bm25 = None
        logger.info(f"βœ… Created new FAISS index with dimension {vector_size}")

    def _save_index(self):
        """
        Save FAISS index and metadata for persistence

        This ensures that the system state is preserved across restarts.
        """
        try:
            # Save FAISS index
            faiss_path = os.path.join(self.vector_store_path, "faiss_index.bin")
            faiss.write_index(self.faiss_index, faiss_path)

            # Save metadata including documents and chunks
            metadata_path = os.path.join(self.vector_store_path, "metadata.pkl")
            metadata = {"documents": self.documents, "chunks": self.chunks}
            with open(metadata_path, "wb") as f:
                pickle.dump(metadata, f)

            logger.info("βœ… Index saved successfully")
        except Exception as e:
            logger.error(f"❌ Failed to save index: {e}")

    def add_document(self, file_path: str, filename: str) -> bool:
        """
        Add a document to the RAG system with thread safety

        This method:
        1. Processes the PDF document into chunks
        2. Adds document metadata to the system
        3. Updates embeddings and BM25 index
        4. Saves the updated index

        Args:
            file_path: Path to the PDF file
            filename: Name of the file for reference

        Returns:
            True if successful, False otherwise
        """
        try:
            from pdf_processor import SimplePDFProcessor

            # Process the document using the PDF processor
            processor = SimplePDFProcessor()
            processed_doc = processor.process_document(file_path, self.chunk_sizes)

            # Thread-safe document addition using lock
            with self._lock:
                # Add document metadata to the system
                self.documents.append(
                    {
                        "filename": filename,
                        "title": processed_doc.title,
                        "author": processed_doc.author,
                        "file_path": file_path,
                    }
                )

                # Add all chunks from the processed document
                for chunk in processed_doc.chunks:
                    self.chunks.append(chunk)

                # Update search indices with new content
                self._update_embeddings()
                self._update_bm25()

                # Persist the updated index
                self._save_index()

            logger.info(
                f"βœ… Added document: {filename} ({len(processed_doc.chunks)} chunks)"
            )
            return True

        except Exception as e:
            logger.error(f"❌ Failed to add document {filename}: {e}")
            return False

    def _update_embeddings(self):
        """
        Update FAISS index with new embeddings

        This method:
        1. Extracts text from all chunks
        2. Generates embeddings using the sentence transformer
        3. Adds embeddings to the FAISS index
        """
        if not self.chunks:
            return

        # Generate embeddings for all chunks
        texts = [chunk.text for chunk in self.chunks]
        embeddings = self.embedder.encode(texts, show_progress_bar=False)

        # Add embeddings to FAISS index
        self.faiss_index.add(embeddings.astype("float32"))

    def _update_bm25(self):
        """
        Update BM25 index with new chunks

        This method rebuilds the BM25 index with all current chunks
        for keyword-based search functionality.
        """
        if not self.chunks:
            return

        # Rebuild BM25 with all chunks
        texts = [chunk.text for chunk in self.chunks]
        tokenized_texts = [text.lower().split() for text in texts]
        self.bm25 = BM25Okapi(tokenized_texts)

    def search(
        self, query: str, method: str = "hybrid", top_k: int = 5
    ) -> List[SearchResult]:
        """
        Search for relevant documents using specified method

        This method supports three search strategies:
        - **dense**: Vector similarity search using FAISS
        - **sparse**: Keyword matching using BM25
        - **hybrid**: Combines both methods for optimal results

        Args:
            query: Search query string
            method: Search method (hybrid, dense, sparse)
            top_k: Number of results to return

        Returns:
            List of search results with scores and metadata
        """
        if not self.chunks:
            return []

        results = []

        # Perform dense search (vector similarity)
        if method == "dense" or method == "hybrid":
            # Generate query embedding
            query_embedding = self.embedder.encode([query])
            # Search FAISS index
            scores, indices = self.faiss_index.search(
                query_embedding.astype("float32"), min(top_k, len(self.chunks))
            )

            # Process dense search results
            for score, idx in zip(scores[0], indices[0]):
                if idx < len(self.chunks):
                    chunk = self.chunks[idx]
                    results.append(
                        SearchResult(
                            text=chunk.text,
                            score=float(score),
                            doc_id=chunk.doc_id,
                            filename=chunk.filename,
                            search_method="dense",
                            dense_score=float(score),
                        )
                    )

        # Perform sparse search (keyword matching)
        if method == "sparse" or method == "hybrid":
            if self.bm25:
                # Tokenize query for BM25
                tokenized_query = query.lower().split()
                bm25_scores = self.bm25.get_scores(tokenized_query)

                # Get top BM25 results
                top_indices = np.argsort(bm25_scores)[::-1][:top_k]

                # Process sparse search results
                for idx in top_indices:
                    if idx < len(self.chunks):
                        chunk = self.chunks[idx]
                        score = float(bm25_scores[idx])

                        # Check if result already exists (for hybrid search)
                        existing_result = next(
                            (
                                r
                                for r in results
                                if r.doc_id == chunk.doc_id and r.text == chunk.text
                            ),
                            None,
                        )

                        if existing_result:
                            # Update existing result with sparse score
                            existing_result.sparse_score = score
                            if method == "hybrid":
                                # Combine scores for hybrid search
                                existing_result.score = (
                                    existing_result.dense_score + score
                                ) / 2
                        else:
                            # Add new sparse result
                            results.append(
                                SearchResult(
                                    text=chunk.text,
                                    score=score,
                                    doc_id=chunk.doc_id,
                                    filename=chunk.filename,
                                    search_method="sparse",
                                    sparse_score=score,
                                )
                            )

        # Sort by score and return top_k results
        results.sort(key=lambda x: x.score, reverse=True)
        return results[:top_k]

    def generate_response(self, query: str, context: str) -> str:
        """
        Generate response using the language model

        This method:
        1. Prepares a prompt with context and query
        2. Uses the appropriate chat template for the model
        3. Generates a response with controlled parameters
        4. Handles model-specific response formatting

        Args:
            query: User's question
            context: Retrieved context from search

        Returns:
            Generated response text
        """
        try:
            # Prepare prompt based on model capabilities
            if hasattr(self.tokenizer, "apply_chat_template"):
                # Use chat template for modern models like Qwen
                messages = [
                    {
                        "role": "system",
                        "content": "You are a helpful AI assistant. Use the provided context to answer the user's question accurately and concisely. If the context doesn't contain enough information to answer the question, say so.",
                    },
                    {
                        "role": "user",
                        "content": f"Context: {context}\n\nQuestion: {query}",
                    },
                ]
                prompt = self.tokenizer.apply_chat_template(
                    messages, tokenize=False, add_generation_prompt=True
                )
            else:
                # Fallback for non-chat models
                prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"

            # Tokenize input with appropriate settings
            tokenized = self.tokenizer(
                prompt,
                return_tensors="pt",
                truncation=True,
                max_length=1024,  # Limit input length
                padding=True,
                return_attention_mask=True,
            )

            # Generate response with controlled parameters
            with torch.no_grad():
                try:
                    outputs = self.model.generate(
                        tokenized.input_ids,
                        attention_mask=tokenized.attention_mask,
                        max_new_tokens=512,  # Limit response length
                        num_return_sequences=1,
                        temperature=0.7,  # Balance creativity and coherence
                        do_sample=True,  # Enable sampling for more natural responses
                        pad_token_id=self.tokenizer.pad_token_id,
                        eos_token_id=self.tokenizer.eos_token_id,
                    )
                except RuntimeError as e:
                    if "Half" in str(e):
                        # Handle half-precision compatibility issues
                        logger.warning(
                            "Half precision not supported on CPU, converting to float32"
                        )
                        # Convert model to float32
                        self.model = self.model.float()
                        outputs = self.model.generate(
                            tokenized.input_ids,
                            attention_mask=tokenized.attention_mask,
                            max_new_tokens=512,
                            num_return_sequences=1,
                            temperature=0.7,
                            do_sample=True,
                            pad_token_id=self.tokenizer.pad_token_id,
                            eos_token_id=self.tokenizer.eos_token_id,
                        )
                    else:
                        raise e

            # Decode the generated response
            response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)

            # Extract only the generated part (remove input prompt)
            if hasattr(self.tokenizer, "apply_chat_template"):
                # Handle chat model response formatting
                if "<|im_start|>assistant" in response:
                    response = response.split("<|im_start|>assistant")[-1]
                if "<|im_end|>" in response:
                    response = response.split("<|im_end|>")[0]
            else:
                # Handle standard model response formatting
                response = response[len(prompt) :]

            return response.strip()

        except Exception as e:
            logger.error(f"Error generating response: {e}")
            return f"Error generating response: {str(e)}"

    def query(
        self,
        query: str,
        method: str = "hybrid",
        top_k: int = 5,
        user_id: str = "anonymous",
    ) -> RAGResponse:
        """
        Complete RAG query pipeline with guard rail protection

        This method orchestrates the entire RAG process with safety checks:
        1. Validates input using guard rails
        2. Searches for relevant documents
        3. Combines context from search results
        4. Generates a response using the language model
        5. Validates output using guard rails
        6. Calculates confidence and timing metrics

        Args:
            query: User's question
            method: Search method to use
            top_k: Number of search results to use
            user_id: User identifier for rate limiting and tracking

        Returns:
            Complete RAG response with answer, metadata, and metrics
        """
        start_time = time.time()

        # =============================================================================
        # INPUT VALIDATION WITH GUARD RAILS
        # =============================================================================

        if self.enable_guard_rails and self.guard_rails:
            # Validate input using guard rails
            input_validation = self.guard_rails.validate_input(query, user_id)
            if not input_validation.passed:
                logger.warning(f"Input validation failed: {input_validation.reason}")
                if input_validation.blocked:
                    return RAGResponse(
                        answer=f"I cannot process this request: {input_validation.reason}",
                        confidence=0.0,
                        search_results=[],
                        method_used=method,
                        response_time=time.time() - start_time,
                        query=query,
                    )
                else:
                    # Warning but continue processing
                    logger.warning(
                        f"Input validation warning: {input_validation.reason}"
                    )

            # Sanitize input
            query = self.guard_rails.sanitize_input(query)

        # Search for relevant documents
        search_results = self.search(query, method, top_k)

        # Handle case where no relevant documents found
        if not search_results:
            return RAGResponse(
                answer="I couldn't find any relevant information to answer your question.",
                confidence=0.0,
                search_results=[],
                method_used=method,
                response_time=time.time() - start_time,
                query=query,
            )

        # Combine context from search results
        context = "\n\n".join([result.text for result in search_results])

        # Generate response using the language model
        answer = self.generate_response(query, context)

        # Calculate confidence based on search result scores
        confidence = np.mean([result.score for result in search_results])

        # =============================================================================
        # OUTPUT VALIDATION WITH GUARD RAILS
        # =============================================================================

        if self.enable_guard_rails and self.guard_rails:
            # Validate output using guard rails
            output_validation = self.guard_rails.validate_output(
                answer, confidence, context
            )
            if not output_validation.passed:
                logger.warning(f"Output validation failed: {output_validation.reason}")
                if output_validation.blocked:
                    return RAGResponse(
                        answer="I cannot provide this response due to safety concerns.",
                        confidence=0.0,
                        search_results=search_results,
                        method_used=method,
                        response_time=time.time() - start_time,
                        query=query,
                    )
                else:
                    # Warning but continue with response
                    logger.warning(
                        f"Output validation warning: {output_validation.reason}"
                    )

            # Sanitize output
            answer = self.guard_rails.sanitize_output(answer)

        # Create and return complete response
        return RAGResponse(
            answer=answer,
            confidence=confidence,
            search_results=search_results,
            method_used=method,
            response_time=time.time() - start_time,
            query=query,
        )

    def get_stats(self) -> Dict:
        """
        Get system statistics and configuration information

        Returns:
            Dictionary containing system metrics and settings
        """
        return {
            "total_documents": len(self.documents),
            "total_chunks": len(self.chunks),
            "vector_size": (
                self.embedder.get_sentence_embedding_dimension() if self.embedder else 0
            ),
            "model_name": self.generative_model,
            "embedding_model": self.embedding_model,
            "chunk_sizes": self.chunk_sizes,
        }

    def clear(self):
        """
        Clear all documents and reset the system

        This method:
        1. Clears all documents and chunks
        2. Creates a new FAISS index
        3. Saves the empty state
        """
        self.documents = []
        self.chunks = []
        self._create_new_index()
        self._save_index()
        logger.info("βœ… System cleared successfully")