-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
482 lines (410 loc) · 15.9 KB
/
cache.py
File metadata and controls
482 lines (410 loc) · 15.9 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
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
"""Semantic caching system for API responses.
Supports exact match and fuzzy/semantic cache hits using embeddings.
"""
import sqlite3
import json
import time
import numpy as np
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union
from dataclasses import dataclass, asdict
from datetime import datetime, timedelta
import hashlib
import threading
from abc import ABC, abstractmethod
from .utils import hash_content, normalize_prompt, format_messages, get_timestamp
@dataclass
class CacheEntry:
"""Represents a cached response."""
key: str
prompt_hash: str
prompt_text: str
response: str
model: str
embedding: Optional[List[float]]
created_at: str
expires_at: str
hit_count: int = 0
tokens_saved: int = 0
cost_saved: float = 0.0
@dataclass
class CacheStats:
"""Cache statistics."""
total_entries: int = 0
total_hits: int = 0
total_misses: int = 0
exact_hits: int = 0
semantic_hits: int = 0
total_tokens_saved: int = 0
total_cost_saved: float = 0.0
@property
def hit_rate(self) -> float:
total = self.total_hits + self.total_misses
return self.total_hits / total if total > 0 else 0.0
class EmbeddingProvider(ABC):
"""Abstract base class for embedding providers."""
@abstractmethod
def get_embedding(self, text: str) -> List[float]:
"""Get embedding vector for text."""
pass
class SimpleEmbeddingProvider(EmbeddingProvider):
"""Simple TF-IDF-like embedding for offline use."""
def __init__(self, dim: int = 256):
self.dim = dim
def get_embedding(self, text: str) -> List[float]:
"""Generate a simple hash-based embedding."""
# Normalize text
text = normalize_prompt(text)
words = text.split()
# Create embedding using word hashes
embedding = [0.0] * self.dim
for word in words:
word_hash = int(hashlib.md5(word.encode()).hexdigest(), 16)
for i in range(self.dim):
embedding[i] += ((word_hash >> i) & 1) * 2 - 1
# Normalize
norm = np.linalg.norm(embedding)
if norm > 0:
embedding = [x / norm for x in embedding]
return embedding
class OpenAIEmbeddingProvider(EmbeddingProvider):
"""OpenAI embedding provider."""
def __init__(self, api_key: str, model: str = "text-embedding-3-small"):
self.api_key = api_key
self.model = model
self._client = None
@property
def client(self):
if self._client is None:
try:
from openai import OpenAI
self._client = OpenAI(api_key=self.api_key)
except ImportError:
raise ImportError("openai package required for OpenAI embeddings")
return self._client
def get_embedding(self, text: str) -> List[float]:
"""Get embedding from OpenAI API."""
response = self.client.embeddings.create(
model=self.model,
input=text[:8000] # Truncate to avoid token limits
)
return response.data[0].embedding
class ResponseCache:
"""Semantic caching system for API responses."""
def __init__(
self,
db_path: str = "cache.db",
default_ttl: int = 3600,
similarity_threshold: float = 0.92,
embedding_provider: Optional[EmbeddingProvider] = None,
max_entries: int = 10000
):
"""
Initialize the cache.
Args:
db_path: Path to SQLite database
default_ttl: Default time-to-live in seconds
similarity_threshold: Minimum similarity for semantic match (0-1)
embedding_provider: Provider for generating embeddings
max_entries: Maximum cache entries before cleanup
"""
self.db_path = Path(db_path)
self.default_ttl = default_ttl
self.similarity_threshold = similarity_threshold
self.embedding_provider = embedding_provider or SimpleEmbeddingProvider()
self.max_entries = max_entries
self._lock = threading.Lock()
self._stats = CacheStats()
self._init_db()
self._load_stats()
def _init_db(self):
"""Initialize SQLite database."""
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS cache_entries (
key TEXT PRIMARY KEY,
prompt_hash TEXT NOT NULL,
prompt_text TEXT NOT NULL,
response TEXT NOT NULL,
model TEXT NOT NULL,
embedding BLOB,
created_at TEXT NOT NULL,
expires_at TEXT NOT NULL,
hit_count INTEGER DEFAULT 0,
tokens_saved INTEGER DEFAULT 0,
cost_saved REAL DEFAULT 0.0
)
""")
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_prompt_hash
ON cache_entries(prompt_hash)
""")
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_expires_at
ON cache_entries(expires_at)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS cache_stats (
id INTEGER PRIMARY KEY CHECK (id = 1),
total_hits INTEGER DEFAULT 0,
total_misses INTEGER DEFAULT 0,
exact_hits INTEGER DEFAULT 0,
semantic_hits INTEGER DEFAULT 0,
total_tokens_saved INTEGER DEFAULT 0,
total_cost_saved REAL DEFAULT 0.0
)
""")
conn.execute("""
INSERT OR IGNORE INTO cache_stats (id) VALUES (1)
""")
conn.commit()
def _load_stats(self):
"""Load stats from database."""
with sqlite3.connect(self.db_path) as conn:
row = conn.execute("""
SELECT total_hits, total_misses, exact_hits, semantic_hits,
total_tokens_saved, total_cost_saved
FROM cache_stats WHERE id = 1
""").fetchone()
if row:
self._stats.total_hits = row[0]
self._stats.total_misses = row[1]
self._stats.exact_hits = row[2]
self._stats.semantic_hits = row[3]
self._stats.total_tokens_saved = row[4]
self._stats.total_cost_saved = row[5]
count = conn.execute(
"SELECT COUNT(*) FROM cache_entries"
).fetchone()[0]
self._stats.total_entries = count
def _save_stats(self):
"""Save stats to database."""
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
UPDATE cache_stats SET
total_hits = ?,
total_misses = ?,
exact_hits = ?,
semantic_hits = ?,
total_tokens_saved = ?,
total_cost_saved = ?
WHERE id = 1
""", (
self._stats.total_hits,
self._stats.total_misses,
self._stats.exact_hits,
self._stats.semantic_hits,
self._stats.total_tokens_saved,
self._stats.total_cost_saved
))
conn.commit()
def _cosine_similarity(self, a: List[float], b: List[float]) -> float:
"""Calculate cosine similarity between two vectors."""
a_arr = np.array(a)
b_arr = np.array(b)
dot = np.dot(a_arr, b_arr)
norm_a = np.linalg.norm(a_arr)
norm_b = np.linalg.norm(b_arr)
if norm_a == 0 or norm_b == 0:
return 0.0
return dot / (norm_a * norm_b)
def _is_expired(self, expires_at: str) -> bool:
"""Check if entry is expired."""
exp_time = datetime.fromisoformat(expires_at)
return datetime.utcnow() > exp_time
def get(
self,
prompt: Union[str, List[Dict[str, str]]],
model: str = "",
semantic_search: bool = True
) -> Optional[Tuple[str, str]]:
"""
Get cached response for prompt.
Args:
prompt: The prompt string or messages list
model: Optional model filter
semantic_search: Whether to use semantic matching
Returns:
Tuple of (response, match_type) or None if not found
"""
with self._lock:
# Normalize prompt
if isinstance(prompt, list):
prompt_text = format_messages(prompt)
else:
prompt_text = prompt
prompt_hash = hash_content(normalize_prompt(prompt_text))
with sqlite3.connect(self.db_path) as conn:
# Try exact match first
query = """
SELECT key, response, expires_at, embedding
FROM cache_entries
WHERE prompt_hash = ?
"""
params = [prompt_hash]
if model:
query += " AND model = ?"
params.append(model)
rows = conn.execute(query, params).fetchall()
for row in rows:
key, response, expires_at, _ = row
if not self._is_expired(expires_at):
self._record_hit(conn, key, "exact")
return (response, "exact")
# Try semantic match if enabled
if semantic_search:
prompt_embedding = self.embedding_provider.get_embedding(prompt_text)
# Get all non-expired entries
query = """
SELECT key, response, expires_at, embedding
FROM cache_entries
WHERE expires_at > ?
"""
params = [datetime.utcnow().isoformat()]
if model:
query += " AND model = ?"
params.append(model)
rows = conn.execute(query, params).fetchall()
best_match = None
best_similarity = 0.0
for row in rows:
key, response, expires_at, embedding_blob = row
if embedding_blob:
stored_embedding = json.loads(embedding_blob)
similarity = self._cosine_similarity(
prompt_embedding, stored_embedding
)
if similarity > best_similarity:
best_similarity = similarity
best_match = (key, response)
if best_match and best_similarity >= self.similarity_threshold:
self._record_hit(conn, best_match[0], "semantic")
return (best_match[1], f"semantic:{best_similarity:.3f}")
# Cache miss
self._stats.total_misses += 1
self._save_stats()
return None
def _record_hit(
self,
conn: sqlite3.Connection,
key: str,
hit_type: str
):
"""Record a cache hit."""
conn.execute(
"UPDATE cache_entries SET hit_count = hit_count + 1 WHERE key = ?",
(key,)
)
conn.commit()
self._stats.total_hits += 1
if hit_type == "exact":
self._stats.exact_hits += 1
else:
self._stats.semantic_hits += 1
self._save_stats()
def set(
self,
prompt: Union[str, List[Dict[str, str]]],
response: str,
model: str = "",
ttl: Optional[int] = None,
tokens_saved: int = 0,
cost_saved: float = 0.0
) -> str:
"""
Cache a response.
Args:
prompt: The prompt string or messages list
response: The response to cache
model: The model used
ttl: Time-to-live in seconds (uses default if None)
tokens_saved: Estimated tokens this cache will save
cost_saved: Estimated cost this cache will save
Returns:
Cache entry key
"""
with self._lock:
# Normalize prompt
if isinstance(prompt, list):
prompt_text = format_messages(prompt)
else:
prompt_text = prompt
prompt_hash = hash_content(normalize_prompt(prompt_text))
key = hash_content(f"{prompt_hash}:{model}:{time.time()}")
# Generate embedding
embedding = self.embedding_provider.get_embedding(prompt_text)
embedding_blob = json.dumps(embedding)
# Calculate expiry
ttl = ttl or self.default_ttl
created_at = datetime.utcnow()
expires_at = created_at + timedelta(seconds=ttl)
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
INSERT OR REPLACE INTO cache_entries
(key, prompt_hash, prompt_text, response, model, embedding,
created_at, expires_at, hit_count, tokens_saved, cost_saved)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)
""", (
key, prompt_hash, prompt_text, response, model, embedding_blob,
created_at.isoformat(), expires_at.isoformat(),
tokens_saved, cost_saved
))
conn.commit()
self._stats.total_entries += 1
# Cleanup if needed
if self._stats.total_entries > self.max_entries:
self._cleanup()
return key
def _cleanup(self):
"""Remove expired entries and oldest entries if over limit."""
with sqlite3.connect(self.db_path) as conn:
# Remove expired
conn.execute(
"DELETE FROM cache_entries WHERE expires_at < ?",
(datetime.utcnow().isoformat(),)
)
# Remove oldest if still over limit
count = conn.execute(
"SELECT COUNT(*) FROM cache_entries"
).fetchone()[0]
if count > self.max_entries:
to_remove = count - self.max_entries + 100 # Remove extra buffer
conn.execute("""
DELETE FROM cache_entries WHERE key IN (
SELECT key FROM cache_entries
ORDER BY hit_count ASC, created_at ASC
LIMIT ?
)
""", (to_remove,))
conn.commit()
self._stats.total_entries = conn.execute(
"SELECT COUNT(*) FROM cache_entries"
).fetchone()[0]
def invalidate(self, key: str) -> bool:
"""Invalidate a specific cache entry."""
with self._lock:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute(
"DELETE FROM cache_entries WHERE key = ?", (key,)
)
conn.commit()
if cursor.rowcount > 0:
self._stats.total_entries -= 1
return True
return False
def clear(self):
"""Clear all cache entries."""
with self._lock:
with sqlite3.connect(self.db_path) as conn:
conn.execute("DELETE FROM cache_entries")
conn.commit()
self._stats.total_entries = 0
def get_stats(self) -> CacheStats:
"""Get cache statistics."""
return self._stats
def record_savings(self, tokens: int, cost: float):
"""Record tokens and cost saved from cache hit."""
with self._lock:
self._stats.total_tokens_saved += tokens
self._stats.total_cost_saved += cost
self._save_stats()