File size: 24,839 Bytes
09281fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
GPU-Optimized RAG System for High Performance
"""

import os
import json
import logging
import hashlib
from datetime import datetime
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass, asdict
from pathlib import Path

import fitz  # PyMuPDF
import pytesseract
from pdf2image import convert_from_path
from PIL import Image
import cv2
import numpy as np
from docx import Document
from bs4 import BeautifulSoup
import requests

from sentence_transformers import SentenceTransformer
import chromadb
from chromadb.config import Settings

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import Document
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor

# Disable ChromaDB telemetry
os.environ["ANONYMIZED_TELEMETRY"] = "False"

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@dataclass
class DocumentChunk:
    """Represents a chunk of processed document with metadata"""
    chunk_id: str
    content: str
    source_file: str
    page_number: Optional[int] = None
    chunk_index: Optional[int] = None
    category: Optional[str] = None
    embedding: Optional[List[float]] = None

@dataclass
class QueryResult:
    """Structured result from query processing"""
    decision: str  # approved/rejected/conditional
    justification: str
    relevant_clauses: List[str]
    confidence_score: float
    audit_trail: Dict[str, Any]
    amount: Optional[float] = None

class DocumentProcessor:
    """Handles document ingestion and preprocessing with OCR support"""
    
    def __init__(self, ocr_language='eng'):
        self.ocr_language = ocr_language
        self.text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=1000,
            chunk_overlap=200,
            separators=["\n\n", "\n", ". ", " ", ""]
        )
    
    def extract_text_from_pdf(self, pdf_path: str, use_ocr: bool = False) -> str:
        """Extract text from PDF with optional OCR for scanned documents"""
        try:
            doc = fitz.open(pdf_path)
            text = ""
            
            for page_num in range(len(doc)):
                page = doc.load_page(page_num)
                
                # Try to extract text normally first
                page_text = page.get_text()
                
                # If no text found or very little text, use OCR
                if use_ocr or len(page_text.strip()) < 50:
                    logger.info(f"Using OCR for page {page_num + 1}")
                    try:
                        pix = page.get_pixmap()
                        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
                        
                        # Convert to grayscale for better OCR
                        img_gray = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
                        
                        # Apply preprocessing for better OCR
                        img_processed = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
                        
                        # Extract text using OCR
                        page_text = pytesseract.image_to_string(
                            img_processed, 
                            lang=self.ocr_language,
                            config='--psm 6'
                        )
                    except Exception as ocr_error:
                        logger.warning(f"OCR failed for page {page_num + 1}: {ocr_error}")
                        logger.warning("Continuing with existing text extraction")
                        # Keep the existing page_text (from normal extraction)
                
                text += f"\n\n--- Page {page_num + 1} ---\n{page_text}"
            
            doc.close()
            return text
            
        except Exception as e:
            logger.error(f"Error extracting text from PDF {pdf_path}: {e}")
            raise
    
    def chunk_document(self, text: str, source_file: str) -> List[DocumentChunk]:
        """Chunk document into semantically coherent passages"""
        try:
            # Use LangChain's text splitter for better semantic chunking
            docs = [Document(page_content=text, metadata={"source": source_file})]
            split_docs = self.text_splitter.split_documents(docs)
            
            chunks = []
            for i, doc in enumerate(split_docs):
                chunk = DocumentChunk(
                    chunk_id=f"chunk_{i+1}_{hashlib.md5(doc.page_content.encode()).hexdigest()[:8]}",
                    content=doc.page_content.strip(),
                    source_file=source_file,
                    chunk_index=i
                )
                chunks.append(chunk)
            
            return chunks
            
        except Exception as e:
            logger.error(f"Error chunking document: {e}")
            raise

class VectorDatabase:
    """Manages vector storage and retrieval with GPU optimization"""
    
    def _clean_metadata(self, metadata_dict: Dict[str, Any]) -> Dict[str, Any]:
        """Clean metadata by removing None values and converting to proper types"""
        clean_metadata = {}
        for key, value in metadata_dict.items():
            if value is not None:
                if isinstance(value, (int, float)):
                    clean_metadata[key] = value
                else:
                    clean_metadata[key] = str(value)
        return clean_metadata
    
    def __init__(self, persist_directory: str = "./vector_db", use_gpu: bool = True):
        self.persist_directory = persist_directory
        
        # GPU-optimized embeddings
        device = 'cuda' if use_gpu else 'cpu'
        self.embeddings = HuggingFaceEmbeddings(
            model_name="all-MiniLM-L6-v2",
            model_kwargs={'device': device}
        )
        
        # Initialize ChromaDB
        import chromadb
        from chromadb.config import Settings
        
        # Create ChromaDB client with proper settings
        client = chromadb.PersistentClient(
            path=persist_directory,
            settings=Settings(
                anonymized_telemetry=False,
                is_persistent=True
            )
        )
        
        # Create collection for documents
        collection_name = "insurance_documents"
        try:
            self.collection = client.get_collection(collection_name)
        except:
            self.collection = client.create_collection(collection_name)
        
        self.vectorstore = Chroma(
            embedding_function=self.embeddings,
            persist_directory=persist_directory,
            client=client,
            collection_name=collection_name
        )
    
    def add_documents(self, chunks: List[DocumentChunk]) -> None:
        """Add document chunks to vector database"""
        try:
            documents = []
            metadatas = []
            ids = []
            
            for chunk in chunks:
                documents.append(chunk.content)
                
                # Create metadata dictionary
                metadata = {
                    "chunk_id": chunk.chunk_id,
                    "source_file": chunk.source_file,
                    "page_number": chunk.page_number,
                    "chunk_index": chunk.chunk_index,
                    "category": chunk.category
                }
                
                # Clean metadata using helper function
                clean_metadata = self._clean_metadata(metadata)
                metadatas.append(clean_metadata)
                ids.append(chunk.chunk_id)
            
            # Get embeddings for documents (GPU-accelerated)
            embeddings = self.embeddings.embed_documents(documents)
            
            # Add to collection
            self.collection.add(
                documents=documents,
                metadatas=metadatas,
                embeddings=embeddings,
                ids=ids
            )
            
            logger.info(f"Added {len(chunks)} chunks to vector database")
            
        except Exception as e:
            logger.error(f"Error adding documents to vector database: {e}")
            raise
    
    def search(self, query: str, k: int = 5) -> List[Dict[str, Any]]:
        """Search for relevant document chunks"""
        try:
            # Get query embedding (GPU-accelerated)
            query_embedding = self.embeddings.embed_query(query)
            
            # Search in collection
            results = self.collection.query(
                query_embeddings=[query_embedding],
                n_results=k,
                include=["documents", "metadatas", "distances"]
            )
            
            search_results = []
            if results['documents'] and results['documents'][0]:
                for i, (doc, metadata, distance) in enumerate(zip(
                    results['documents'][0], 
                    results['metadatas'][0], 
                    results['distances'][0]
                )):
                    # Clean metadata using helper function
                    clean_metadata = self._clean_metadata(metadata)
                    
                    search_results.append({
                        "content": doc,
                        "metadata": clean_metadata,
                        "similarity_score": 1.0 - float(distance)  # Convert distance to similarity
                    })
            
            return search_results
            
        except Exception as e:
            logger.error(f"Error searching vector database: {e}")
            raise

class QueryParser:
    """Parses and structures natural language queries"""
    
    def __init__(self, llm_model):
        self.llm = llm_model
    
    def extract_entities(self, query: str) -> Dict[str, Any]:
        """Extract structured entities from natural language query"""
        try:
            prompt = f"""
            Extract structured information from the following query about insurance/policy:
            
            Query: {query}
            
            Extract the following information in JSON format:
            {{
                "age": <age if mentioned>,
                "procedure": <medical procedure if mentioned>,
                "location": <location if mentioned>,
                "policy_type": <type of policy mentioned>,
                "claim_amount": <amount if mentioned>,
                "condition": <medical condition if mentioned>,
                "intent": <what the user is asking about>
            }}
            
            JSON Response:
            """
            
            # Call LLM with proper format
            if hasattr(self.llm, 'generate_content'):  # Gemini
                response = self.llm.generate_content(prompt).text
            else:  # Llama
                response = self.llm(prompt, max_tokens=200, temperature=0.1, stop=["\n\n"])
                if isinstance(response, dict):
                    response = response.get('choices', [{}])[0].get('text', '')
                elif hasattr(response, 'choices'):
                    response = response.choices[0].text
            
            # Parse response - handle both string and dict responses
            try:
                if isinstance(response, dict):
                    entities = response
                else:
                    entities = json.loads(response)
                return entities
            except (json.JSONDecodeError, TypeError):
                # Fallback parsing
                return self._fallback_entity_extraction(query)
                
        except Exception as e:
            logger.error(f"Error extracting entities: {e}")
            return self._fallback_entity_extraction(query)
    
    def _fallback_entity_extraction(self, query: str) -> Dict[str, Any]:
        """Simple fallback entity extraction"""
        entities = {
            "age": None,
            "procedure": None,
            "location": None,
            "policy_type": None,
            "claim_amount": None,
            "condition": None,
            "intent": "general_inquiry"
        }
        
        # Simple keyword-based extraction
        query_lower = query.lower()
        
        # Extract age
        import re
        age_match = re.search(r'(\d+)\s*(?:years?|yrs?)', query_lower)
        if age_match:
            entities["age"] = int(age_match.group(1))
        
        # Extract amount
        amount_match = re.search(r'(\d+(?:,\d+)*(?:\.\d+)?)\s*(?:rs?|rupees?|inr)', query_lower)
        if amount_match:
            entities["claim_amount"] = float(amount_match.group(1).replace(',', ''))
        
        return entities

class LLMReasoning:
    """Handles LLM-based reasoning and decision logic"""
    
    def __init__(self, llm_model):
        self.llm = llm_model
    
    def analyze_query(self, query: str, relevant_chunks: List[Dict], parsed_entities: Dict) -> QueryResult:
        """Analyze query against relevant document chunks"""
        try:
            # Prepare context from relevant chunks
            context = "\n\n".join([
                f"Document {i+1}:\n{chunk['content']}\nClause ID: {chunk['metadata'].get('chunk_id', 'N/A')}"
                for i, chunk in enumerate(relevant_chunks)
            ])
            
            # Create reasoning prompt
            prompt = f"""
            You are an insurance policy analyzer. Analyze the following query against the provided policy documents.
            
            User Query: {query}
            Extracted Entities: {json.dumps(parsed_entities, indent=2)}
            
            Relevant Policy Clauses:
            {context}
            
            Please provide a structured analysis in the following JSON format:
            {{
                "decision": "approved/rejected/conditional",
                "amount": <amount if applicable, null otherwise>,
                "justification": "<detailed explanation with specific clause references>",
                "relevant_clauses": ["<list of clause IDs that support the decision>"],
                "confidence_score": <0.0 to 1.0>,
                "conditions": ["<any conditions that must be met>"]
            }}
            
            Base your decision on:
            1. Policy coverage and exclusions
            2. Eligibility criteria
            3. Waiting periods
            4. Pre-existing conditions
            5. Specific terms and conditions
            
            JSON Response:
            """
            
            # Call LLM with proper format
            if hasattr(self.llm, 'generate_content'):  # Gemini
                response = self.llm.generate_content(prompt).text
            else:  # Llama
                response = self.llm(prompt, max_tokens=500, temperature=0.1, stop=["\n\n"])
                if isinstance(response, dict):
                    response = response.get('choices', [{}])[0].get('text', '')
                elif hasattr(response, 'choices'):
                    response = response.choices[0].text
            
            # Parse response - handle both string and dict responses
            try:
                if isinstance(response, dict):
                    result_data = response
                else:
                    result_data = json.loads(response)
                
                # Create audit trail
                audit_trail = {
                    "timestamp": datetime.now().isoformat(),
                    "query": query,
                    "parsed_entities": parsed_entities,
                    "relevant_chunks_count": len(relevant_chunks),
                    "llm_prompt": prompt,
                    "llm_response": response,
                    "chunk_ids": [chunk['metadata'].get('chunk_id') for chunk in relevant_chunks]
                }
                
                return QueryResult(
                    decision=result_data.get("decision", "conditional"),
                    amount=result_data.get("amount"),
                    justification=result_data.get("justification", "Analysis incomplete"),
                    relevant_clauses=result_data.get("relevant_clauses", []),
                    confidence_score=result_data.get("confidence_score", 0.5),
                    audit_trail=audit_trail
                )
                
            except (json.JSONDecodeError, TypeError, AttributeError) as e:
                # Fallback response
                return QueryResult(
                    decision="conditional",
                    amount=None,
                    justification=f"Unable to parse LLM response: {str(e)}. Raw response: {str(response)[:200]}...",
                    relevant_clauses=[],
                    confidence_score=0.3,
                    audit_trail={
                        "timestamp": datetime.now().isoformat(),
                        "query": query,
                        "error": f"JSON parsing failed: {str(e)}",
                        "raw_response": str(response)[:500]
                    }
                )
                
        except Exception as e:
            logger.error(f"Error in LLM reasoning: {e}")
            return QueryResult(
                decision="conditional",
                amount=None,
                justification=f"Error in analysis: {str(e)}",
                relevant_clauses=[],
                confidence_score=0.0,
                audit_trail={
                    "timestamp": datetime.now().isoformat(),
                    "query": query,
                    "error": str(e)
                }
            )

class RAGSystem:
    """GPU-Optimized RAG system orchestrating all components"""
    
    def __init__(self, model_path: str = "./mistral-7b-instruct-v0.1.Q4_K_M.gguf", use_gpu: bool = True):
        # Initialize components
        self.document_processor = DocumentProcessor()
        
        # Initialize vector database with GPU optimization
        try:
            self.vector_db = VectorDatabase(use_gpu=use_gpu)
            logger.info(f"Vector database initialized successfully (GPU: {use_gpu})")
        except Exception as e:
            logger.error(f"Failed to initialize vector database: {e}")
            raise
        
        # Initialize LLM with GPU optimization
        try:
            logger.info(f"Loading model from: {model_path} (GPU: {use_gpu})")
            
            from llama_cpp import Llama
            
            # GPU-optimized configuration
            if use_gpu:
                self.llm = Llama(
                    model_path=model_path,
                    n_ctx=4096,
                    n_threads=8,  # More threads for GPU
                    n_gpu_layers=35,  # Use GPU layers
                    verbose=False,
                    use_mmap=True,
                    use_mlock=False,
                    seed=42
                )
                logger.info("GPU-optimized LLM model loaded successfully")
            else:
                # CPU fallback
                self.llm = Llama(
                    model_path=model_path,
                    n_ctx=4096,
                    n_threads=4,
                    n_gpu_layers=0,
                    verbose=False,
                    use_mmap=True,
                    use_mlock=False,
                    seed=42
                )
                logger.info("CPU LLM model loaded successfully")
            
        except Exception as e:
            logger.warning(f"Could not load local model: {e}")
            logger.info("Falling back to Gemini model")
            try:
                import google.generativeai as genai
                genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
                self.llm = genai.GenerativeModel("gemini-1.5-pro")
                logger.info("Gemini model loaded successfully")
            except ImportError:
                logger.error("Google Generative AI not available. Install with: pip install google-generativeai")
                raise Exception("No LLM model available. Please install google-generativeai or ensure local model file exists.")
            except Exception as gemini_error:
                logger.error(f"Failed to load both local and Gemini models: {gemini_error}")
                raise Exception("No LLM model available. Please check model file or API key.")
        
        self.query_parser = QueryParser(self.llm)
        self.reasoning_engine = LLMReasoning(self.llm)
        
        # Audit trail storage
        self.audit_log = []
    
    def ingest_document(self, file_path: str, use_ocr: bool = False) -> List[DocumentChunk]:
        """Ingest and process a document"""
        try:
            file_path = Path(file_path)
            
            # Extract text based on file type
            if file_path.suffix.lower() == '.pdf':
                text = self.document_processor.extract_text_from_pdf(str(file_path), use_ocr)
            elif file_path.suffix.lower() == '.docx':
                text = self.document_processor.extract_text_from_docx(str(file_path))
            elif file_path.suffix.lower() == '.html':
                text = self.document_processor.extract_text_from_html(str(file_path))
            elif file_path.suffix.lower() == '.eml':
                text = self.document_processor.extract_text_from_email(str(file_path))
            else:
                raise ValueError(f"Unsupported file type: {file_path.suffix}")
            
            # Chunk the document
            chunks = self.document_processor.chunk_document(text, str(file_path))
            
            # Add to vector database
            self.vector_db.add_documents(chunks)
            
            logger.info(f"Successfully ingested {len(chunks)} chunks from {file_path}")
            return chunks
            
        except Exception as e:
            logger.error(f"Error ingesting document {file_path}: {e}")
            raise
    
    def process_query(self, query: str) -> QueryResult:
        """Process a natural language query"""
        try:
            # Step 1: Parse and structure the query
            parsed_entities = self.query_parser.extract_entities(query)
            
            # Step 2: Semantic retrieval
            relevant_chunks = self.vector_db.search(query, k=5)
            
            # Step 3: LLM reasoning and decision logic
            result = self.reasoning_engine.analyze_query(query, relevant_chunks, parsed_entities)
            
            # Step 4: Store audit trail
            self.audit_log.append(result.audit_trail)
            
            return result
            
        except Exception as e:
            logger.error(f"Error processing query: {e}")
            return QueryResult(
                decision="error",
                amount=None,
                justification=f"System error: {str(e)}",
                relevant_clauses=[],
                confidence_score=0.0,
                audit_trail={
                    "timestamp": datetime.now().isoformat(),
                    "query": query,
                    "error": str(e)
                }
            )
    
    def get_audit_trail(self) -> List[Dict]:
        """Get complete audit trail"""
        return self.audit_log
    
    def save_audit_trail(self, file_path: str):
        """Save audit trail to file"""
        try:
            with open(file_path, 'w') as f:
                json.dump(self.audit_log, f, indent=2)
            logger.info(f"Audit trail saved to {file_path}")
        except Exception as e:
            logger.error(f"Error saving audit trail: {e}")

# Example usage
if __name__ == "__main__":
    # Initialize GPU-optimized RAG system
    rag_system = RAGSystem(use_gpu=True)  # Set to False for CPU-only
    
    # Ingest sample document
    print("Ingesting sample document...")
    chunks = rag_system.ingest_document("sample.pdf", use_ocr=False)
    
    # Process example queries
    example_queries = [
        "Is heart surgery covered under this policy?",
        "What is the waiting period for pre-existing diseases?",
        "Can I claim for dental treatment?",
        "What is the maximum coverage amount?"
    ]
    
    print("\nProcessing queries...")
    for query in example_queries:
        print(f"\nQuery: {query}")
        result = rag_system.process_query(query)
        print(f"Decision: {result.decision}")
        print(f"Justification: {result.justification}")
        print(f"Confidence: {result.confidence_score}")
    
    # Save audit trail
    rag_system.save_audit_trail("audit_trail.json")