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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
HYPER-OPTIMIZED RAG SYSTEM
Combines all advanced optimizations for 10x+ performance.
"""
import time
import numpy as np
from typing import List, Tuple, Optional, Dict, Any
from pathlib import Path
import logging
from dataclasses import dataclass
import asyncio
from concurrent.futures import ThreadPoolExecutor

from app.hyper_config import config
from app.ultra_fast_embeddings import get_embedder, UltraFastONNXEmbedder
from app.ultra_fast_llm import get_llm, UltraFastLLM, GenerationResult
from app.semantic_cache import get_semantic_cache, SemanticCache
import faiss
import sqlite3
import hashlib
import json

logger = logging.getLogger(__name__)

@dataclass
class HyperRAGResult:
    answer: str
    latency_ms: float
    memory_mb: float
    chunks_used: int
    cache_hit: bool
    cache_type: Optional[str]
    optimization_stats: Dict[str, Any]

class HyperOptimizedRAG:
    """
    Hyper-optimized RAG system combining all advanced techniques.
    
    Features:
    - Ultra-fast ONNX embeddings
    - vLLM-powered LLM inference
    - Semantic caching
    - Hybrid filtering (keyword + semantic)
    - Adaptive chunk retrieval
    - Prompt compression & summarization
    - Real-time performance optimization
    - Distributed cache ready
    """
    
    def __init__(self, metrics_tracker=None):
        self.metrics_tracker = metrics_tracker
        
        # Core components
        self.embedder: Optional[UltraFastONNXEmbedder] = None
        self.llm: Optional[UltraFastLLM] = None
        self.semantic_cache: Optional[SemanticCache] = None
        self.faiss_index = None
        self.docstore_conn = None
        
        # Performance optimizers
        self.thread_pool = ThreadPoolExecutor(max_workers=4)
        self._initialized = False
        
        # Adaptive parameters
        self.query_complexity_thresholds = {
            "simple": 5,    # words
            "medium": 15,
            "complex": 30
        }
        
        # Performance tracking
        self.total_queries = 0
        self.cache_hits = 0
        self.avg_latency_ms = 0
        
        logger.info("🚀 Initializing HyperOptimizedRAG")
    
    async def initialize_async(self):
        """Async initialization of all components."""
        if self._initialized:
            return
        
        logger.info("🔄 Async initialization started...")
        start_time = time.perf_counter()
        
        # Initialize components in parallel
        init_tasks = [
            self._init_embedder(),
            self._init_llm(),
            self._init_cache(),
            self._init_vector_store(),
            self._init_document_store()
        ]
        
        await asyncio.gather(*init_tasks)
        
        init_time = (time.perf_counter() - start_time) * 1000
        logger.info(f"✅ HyperOptimizedRAG initialized in {init_time:.1f}ms")
        self._initialized = True
    
    async def _init_embedder(self):
        """Initialize ultra-fast embedder."""
        logger.info("   Initializing UltraFastONNXEmbedder...")
        self.embedder = get_embedder()
        # Embedder initializes on first use
    
    async def _init_llm(self):
        """Initialize ultra-fast LLM."""
        logger.info("   Initializing UltraFastLLM...")
        self.llm = get_llm()
        # LLM initializes on first use
    
    async def _init_cache(self):
        """Initialize semantic cache."""
        logger.info("   Initializing SemanticCache...")
        self.semantic_cache = get_semantic_cache()
        self.semantic_cache.initialize()
    
    async def _init_vector_store(self):
        """Initialize FAISS vector store."""
        logger.info("   Loading FAISS index...")
        faiss_path = config.data_dir / "faiss_index.bin"
        if faiss_path.exists():
            self.faiss_index = faiss.read_index(str(faiss_path))
            logger.info(f"   FAISS index loaded: {self.faiss_index.ntotal} vectors")
        else:
            logger.warning("   FAISS index not found")
    
    async def _init_document_store(self):
        """Initialize document store."""
        logger.info("   Connecting to document store...")
        db_path = config.data_dir / "docstore.db"
        self.docstore_conn = sqlite3.connect(db_path)
    
    def initialize(self):
        """Synchronous initialization wrapper."""
        if not self._initialized:
            asyncio.run(self.initialize_async())
    
    async def query_async(self, question: str, **kwargs) -> HyperRAGResult:
        """
        Async query processing with all optimizations.
        
        Returns:
            HyperRAGResult with answer and comprehensive metrics
        """
        if not self._initialized:
            await self.initialize_async()
        
        start_time = time.perf_counter()
        memory_start = self._get_memory_usage()
        
        # Track optimization stats
        stats = {
            "query_length": len(question.split()),
            "cache_attempted": False,
            "cache_hit": False,
            "cache_type": None,
            "embedding_time_ms": 0,
            "filtering_time_ms": 0,
            "retrieval_time_ms": 0,
            "generation_time_ms": 0,
            "compression_ratio": 1.0,
            "chunks_before_filter": 0,
            "chunks_after_filter": 0
        }
        
        # Step 0: Check semantic cache
        cache_start = time.perf_counter()
        cached_result = self.semantic_cache.get(question)
        cache_time = (time.perf_counter() - cache_start) * 1000
        
        if cached_result:
            stats["cache_attempted"] = True
            stats["cache_hit"] = True
            stats["cache_type"] = "exact"
            
            answer, chunks_used = cached_result
            total_time = (time.perf_counter() - start_time) * 1000
            memory_used = self._get_memory_usage() - memory_start
            
            logger.info(f"🎯 Semantic cache HIT: {total_time:.1f}ms")
            
            self.cache_hits += 1
            self.total_queries += 1
            self.avg_latency_ms = (self.avg_latency_ms * (self.total_queries - 1) + total_time) / self.total_queries
            
            return HyperRAGResult(
                answer=answer,
                latency_ms=total_time,
                memory_mb=memory_used,
                chunks_used=len(chunks_used),
                cache_hit=True,
                cache_type="semantic",
                optimization_stats=stats
            )
        
        # Step 1: Parallel embedding and filtering
        embed_task = asyncio.create_task(self._embed_query(question))
        filter_task = asyncio.create_task(self._filter_query(question))
        
        embedding_result, filter_result = await asyncio.gather(embed_task, filter_task)
        
        query_embedding, embed_time = embedding_result
        filter_ids, filter_time = filter_result
        
        stats["embedding_time_ms"] = embed_time
        stats["filtering_time_ms"] = filter_time
        
        # Step 2: Adaptive retrieval
        retrieval_start = time.perf_counter()
        chunk_ids = await self._retrieve_chunks_adaptive(
            query_embedding, 
            question, 
            filter_ids
        )
        stats["retrieval_time_ms"] = (time.perf_counter() - retrieval_start) * 1000
        
        # Step 3: Retrieve chunks with compression
        chunks = await self._retrieve_chunks_with_compression(chunk_ids, question)
        
        if not chunks:
            # No relevant chunks found
            answer = "I don't have enough relevant information to answer that question."
            chunks_used = 0
        else:
            # Step 4: Generate answer with ultra-fast LLM
            generation_start = time.perf_counter()
            answer = await self._generate_answer(question, chunks)
            stats["generation_time_ms"] = (time.perf_counter() - generation_start) * 1000
        
        # Step 5: Cache the result
        if chunks:
            await self._cache_result_async(question, answer, chunks)
        
        # Calculate final metrics
        total_time = (time.perf_counter() - start_time) * 1000
        memory_used = self._get_memory_usage() - memory_start
        
        # Update performance tracking
        self.total_queries += 1
        self.avg_latency_ms = (self.avg_latency_ms * (self.total_queries - 1) + total_time) / self.total_queries
        
        # Log performance
        logger.info(f"⚡ Query processed in {total_time:.1f}ms "
                   f"(embed: {embed_time:.1f}ms, "
                   f"filter: {filter_time:.1f}ms, "
                   f"retrieve: {stats['retrieval_time_ms']:.1f}ms, "
                   f"generate: {stats['generation_time_ms']:.1f}ms)")
        
        return HyperRAGResult(
            answer=answer,
            latency_ms=total_time,
            memory_mb=memory_used,
            chunks_used=len(chunks) if chunks else 0,
            cache_hit=False,
            cache_type=None,
            optimization_stats=stats
        )
    
    async def _embed_query(self, question: str) -> Tuple[np.ndarray, float]:
        """Embed query with ultra-fast ONNX embedder."""
        start = time.perf_counter()
        embedding = self.embedder.embed_single(question)
        time_ms = (time.perf_counter() - start) * 1000
        return embedding, time_ms
    
    async def _filter_query(self, question: str) -> Tuple[Optional[List[int]], float]:
        """Apply hybrid filtering to query."""
        if not config.enable_hybrid_filter:
            return None, 0.0
        
        start = time.perf_counter()
        
        # Keyword filtering
        keyword_ids = await self._keyword_filter(question)
        
        # Semantic filtering if enabled
        semantic_ids = None
        if config.enable_semantic_filter and self.embedder and self.faiss_index:
            semantic_ids = await self._semantic_filter(question)
        
        # Combine filters
        if keyword_ids and semantic_ids:
            # Intersection of both filters
            filter_ids = list(set(keyword_ids) & set(semantic_ids))
        elif keyword_ids:
            filter_ids = keyword_ids
        elif semantic_ids:
            filter_ids = semantic_ids
        else:
            filter_ids = None
        
        time_ms = (time.perf_counter() - start) * 1000
        return filter_ids, time_ms
    
    async def _keyword_filter(self, question: str) -> Optional[List[int]]:
        """Apply keyword filtering."""
        # Simplified implementation
        # In production, use proper keyword extraction and indexing
        import re
        from collections import defaultdict
        
        # Get all chunks
        cursor = self.docstore_conn.cursor()
        cursor.execute("SELECT id, chunk_text FROM chunks")
        chunks = cursor.fetchall()
        
        # Build simple keyword index
        keyword_index = defaultdict(list)
        for chunk_id, text in chunks:
            words = set(re.findall(r'\b\w{3,}\b', text.lower()))
            for word in words:
                keyword_index[word].append(chunk_id)
        
        # Extract question keywords
        question_words = set(re.findall(r'\b\w{3,}\b', question.lower()))
        
        # Find matching chunks
        candidate_ids = set()
        for word in question_words:
            if word in keyword_index:
                candidate_ids.update(keyword_index[word])
        
        return list(candidate_ids) if candidate_ids else None
    
    async def _semantic_filter(self, question: str) -> Optional[List[int]]:
        """Apply semantic filtering using embeddings."""
        if not self.faiss_index or not self.embedder:
            return None
        
        # Get query embedding
        query_embedding = self.embedder.embed_single(question)
        query_embedding = query_embedding.astype(np.float32).reshape(1, -1)
        
        # Search with threshold
        distances, indices = self.faiss_index.search(
            query_embedding, 
            min(100, self.faiss_index.ntotal)  # Limit candidates
        )
        
        # Filter by similarity threshold
        filtered_indices = []
        for dist, idx in zip(distances[0], indices[0]):
            if idx >= 0:
                similarity = 1.0 / (1.0 + dist)
                if similarity >= config.filter_threshold:
                    filtered_indices.append(idx + 1)  # Convert to 1-based
        
        return filtered_indices if filtered_indices else None
    
    async def _retrieve_chunks_adaptive(
        self, 
        query_embedding: np.ndarray, 
        question: str, 
        filter_ids: Optional[List[int]]
    ) -> List[int]:
        """Retrieve chunks with adaptive top-k based on query complexity."""
        # Determine top-k based on query complexity
        words = len(question.split())
        
        if words < self.query_complexity_thresholds["simple"]:
            top_k = config.dynamic_top_k["simple"]
        elif words < self.query_complexity_thresholds["medium"]:
            top_k = config.dynamic_top_k["medium"]
        elif words < self.query_complexity_thresholds["complex"]:
            top_k = config.dynamic_top_k["complex"]
        else:
            top_k = config.dynamic_top_k.get("expert", 8)
        
        # Adjust based on filter results
        if filter_ids:
            # If filtering greatly reduces candidates, adjust top_k
            if len(filter_ids) < top_k * 2:
                top_k = min(top_k, len(filter_ids))
        
        # Perform retrieval
        if self.faiss_index is None:
            return []
        
        query_embedding = query_embedding.astype(np.float32).reshape(1, -1)
        
        if filter_ids:
            # Post-filtering approach
            expanded_k = min(top_k * 3, len(filter_ids))
            distances, indices = self.faiss_index.search(query_embedding, expanded_k)
            
            # Convert and filter
            faiss_results = [int(idx + 1) for idx in indices[0] if idx >= 0]
            filtered_results = [idx for idx in faiss_results if idx in filter_ids]
            
            return filtered_results[:top_k]
        else:
            # Standard retrieval
            distances, indices = self.faiss_index.search(query_embedding, top_k)
            return [int(idx + 1) for idx in indices[0] if idx >= 0]
    
    async def _retrieve_chunks_with_compression(
        self, 
        chunk_ids: List[int], 
        question: str
    ) -> List[str]:
        """Retrieve and compress chunks based on relevance to question."""
        if not chunk_ids:
            return []
        
        # Retrieve chunks
        cursor = self.docstore_conn.cursor()
        placeholders = ','.join('?' for _ in chunk_ids)
        query = f"SELECT id, chunk_text FROM chunks WHERE id IN ({placeholders})"
        cursor.execute(query, chunk_ids)
        chunks = [(row[0], row[1]) for row in cursor.fetchall()]
        
        if not chunks:
            return []
        
        # Sort by relevance (simplified - in production use embedding similarity)
        # For now, just return top chunks
        max_chunks = min(5, len(chunks))  # Limit to 5 chunks
        return [chunk_text for _, chunk_text in chunks[:max_chunks]]
    
    async def _generate_answer(self, question: str, chunks: List[str]) -> str:
        """Generate answer using ultra-fast LLM."""
        if not self.llm:
            # Fallback to simple response
            context = "\n\n".join(chunks[:3])
            return f"Based on the context: {context[:300]}..."
        
        # Create optimized prompt
        prompt = self._create_optimized_prompt(question, chunks)
        
        # Generate with ultra-fast LLM
        try:
            result = self.llm.generate(
                prompt=prompt,
                max_tokens=config.llm_max_tokens,
                temperature=config.llm_temperature,
                top_p=config.llm_top_p
            )
            return result.text
        except Exception as e:
            logger.error(f"LLM generation failed: {e}")
            # Fallback
            context = "\n\n".join(chunks[:3])
            return f"Based on the context: {context[:300]}..."
    
    def _create_optimized_prompt(self, question: str, chunks: List[str]) -> str:
        """Create optimized prompt with compression."""
        if not chunks:
            return f"Question: {question}\n\nAnswer: I don't have enough information."
        
        # Simple prompt template
        context = "\n\n".join(chunks[:3])  # Use top 3 chunks
        
        prompt = f"""Context information:
{context}

Based on the context above, answer the following question concisely and accurately:
Question: {question}

Answer: """
        
        return prompt
    
    async def _cache_result_async(self, question: str, answer: str, chunks: List[str]):
        """Cache the result asynchronously."""
        if self.semantic_cache:
            # Run in thread pool to avoid blocking
            await asyncio.get_event_loop().run_in_executor(
                self.thread_pool,
                lambda: self.semantic_cache.put(
                    question=question,
                    answer=answer,
                    chunks_used=chunks,
                    metadata={
                        "timestamp": time.time(),
                        "chunk_count": len(chunks),
                        "query_length": len(question)
                    },
                    ttl_seconds=config.cache_ttl_seconds
                )
            )
    
    def _get_memory_usage(self) -> float:
        """Get current memory usage in MB."""
        import psutil
        import os
        process = psutil.Process(os.getpid())
        return process.memory_info().rss / 1024 / 1024
    
    def get_performance_stats(self) -> Dict[str, Any]:
        """Get performance statistics."""
        cache_stats = self.semantic_cache.get_stats() if self.semantic_cache else {}
        
        return {
            "total_queries": self.total_queries,
            "cache_hits": self.cache_hits,
            "cache_hit_rate": self.cache_hits / self.total_queries if self.total_queries > 0 else 0,
            "avg_latency_ms": self.avg_latency_ms,
            "embedder_stats": self.embedder.get_performance_stats() if self.embedder else {},
            "llm_stats": self.llm.get_performance_stats() if self.llm else {},
            "cache_stats": cache_stats
        }
    
    def query(self, question: str, **kwargs) -> HyperRAGResult:
        """Synchronous query wrapper."""
        return asyncio.run(self.query_async(question, **kwargs))
    
    async def close_async(self):
        """Async cleanup."""
        if self.thread_pool:
            self.thread_pool.shutdown(wait=True)
        
        if self.docstore_conn:
            self.docstore_conn.close()
    
    def close(self):
        """Synchronous cleanup."""
        asyncio.run(self.close_async())

# Test function
if __name__ == "__main__":
    import logging
    logging.basicConfig(level=logging.INFO)
    
    print("\n" + "=" * 60)
    print("🧪 TESTING HYPER-OPTIMIZED RAG SYSTEM")
    print("=" * 60)
    
    # Create instance
    rag = HyperOptimizedRAG()
    
    print("\n🔄 Initializing...")
    rag.initialize()
    
    # Test queries
    test_queries = [
        "What is machine learning?",
        "Explain artificial intelligence",
        "How does deep learning work?",
        "What are neural networks?"
    ]
    
    print("\n⚡ Running performance test...")
    
    for i, query in enumerate(test_queries, 1):
        print(f"\nQuery {i}: {query}")
        
        result = rag.query(query)
        
        print(f"  Answer: {result.answer[:100]}...")
        print(f"  Latency: {result.latency_ms:.1f}ms")
        print(f"  Memory: {result.memory_mb:.1f}MB")
        print(f"  Chunks used: {result.chunks_used}")
        print(f"  Cache hit: {result.cache_hit}")
        
        if result.optimization_stats:
            print(f"  Embedding: {result.optimization_stats['embedding_time_ms']:.1f}ms")
            print(f"  Generation: {result.optimization_stats['generation_time_ms']:.1f}ms")
    
    # Get performance stats
    print("\n📊 Performance Statistics:")
    stats = rag.get_performance_stats()
    
    for key, value in stats.items():
        if isinstance(value, dict):
            print(f"\n  {key}:")
            for subkey, subvalue in value.items():
                print(f"    {subkey}: {subvalue}")
        else:
            print(f"  {key}: {value}")
    
    # Cleanup
    rag.close()
    print("\n✅ Test complete!")