Repository navigation
Expand file tree
/
Copy path_smoke_test_cache.py
More file actions
59 lines (51 loc) · 1.77 KB
/
Copy path_smoke_test_cache.py
File metadata and controls
59 lines (51 loc) · 1.77 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
from astroml.llm.embedding_cache import EmbeddingCache
import sys
sys.path.insert(0, ".")
cache = EmbeddingCache(similarity_threshold=0.85)
# Store 5 entries
texts = [
"What is the weather today?",
"How do I detect fraud in transactions?",
"Explain blockchain technology",
"Calculate risk score for account",
"Show me recent transactions",
]
for i, t in enumerate(texts):
cache.store(t, "result_" + str(i))
# Exact hits
for t in texts:
r = cache.get(t)
assert r is not None, "exact hit should work for: " + t
# Semantic hits (slightly varied phrasing)
semantic_queries = [
"What is the weather outside today?", # similar to texts[0]
"How to detect fraudulent transactions?", # similar to texts[1]
]
for q in semantic_queries:
r = cache.get(q)
# Not guaranteed to hit at 0.85 threshold, but log result
print('semantic query "%s" -> %s' % (q[:40], "HIT" if r else "MISS"))
stats = cache.get_stats()
total = stats["hits"] + stats["misses"]
print(
"Hits: %d, Misses: %d, Hit rate: %.2f%%"
% (stats["hits"], stats["misses"], stats["hit_rate"] * 100)
)
print("Sets: %d, Invalidations: %d" % (stats["sets"], stats["invalidations"]))
assert stats["hits"] >= 5, "expected at least 5 exact hits"
print("PASS: exact hits work, hit_rate =", stats["hit_rate"])
# Test invalidation
cache.store("delete me", "data")
assert cache.get("delete me") == "data"
removed = cache.invalidate("delete me")
assert removed == True
assert cache.get("delete me") is None
print("PASS: single entry invalidation works")
# Test invalidate_all
count = cache.invalidate_all()
print("PASS: invalidate_all removed %d entries" % count)
after = cache.get_stats()
print("stats after clear:", after)
assert after["sets"] == 0
assert after["hits"] == 0
print("ALL SMOKE TESTS PASSED")