File size: 12,063 Bytes
0ca462b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Intent Engine - Hybrid Embedding Cache (Redis + Local LRU)

This module provides a two-tier caching strategy:
- L1: Local in-memory LRU cache for ultra-fast access
- L2: Redis distributed cache for multi-instance deployments

Features:
- Automatic fallback to local cache if Redis unavailable
- Cache coherence across instances via Redis
- Configurable TTL for embeddings
- Batch processing for efficiency
- Thread-safe operations
"""

import hashlib
import logging
import os
import threading
from typing import Any

import numpy as np

from app.config.optimized_cache import LRUCache
from app.config.redis_cache import RedisCache, get_redis_cache

logger = logging.getLogger(__name__)


class HybridEmbeddingCache:
    """
    Two-tier embedding cache with Redis + local LRU.

    Cache lookup order:
    1. Check local L1 cache (fastest)
    2. Check Redis L2 cache (distributed)
    3. Compute embedding and store in both caches

    Cache invalidation:
    - Writes go to both caches
    - Redis TTL ensures automatic expiration
    - Local cache uses LRU eviction
    """

    def __init__(
        self,
        model_name: str = "sentence-transformers/all-MiniLM-L6-v2",
        local_capacity: int = 5000,
        redis_ttl: int = 3600,  # 1 hour
        use_redis: bool = True,
    ):
        """
        Initialize hybrid cache.

        Args:
            model_name: Sentence transformer model to use
            local_capacity: Local LRU cache capacity
            redis_ttl: Redis cache TTL in seconds
            use_redis: Whether to use Redis (falls back to local only if False)
        """
        self.model_name = model_name
        self.redis_ttl = redis_ttl
        self.use_redis = (
            use_redis and os.getenv("REDIS_ENABLED", "false").lower() == "true"
        )

        # L1: Local cache
        self.local_cache = LRUCache(capacity=local_capacity)

        # L2: Redis cache (optional)
        self.redis_cache: RedisCache | None = None
        if self.use_redis:
            self.redis_cache = get_redis_cache()
            if self.redis_cache is None:
                logger.warning("Redis not available, using local cache only")
                self.use_redis = False

        # Model
        self.model = None
        self.tokenizer = None
        self.batch_size = 32
        self._load_model()

        # Stats
        self._lock = threading.Lock()
        self.hits_l1 = 0
        self.hits_l2 = 0
        self.misses = 0

        logger.info(f"HybridEmbeddingCache initialized (Redis: {self.use_redis})")

    def _load_model(self) -> None:
        """Load the sentence transformer model."""
        try:
            from sentence_transformers import SentenceTransformer

            logger.info(f"Loading SentenceTransformer model: {self.model_name}...")

            self.model = SentenceTransformer(
                self.model_name,
                trust_remote_code=True,
            )
            self.model = self.model.to("cpu")
            self.model.eval()

            logger.info(
                f"Successfully loaded SentenceTransformer model: {self.model_name}"
            )
        except ImportError as e:
            logger.warning(
                f"SentenceTransformers library not available: {e}. Using mock embeddings."
            )
            self.model = None
        except Exception as e:
            logger.error(
                f"Error loading embedding model {self.model_name}: {e}. Using mock embeddings."
            )
            self.model = None

    def _get_cache_key(self, text: str) -> str:
        """Generate cache key for text."""
        text_hash = hashlib.md5(text.encode()).hexdigest()
        return f"embedding:{self.model_name}:{text_hash}"

    def encode_text(self, text: str, use_cache: bool = True) -> np.ndarray | None:
        """
        Encode single text to embedding with two-tier caching.

        Args:
            text: Text to encode
            use_cache: Whether to use caching

        Returns:
            Embedding vector or None
        """
        if not text or not text.strip():
            return None

        # Check L1 cache
        if use_cache:
            cached = self.local_cache.get(text)
            if cached is not None:
                with self._lock:
                    self.hits_l1 += 1
                return cached

        # Check L2 (Redis) cache
        if use_cache and self.use_redis and self.redis_cache:
            cache_key = self._get_cache_key(text)
            cached = self.redis_cache.get(cache_key)
            if cached is not None and isinstance(cached, np.ndarray):
                # Store in L1 for faster next access
                self.local_cache.put(text, cached)
                with self._lock:
                    self.hits_l2 += 1
                return cached

        # Cache miss - compute embedding
        with self._lock:
            self.misses += 1

        embedding = self._compute_embedding(text)
        if embedding is None:
            return None

        # Store in both caches
        if use_cache:
            self.local_cache.put(text, embedding)

            if self.use_redis and self.redis_cache:
                cache_key = self._get_cache_key(text)
                self.redis_cache.set(cache_key, embedding, ttl=self.redis_ttl)

        return embedding

    def encode_batch(
        self, texts: list[str], use_cache: bool = True
    ) -> list[np.ndarray]:
        """
        Encode multiple texts with two-tier caching.

        Args:
            texts: List of texts to encode
            use_cache: Whether to use caching

        Returns:
            List of embedding vectors
        """
        if not texts:
            return []

        results: list[np.ndarray | None] = [None] * len(texts)
        texts_to_encode: list[str] = []
        indices_to_encode: list[int] = []

        # Check caches for all texts
        for i, text in enumerate(texts):
            if not text or not text.strip():
                results[i] = np.random.rand(384).astype(np.float32)
                continue

            # Check L1 cache
            cached = self.local_cache.get(text)
            if cached is not None:
                results[i] = cached
                with self._lock:
                    self.hits_l1 += 1
                continue

            # Check L2 (Redis) cache
            if use_cache and self.use_redis and self.redis_cache:
                cache_key = self._get_cache_key(text)
                cached = self.redis_cache.get(cache_key)
                if cached is not None and isinstance(cached, np.ndarray):
                    self.local_cache.put(text, cached)  # Promote to L1
                    results[i] = cached
                    with self._lock:
                        self.hits_l2 += 1
                    continue

            # Need to encode
            texts_to_encode.append(text)
            indices_to_encode.append(i)

        # Encode remaining texts in batches
        if texts_to_encode and self.model is not None:
            try:
                embeddings = self.model.encode(
                    texts_to_encode,
                    convert_to_numpy=True,
                    normalize_embeddings=True,
                    show_progress_bar=False,
                    batch_size=self.batch_size,
                )

                # Store results and cache
                for idx, embedding in zip(indices_to_encode, embeddings, strict=False):
                    result = embedding.astype(np.float32)
                    results[idx] = result

                    if use_cache:
                        text = texts_to_encode[idx]
                        self.local_cache.put(text, result)

                        if self.use_redis and self.redis_cache:
                            cache_key = self._get_cache_key(text)
                            self.redis_cache.set(cache_key, result, ttl=self.redis_ttl)

            except Exception as e:
                logger.error(f"Error encoding batch: {e}")
                # Fallback to individual encoding
                for idx, text in zip(indices_to_encode, texts_to_encode, strict=False):
                    results[idx] = self.encode_text(text, use_cache)

        # Replace any remaining None placeholders
        for i, result in enumerate(results):
            if result is None:
                results[i] = np.random.rand(384).astype(np.float32)

        return results

    def _compute_embedding(self, text: str) -> np.ndarray | None:
        """Compute embedding for text."""
        if self.model is None:
            return np.random.rand(384).astype(np.float32)

        try:
            embedding = self.model.encode(
                text,
                convert_to_numpy=True,
                normalize_embeddings=True,
                show_progress_bar=False,
            )
            return embedding.astype(np.float32)
        except Exception as e:
            logger.error(f"Error computing embedding: {e}")
            return np.random.rand(384).astype(np.float32)

    def cosine_similarity(self, vec1: np.ndarray, vec2: np.ndarray) -> float:
        """Calculate cosine similarity between two vectors."""
        if vec1 is None or vec2 is None:
            return 0.0
        dot_product = np.dot(vec1, vec2)
        return float(np.clip(dot_product, -1.0, 1.0))

    def get_stats(self) -> dict[str, Any]:
        """Get cache statistics."""
        total = self.hits_l1 + self.hits_l2 + self.misses
        hit_rate_l1 = self.hits_l1 / total if total > 0 else 0
        hit_rate_l2 = self.hits_l2 / total if total > 0 else 0

        stats = {
            "model_name": self.model_name,
            "model_loaded": self.model is not None,
            "use_redis": self.use_redis,
            "local_cache": self.local_cache.get_stats(),
            "hits_l1": self.hits_l1,
            "hits_l2": self.hits_l2,
            "misses": self.misses,
            "hit_rate_l1": round(hit_rate_l1, 3),
            "hit_rate_l2": round(hit_rate_l2, 3),
            "total_requests": total,
        }

        if self.use_redis and self.redis_cache:
            stats["redis_cache"] = self.redis_cache.get_stats()

        return stats

    def clear(self) -> None:
        """Clear both caches."""
        self.local_cache.clear()

        if self.use_redis and self.redis_cache:
            # Clear only embedding keys
            keys = self.redis_cache.keys("embedding:*")
            if keys:
                self.redis_cache.delete_many(keys)
                logger.info(f"Cleared {len(keys)} embedding keys from Redis")

        with self._lock:
            self.hits_l1 = 0
            self.hits_l2 = 0
            self.misses = 0

        logger.info("Hybrid embedding cache cleared")


# Global instance
_embedding_cache_instance: HybridEmbeddingCache | None = None
_init_lock = threading.Lock()


def get_embedding_cache(
    model_name: str = "sentence-transformers/all-MiniLM-L6-v2",
    local_capacity: int = 5000,
    redis_ttl: int = 3600,
) -> HybridEmbeddingCache:
    """
    Get singleton instance of HybridEmbeddingCache.

    Args:
        model_name: Model name for initialization
        local_capacity: Local cache capacity
        redis_ttl: Redis TTL in seconds

    Returns:
        HybridEmbeddingCache instance
    """
    global _embedding_cache_instance

    if _embedding_cache_instance is None:
        with _init_lock:
            if _embedding_cache_instance is None:
                _embedding_cache_instance = HybridEmbeddingCache(
                    model_name=model_name,
                    local_capacity=local_capacity,
                    redis_ttl=redis_ttl,
                )

    return _embedding_cache_instance


def clear_embedding_cache() -> None:
    """Clear the global cache instance."""
    global _embedding_cache_instance
    with _init_lock:
        if _embedding_cache_instance is not None:
            _embedding_cache_instance.clear()
        _embedding_cache_instance = None