-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_manager.py
More file actions
384 lines (309 loc) · 12.4 KB
/
cache_manager.py
File metadata and controls
384 lines (309 loc) · 12.4 KB
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
"""
DocuMind Caching System
=======================
Provides intelligent caching for:
- Query results (semantic similarity-based)
- Embeddings
- Gemini verification results
- Arxiv paper searches
Uses disk-based persistence with TTL (time-to-live) support.
"""
import os
import json
import hashlib
import pickle
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any, Tuple
from dataclasses import dataclass, asdict
import numpy as np
import config
@dataclass
class CacheEntry:
"""A single cache entry with metadata."""
key: str
value: Any
created_at: str
expires_at: Optional[str]
hit_count: int = 0
def is_expired(self) -> bool:
if self.expires_at is None:
return False
return datetime.now() > datetime.fromisoformat(self.expires_at)
class DiskCache:
"""
Persistent disk-based cache with TTL support.
"""
def __init__(self, cache_name: str, ttl_hours: int = 24):
"""
Initialize disk cache.
Args:
cache_name: Name of the cache (creates subdirectory)
ttl_hours: Time-to-live in hours (0 = never expire)
"""
self.cache_dir = os.path.join(config.CHAT_HISTORY_PATH, "cache", cache_name)
self.ttl_hours = ttl_hours
self.stats = {"hits": 0, "misses": 0}
os.makedirs(self.cache_dir, exist_ok=True)
self._load_stats()
def _get_cache_path(self, key: str) -> str:
"""Get file path for a cache key."""
# Hash the key to create a valid filename
key_hash = hashlib.md5(key.encode()).hexdigest()
return os.path.join(self.cache_dir, f"{key_hash}.cache")
def _load_stats(self) -> None:
"""Load cache statistics."""
stats_path = os.path.join(self.cache_dir, "_stats.json")
if os.path.exists(stats_path):
with open(stats_path, "r") as f:
self.stats = json.load(f)
def _save_stats(self) -> None:
"""Save cache statistics."""
stats_path = os.path.join(self.cache_dir, "_stats.json")
with open(stats_path, "w") as f:
json.dump(self.stats, f)
def get(self, key: str) -> Optional[Any]:
"""
Get value from cache.
Returns:
Cached value or None if not found/expired
"""
cache_path = self._get_cache_path(key)
if not os.path.exists(cache_path):
self.stats["misses"] += 1
return None
try:
with open(cache_path, "rb") as f:
entry: CacheEntry = pickle.load(f)
if entry.is_expired():
os.remove(cache_path)
self.stats["misses"] += 1
return None
# Update hit count
entry.hit_count += 1
with open(cache_path, "wb") as f:
pickle.dump(entry, f)
self.stats["hits"] += 1
self._save_stats()
return entry.value
except Exception:
self.stats["misses"] += 1
return None
def set(self, key: str, value: Any, ttl_hours: Optional[int] = None) -> None:
"""
Set value in cache.
Args:
key: Cache key
value: Value to cache
ttl_hours: Override default TTL
"""
ttl = ttl_hours if ttl_hours is not None else self.ttl_hours
expires_at = None
if ttl > 0:
expires_at = (datetime.now() + timedelta(hours=ttl)).isoformat()
entry = CacheEntry(
key=key,
value=value,
created_at=datetime.now().isoformat(),
expires_at=expires_at,
hit_count=0
)
cache_path = self._get_cache_path(key)
with open(cache_path, "wb") as f:
pickle.dump(entry, f)
def delete(self, key: str) -> bool:
"""Delete a cache entry."""
cache_path = self._get_cache_path(key)
if os.path.exists(cache_path):
os.remove(cache_path)
return True
return False
def clear(self) -> int:
"""Clear all cache entries. Returns number of entries cleared."""
count = 0
for filename in os.listdir(self.cache_dir):
if filename.endswith(".cache"):
os.remove(os.path.join(self.cache_dir, filename))
count += 1
self.stats = {"hits": 0, "misses": 0}
self._save_stats()
return count
def cleanup_expired(self) -> int:
"""Remove expired entries. Returns number removed."""
count = 0
for filename in os.listdir(self.cache_dir):
if filename.endswith(".cache"):
cache_path = os.path.join(self.cache_dir, filename)
try:
with open(cache_path, "rb") as f:
entry: CacheEntry = pickle.load(f)
if entry.is_expired():
os.remove(cache_path)
count += 1
except Exception:
pass
return count
def get_stats(self) -> Dict:
"""Get cache statistics."""
total = self.stats["hits"] + self.stats["misses"]
hit_rate = self.stats["hits"] / total if total > 0 else 0
# Count entries
entry_count = len([f for f in os.listdir(self.cache_dir) if f.endswith(".cache")])
return {
"hits": self.stats["hits"],
"misses": self.stats["misses"],
"hit_rate": f"{hit_rate:.1%}",
"entries": entry_count
}
class SemanticQueryCache:
"""
Caches query results with semantic similarity matching.
If a similar question was asked before, returns cached answer.
"""
def __init__(self, similarity_threshold: float = 0.92):
"""
Initialize semantic cache.
Args:
similarity_threshold: Cosine similarity threshold for cache hits (0.0-1.0)
"""
self.cache = DiskCache("semantic_queries", ttl_hours=72) # 3 days
self.similarity_threshold = similarity_threshold
self.embeddings_cache: Dict[str, List[float]] = {}
self._load_embeddings_index()
def _get_embeddings_path(self) -> str:
return os.path.join(self.cache.cache_dir, "_embeddings_index.pkl")
def _load_embeddings_index(self) -> None:
"""Load the embeddings index from disk."""
path = self._get_embeddings_path()
if os.path.exists(path):
try:
with open(path, "rb") as f:
self.embeddings_cache = pickle.load(f)
except Exception:
self.embeddings_cache = {}
def _save_embeddings_index(self) -> None:
"""Save embeddings index to disk."""
path = self._get_embeddings_path()
with open(path, "wb") as f:
pickle.dump(self.embeddings_cache, f)
def _compute_embedding(self, text: str) -> List[float]:
"""Compute embedding for text using the configured model."""
from llama_index.core import Settings
embedding = Settings.embed_model.get_text_embedding(text)
return embedding
def _cosine_similarity(self, a: List[float], b: List[float]) -> float:
"""Compute cosine similarity between two vectors."""
a_np = np.array(a)
b_np = np.array(b)
return float(np.dot(a_np, b_np) / (np.linalg.norm(a_np) * np.linalg.norm(b_np)))
def find_similar(self, query: str) -> Optional[Tuple[str, str, float]]:
"""
Find a semantically similar cached query.
Returns:
Tuple of (cached_query, cached_answer, similarity_score) or None
"""
if not self.embeddings_cache:
return None
query_embedding = self._compute_embedding(query)
best_match = None
best_score = 0.0
for cached_query, cached_embedding in self.embeddings_cache.items():
score = self._cosine_similarity(query_embedding, cached_embedding)
if score > best_score and score >= self.similarity_threshold:
best_score = score
best_match = cached_query
if best_match:
cached_answer = self.cache.get(best_match)
if cached_answer:
return (best_match, cached_answer, best_score)
return None
def cache_query(self, query: str, answer: str) -> None:
"""Cache a query-answer pair with its embedding."""
# Store the answer
self.cache.set(query, answer)
# Store the embedding
embedding = self._compute_embedding(query)
self.embeddings_cache[query] = embedding
self._save_embeddings_index()
def get_stats(self) -> Dict:
"""Get cache statistics."""
stats = self.cache.get_stats()
stats["cached_queries"] = len(self.embeddings_cache)
stats["similarity_threshold"] = self.similarity_threshold
return stats
class GeminiResponseCache:
"""Cache for Gemini API responses to reduce API costs."""
def __init__(self):
self.cache = DiskCache("gemini_responses", ttl_hours=168) # 7 days
def get_verification(self, qa_pairs_hash: str) -> Optional[Dict]:
"""Get cached verification result."""
return self.cache.get(qa_pairs_hash)
def cache_verification(self, qa_pairs_hash: str, result: Dict) -> None:
"""Cache a verification result."""
self.cache.set(qa_pairs_hash, result)
@staticmethod
def hash_qa_pairs(qa_pairs: List[Tuple[str, str]]) -> str:
"""Create a hash of Q&A pairs for cache key."""
content = json.dumps(qa_pairs, sort_keys=True)
return hashlib.md5(content.encode()).hexdigest()
class CacheManager:
"""
Central manager for all caches.
Provides unified interface and statistics.
"""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self.query_cache = SemanticQueryCache(similarity_threshold=0.92)
self.gemini_cache = GeminiResponseCache()
self.arxiv_cache = DiskCache("arxiv_papers", ttl_hours=168) # 7 days
self._initialized = True
def get_all_stats(self) -> Dict:
"""Get statistics for all caches."""
return {
"query_cache": self.query_cache.get_stats(),
"gemini_cache": self.gemini_cache.cache.get_stats(),
"arxiv_cache": self.arxiv_cache.get_stats()
}
def cleanup_all(self) -> Dict[str, int]:
"""Cleanup expired entries from all caches."""
return {
"query_cache": self.query_cache.cache.cleanup_expired(),
"gemini_cache": self.gemini_cache.cache.cleanup_expired(),
"arxiv_cache": self.arxiv_cache.cleanup_expired()
}
def clear_all(self) -> Dict[str, int]:
"""Clear all caches."""
return {
"query_cache": self.query_cache.cache.clear(),
"gemini_cache": self.gemini_cache.cache.clear(),
"arxiv_cache": self.arxiv_cache.clear()
}
def print_stats(self) -> None:
"""Print cache statistics."""
stats = self.get_all_stats()
print("\n📊 Cache Statistics:")
print("-" * 40)
print("\n🔍 Query Cache (Semantic):")
qs = stats["query_cache"]
print(f" Cached queries: {qs['cached_queries']}")
print(f" Hit rate: {qs['hit_rate']}")
print(f" Hits/Misses: {qs['hits']}/{qs['misses']}")
print("\n🤖 Gemini Response Cache:")
gs = stats["gemini_cache"]
print(f" Cached responses: {gs['entries']}")
print(f" Hit rate: {gs['hit_rate']}")
print("\n📚 Arxiv Cache:")
arxiv_s = stats["arxiv_cache"]
print(f" Cached searches: {arxiv_s['entries']}")
print(f" Hit rate: {arxiv_s['hit_rate']}")
print("-" * 40)
# Singleton instance
def get_cache_manager() -> CacheManager:
"""Get the global cache manager instance."""
return CacheManager()