Repository navigation
Expand file tree
/
Copy pathembedding_cache.py
More file actions
258 lines (210 loc) Β· 8.88 KB
/
Copy pathembedding_cache.py
File metadata and controls
258 lines (210 loc) Β· 8.88 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
#!/usr/bin/env python3
"""
Simplified Embedding Cache Module
"""
import os
import pickle
import hashlib
import json
from typing import List, Dict, Any, Optional
from pathlib import Path
class EmbeddingCache:
"""Simple, reliable embedding cache."""
def __init__(self, cache_dir: str = "embedding_cache", enabled: bool = True):
self.cache_dir = Path(cache_dir)
self.enabled = enabled
self.memory_cache = {}
if self.enabled:
self.cache_dir.mkdir(exist_ok=True)
print(f"π Cache initialized: {self.cache_dir}")
def _make_key(self, texts: List[str], model: str) -> str:
"""Create a simple cache key."""
# Use first few texts + model + count for deterministic key
sample = texts[:3] if len(texts) > 3 else texts
key_data = {
'model': model,
'count': len(texts),
'sample': sample
}
content = json.dumps(key_data, sort_keys=True)
return hashlib.sha256(content.encode()).hexdigest()[:16]
def get(self, texts: List[str], model: str) -> Optional[List[List[float]]]:
"""Get embeddings from cache."""
if not self.enabled:
return None
key = self._make_key(texts, model)
# Check memory first
if key in self.memory_cache:
print(f" π Memory cache hit for {model}")
return self.memory_cache[key]
# Check disk cache
cache_file = self.cache_dir / f"{key}.pkl"
if cache_file.exists():
try:
with open(cache_file, 'rb') as f:
embeddings = pickle.load(f)
self.memory_cache[key] = embeddings
print(f" πΎ Disk cache hit for {model}")
return embeddings
except Exception as e:
print(f" β οΈ Cache load failed: {e}")
cache_file.unlink(missing_ok=True)
return None
def put(self, texts: List[str], model: str, embeddings: List[List[float]]) -> None:
"""Store embeddings in cache."""
if not self.enabled:
return
key = self._make_key(texts, model)
# Store in memory
self.memory_cache[key] = embeddings
# Store on disk
cache_file = self.cache_dir / f"{key}.pkl"
try:
with open(cache_file, 'wb') as f:
pickle.dump(embeddings, f)
print(f" πΎ Cached {len(embeddings)} embeddings for {model}")
except Exception as e:
print(f" β οΈ Cache save failed: {e}")
def clear_cache(self) -> None:
"""Clear all cache."""
self.memory_cache.clear()
if self.cache_dir.exists():
for f in self.cache_dir.glob("*.pkl"):
f.unlink()
print("ποΈ Cache cleared")
def get_stats(self) -> Dict[str, Any]:
"""Get cache statistics."""
if not self.enabled:
return {"enabled": False}
cache_files = list(self.cache_dir.glob("*.pkl"))
total_size = sum(f.stat().st_size for f in cache_files)
return {
"enabled": True,
"memory_entries": len(self.memory_cache),
"disk_entries": len(cache_files),
"total_size_mb": total_size / (1024 * 1024)
}
def get_by_params(self, model: str, dataset_type: str, n_samples: int, size: int, **kwargs) -> Optional[List[List[float]]]:
"""Get embeddings by dataset parameters instead of actual texts."""
if not self.enabled:
return None
# Create parameter-based key
params = {
'model': model,
'dataset_type': dataset_type,
'n_samples': n_samples,
'size': size,
'random_state': kwargs.get('random_state', 42),
**kwargs
}
content = json.dumps(params, sort_keys=True)
key = hashlib.sha256(content.encode()).hexdigest()[:16]
# Check memory first
if key in self.memory_cache:
print(f" π Parameter cache hit for {model}")
return self.memory_cache[key]
# Check disk cache
cache_file = self.cache_dir / f"params_{key}.pkl"
if cache_file.exists():
try:
with open(cache_file, 'rb') as f:
embeddings = pickle.load(f)
self.memory_cache[key] = embeddings
print(f" πΎ Parameter cache hit for {model}")
return embeddings
except Exception as e:
print(f" β οΈ Parameter cache load failed: {e}")
cache_file.unlink(missing_ok=True)
return None
def put_by_params(self, embeddings: List[List[float]], model: str, dataset_type: str,
n_samples: int, size: int, **kwargs) -> None:
"""Store embeddings by dataset parameters."""
if not self.enabled:
return
# Create parameter-based key
params = {
'model': model,
'dataset_type': dataset_type,
'n_samples': n_samples,
'size': size,
'random_state': kwargs.get('random_state', 42),
**kwargs
}
content = json.dumps(params, sort_keys=True)
key = hashlib.sha256(content.encode()).hexdigest()[:16]
# Store in memory
self.memory_cache[key] = embeddings
# Store on disk
cache_file = self.cache_dir / f"params_{key}.pkl"
try:
with open(cache_file, 'wb') as f:
pickle.dump(embeddings, f)
print(f" πΎ Parameter cached for {model}")
except Exception as e:
print(f" β οΈ Parameter cache save failed: {e}")
class CachedEmbeddingWrapper:
"""Wrapper that adds caching to any embedding wrapper."""
def __init__(self, embedding_wrapper, cache_dir: str = "embedding_cache",
enable_cache: bool = True):
self.wrapper = embedding_wrapper
self.cache = EmbeddingCache(cache_dir, enable_cache)
def __getattr__(self, name):
"""Forward all other attributes to the wrapped object."""
return getattr(self.wrapper, name)
def embed(self, texts: List[str], model: str) -> List[List[float]]:
"""Get embeddings with caching."""
# Try cache first
cached = self.cache.get(texts, model)
if cached is not None:
return cached
# Get from API
print(f" π API call for {model}")
embeddings = self.wrapper.embed(texts, model)
# Cache it
self.cache.put(texts, model, embeddings)
return embeddings
def embed_with_params(self, texts: List[str], model: str, dataset_type: str,
size: int, **kwargs) -> List[List[float]]:
"""Get embeddings with parameter-based caching for reproducible datasets."""
# Try parameter cache first
cached = self.cache.get_by_params(
model, dataset_type, len(texts), size, **kwargs
)
if cached is not None:
return cached
# Try regular cache as fallback
cached = self.cache.get(texts, model)
if cached is not None:
print(f" π Text cache hit for {model}")
# Also store in parameter cache for next time
self.cache.put_by_params(cached, model, dataset_type, len(texts), size, **kwargs)
return cached
# Get from API
print(f" π API call for {model}")
embeddings = self.wrapper.embed(texts, model)
# Cache both ways
self.cache.put(texts, model, embeddings)
self.cache.put_by_params(embeddings, model, dataset_type, len(texts), size, **kwargs)
return embeddings
def preload_embeddings(self, texts: List[str], models: List[str]) -> Dict[str, bool]:
"""Preload embeddings for multiple models."""
results = {}
print(f"π¦ Preloading embeddings for {len(models)} models...")
for model in models:
try:
# Check if already cached
if self.cache.get(texts, model) is not None:
print(f" β {model} (already cached)")
results[model] = True
continue
# Fetch and cache
self.embed(texts, model)
results[model] = True
print(f" β {model} (preloaded)")
except Exception as e:
print(f" β {model} (failed: {e})")
results[model] = False
return results
def get_cache_stats(self) -> Dict[str, Any]:
"""Get cache statistics."""
return self.cache.get_stats()