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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
"""
Cross-encoder re-ranking module for improving search result relevance.
"""

import time
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from tqdm import tqdm

from .error_handler import EmbeddingError, ResourceError
from .search_engine import SearchResult


class CrossEncoderReranker:
    """Cross-encoder model for re-ranking search results."""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self.reranker_config = config.get("models", {}).get("reranker", {})
        
        # Configuration
        self.model_name = self.reranker_config.get("name", "cross-encoder/ms-marco-MiniLM-L-6-v2")
        self.max_seq_length = self.reranker_config.get("max_seq_length", 512)
        self.batch_size = self.reranker_config.get("batch_size", 16)
        self.enabled = self.reranker_config.get("enabled", True)
        self.device = self._get_device()
        
        # Model components
        self.tokenizer: Optional[AutoTokenizer] = None
        self.model: Optional[AutoModelForSequenceClassification] = None
        self._model_loaded = False
        
        # Performance tracking
        self.stats = {
            "reranking_operations": 0,
            "total_pairs_scored": 0,
            "total_time": 0,
            "model_load_time": 0,
            "avg_batch_size": 0
        }
    
    def _get_device(self) -> str:
        """Determine the best device for computation."""
        if torch.cuda.is_available():
            return "cuda"
        elif torch.backends.mps.is_available():  # Apple Silicon
            return "mps"
        else:
            return "cpu"
    
    def _load_model(self) -> None:
        """Lazy load the cross-encoder model."""
        if not self.enabled:
            print("Re-ranker is disabled in configuration")
            return
        
        if self._model_loaded:
            return
        
        try:
            print(f"Loading re-ranker model: {self.model_name}")
            start_time = time.time()
            
            # Load tokenizer and model
            self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
            self.model = AutoModelForSequenceClassification.from_pretrained(self.model_name)
            
            # Move to device
            if self.device != "cpu":
                self.model = self.model.to(self.device)
            
            # Set to evaluation mode
            self.model.eval()
            
            load_time = time.time() - start_time
            self.stats["model_load_time"] = load_time
            
            print(f"Re-ranker model loaded in {load_time:.2f}s on device: {self.device}")
            self._model_loaded = True
            
        except Exception as e:
            print(f"Failed to load re-ranker model: {e}")
            self.enabled = False
            raise EmbeddingError(f"Failed to load re-ranker model: {str(e)}") from e
    
    def rerank(
        self,
        query: str,
        results: List[SearchResult],
        top_k: int = None
    ) -> List[SearchResult]:
        """
        Re-rank search results using cross-encoder scores.
        
        Args:
            query: Original search query
            results: List of search results to re-rank
            top_k: Number of top results to return after re-ranking
            
        Returns:
            Re-ranked list of SearchResult objects
        """
        if not self.enabled or not results:
            return results
        
        if not query or not query.strip():
            return results
        
        start_time = time.time()
        
        try:
            # Load model if needed
            self._load_model()
            
            if not self._model_loaded:
                print("Re-ranker model not available, returning original results")
                return results
            
            # Prepare query-document pairs
            pairs = []
            for result in results:
                # Use content or a reasonable excerpt
                content = result.content
                if len(content) > 500:  # Truncate very long content
                    content = content[:500] + "..."
                
                pairs.append((query.strip(), content))
            
            # Score pairs
            scores = self._score_pairs(pairs)
            
            # Normalize reranker scores to 0-1 range
            if scores and len(scores) > 0:
                min_score = min(scores)
                max_score = max(scores)
                if max_score > min_score:
                    # Normalize to 0-1 range
                    normalized_scores = [(score - min_score) / (max_score - min_score) for score in scores]
                else:
                    # All scores are the same, set to 0.5
                    normalized_scores = [0.5] * len(scores)
            else:
                normalized_scores = scores
            
            # Update results with re-ranking scores
            reranked_results = []
            for i, result in enumerate(results):
                # Create new result with updated scores
                reranked_result = SearchResult(
                    chunk_id=result.chunk_id,
                    content=result.content,
                    metadata=result.metadata,
                    vector_score=result.vector_score,
                    bm25_score=result.bm25_score,
                    final_score=float(normalized_scores[i]),  # Use normalized re-ranker score
                    rank=0  # Will be updated after sorting
                )
                reranked_results.append(reranked_result)
            
            # Sort by re-ranking scores
            reranked_results.sort(key=lambda x: x.final_score, reverse=True)
            
            # Update ranks
            for i, result in enumerate(reranked_results):
                result.rank = i + 1
            
            # Apply top_k limit
            if top_k is not None:
                reranked_results = reranked_results[:top_k]
            
            # Update statistics
            reranking_time = time.time() - start_time
            self.stats["reranking_operations"] += 1
            self.stats["total_pairs_scored"] += len(pairs)
            self.stats["total_time"] += reranking_time
            
            return reranked_results
            
        except Exception as e:
            print(f"Re-ranking failed, returning original results: {e}")
            return results
    
    def _score_pairs(self, pairs: List[Tuple[str, str]]) -> np.ndarray:
        """Score query-document pairs using the cross-encoder."""
        if not pairs:
            return np.array([])
        
        scores = []
        
        # Process in batches
        for i in range(0, len(pairs), self.batch_size):
            batch_pairs = pairs[i:i + self.batch_size]
            batch_scores = self._score_batch(batch_pairs)
            scores.extend(batch_scores)
        
        return np.array(scores)
    
    def _score_batch(self, batch_pairs: List[Tuple[str, str]]) -> List[float]:
        """Score a batch of query-document pairs."""
        try:
            # Prepare inputs
            queries = [pair[0] for pair in batch_pairs]
            documents = [pair[1] for pair in batch_pairs]
            
            # Tokenize
            inputs = self.tokenizer(
                queries,
                documents,
                padding=True,
                truncation=True,
                max_length=self.max_seq_length,
                return_tensors="pt"
            )
            
            # Move to device
            if self.device != "cpu":
                inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            # Get predictions
            with torch.no_grad():
                outputs = self.model(**inputs)
                
                # Get logits and convert to scores
                logits = outputs.logits
                
                # For binary classification models, use sigmoid on logits
                # For regression models, use logits directly
                if logits.size(-1) == 1:
                    # Regression output
                    scores = logits.squeeze(-1).cpu().numpy()
                else:
                    # Classification output - use positive class probability
                    probs = torch.softmax(logits, dim=-1)
                    scores = probs[:, 1].cpu().numpy()  # Positive class
            
            return scores.tolist()
            
        except torch.cuda.OutOfMemoryError as e:
            raise ResourceError(
                "GPU memory insufficient for re-ranking. "
                "Try reducing batch_size or using CPU."
            ) from e
        except Exception as e:
            raise EmbeddingError(f"Failed to score batch: {str(e)}") from e
    
    def score_single_pair(self, query: str, document: str) -> float:
        """Score a single query-document pair."""
        if not self.enabled or not query.strip() or not document.strip():
            return 0.0
        
        try:
            scores = self._score_pairs([(query, document)])
            return float(scores[0]) if len(scores) > 0 else 0.0
        except Exception as e:
            print(f"Failed to score single pair: {e}")
            return 0.0
    
    def warmup(self) -> None:
        """Warm up the re-ranker with a sample query-document pair."""
        if not self.enabled:
            return
        
        self._load_model()
        
        if not self._model_loaded:
            return
        
        # Run a sample prediction to warm up
        sample_pairs = [("sample query", "sample document text")]
        try:
            self._score_pairs(sample_pairs)
            print("Re-ranker model warmed up successfully")
        except Exception as e:
            print(f"Re-ranker warmup failed: {e}")
    
    def get_stats(self) -> Dict[str, Any]:
        """Get re-ranker performance statistics."""
        stats = self.stats.copy()
        
        if stats["reranking_operations"] > 0:
            stats["avg_time_per_operation"] = stats["total_time"] / stats["reranking_operations"]
            stats["avg_pairs_per_operation"] = stats["total_pairs_scored"] / stats["reranking_operations"]
        else:
            stats["avg_time_per_operation"] = 0
            stats["avg_pairs_per_operation"] = 0
        
        if stats["total_pairs_scored"] > 0:
            stats["avg_time_per_pair"] = stats["total_time"] / stats["total_pairs_scored"]
        else:
            stats["avg_time_per_pair"] = 0
        
        stats["model_loaded"] = self._model_loaded
        stats["enabled"] = self.enabled
        stats["device"] = self.device
        stats["model_name"] = self.model_name
        
        return stats
    
    def unload_model(self) -> None:
        """Unload the model to free memory."""
        if self.model is not None:
            del self.model
            del self.tokenizer
            self.model = None
            self.tokenizer = None
            self._model_loaded = False
            
            # Clear GPU cache if using CUDA
            if torch.cuda.is_available():
                torch.cuda.empty_cache()
            
            print("Re-ranker model unloaded")
    
    def is_available(self) -> bool:
        """Check if re-ranker is available and enabled."""
        return self.enabled and self._model_loaded


class RerankingPipeline:
    """Pipeline for applying re-ranking with fallback options."""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self.search_config = config.get("search", {})
        
        # Re-ranking configuration
        self.rerank_top_k = self.search_config.get("rerank_top_k", 50)
        self.final_top_k = self.search_config.get("final_top_k", 10)
        self.enable_reranking = config.get("models", {}).get("reranker", {}).get("enabled", True)
        
        # Initialize re-ranker
        self.reranker = CrossEncoderReranker(config) if self.enable_reranking else None
        
        # Statistics
        self.stats = {
            "pipeline_calls": 0,
            "reranking_applied": 0,
            "fallback_used": 0,
            "avg_input_results": 0,
            "avg_output_results": 0
        }
    
    def process(
        self,
        query: str,
        results: List[SearchResult],
        apply_reranking: bool = True
    ) -> List[SearchResult]:
        """
        Process search results through the re-ranking pipeline.
        
        Args:
            query: Original search query
            results: Search results to process
            apply_reranking: Whether to apply re-ranking
            
        Returns:
            Processed results (re-ranked if enabled and successful)
        """
        if not results:
            return results
        
        start_input_count = len(results)
        self.stats["pipeline_calls"] += 1
        self.stats["avg_input_results"] = (
            (self.stats["avg_input_results"] * (self.stats["pipeline_calls"] - 1) + start_input_count)
            / self.stats["pipeline_calls"]
        )
        
        # Apply re-ranking if enabled and requested
        if (apply_reranking and 
            self.enable_reranking and 
            self.reranker is not None and 
            len(results) > 1):
            
            try:
                # Limit candidates for re-ranking to improve performance
                candidates = results[:self.rerank_top_k]
                
                # Apply re-ranking
                reranked_results = self.reranker.rerank(query, candidates)
                
                # Combine with remaining results if any
                if len(results) > self.rerank_top_k:
                    remaining_results = results[self.rerank_top_k:]
                    # Adjust ranks for remaining results
                    for i, result in enumerate(remaining_results):
                        result.rank = len(reranked_results) + i + 1
                    
                    final_results = reranked_results + remaining_results
                else:
                    final_results = reranked_results
                
                self.stats["reranking_applied"] += 1
                
            except Exception as e:
                print(f"Re-ranking failed, using original results: {e}")
                final_results = results
                self.stats["fallback_used"] += 1
        else:
            final_results = results
        
        # Apply final top-k limit
        final_results = final_results[:self.final_top_k]
        
        # Update output statistics
        output_count = len(final_results)
        self.stats["avg_output_results"] = (
            (self.stats["avg_output_results"] * (self.stats["pipeline_calls"] - 1) + output_count)
            / self.stats["pipeline_calls"]
        )
        
        return final_results
    
    def get_stats(self) -> Dict[str, Any]:
        """Get pipeline statistics."""
        stats = self.stats.copy()
        
        if self.reranker:
            stats["reranker_stats"] = self.reranker.get_stats()
        
        stats["reranking_enabled"] = self.enable_reranking
        stats["rerank_top_k"] = self.rerank_top_k
        stats["final_top_k"] = self.final_top_k
        
        if stats["pipeline_calls"] > 0:
            stats["reranking_success_rate"] = stats["reranking_applied"] / stats["pipeline_calls"]
            stats["fallback_rate"] = stats["fallback_used"] / stats["pipeline_calls"]
        else:
            stats["reranking_success_rate"] = 0
            stats["fallback_rate"] = 0
        
        return stats
    
    def warmup(self) -> None:
        """Warm up the re-ranking pipeline."""
        if self.reranker:
            self.reranker.warmup()
    
    def unload_models(self) -> None:
        """Unload re-ranker models to free memory."""
        if self.reranker:
            self.reranker.unload_model()