-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_all_caches.py
More file actions
66 lines (54 loc) · 2.44 KB
/
Copy pathclear_all_caches.py
File metadata and controls
66 lines (54 loc) · 2.44 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
#!/usr/bin/env python3
"""
Clear all caches used by the classification system.
WHEN TO USE THIS:
- Once before testing a NEW HYPOTHESIS (before first eval of that hypothesis)
- NOT needed between individual test runs (the LRU cache is auto-cleared each invocation)
- The other 3 cache layers (rendered prompts, service responses, promptfoo results)
persist across evaluations and are cleared here
CACHES CLEARED:
1. Python's LRU cache for prompt templates (auto-cleared in provider, but cleared here too for safety)
2. In-memory rendered prompt cache dictionary
3. Service response cache (diskcache: ./cache/provider_responses_cache/)
4. Promptfoo evaluation cache (.promptfoo/cache/cache.json)
"""
import os
import shutil
from pathlib import Path
def clear_all_caches():
"""Clear all cache layers."""
# Change to repo root
if os.path.isfile(__file__):
repo_root = os.path.dirname(os.path.abspath(__file__))
else:
repo_root = os.path.abspath(".")
os.chdir(repo_root)
if repo_root not in __import__("sys").path:
__import__("sys").path.insert(0, repo_root)
print("🔄 Clearing all caches before new hypothesis test...\n")
# Layer 1 & 2: Python LRU cache AND rendered prompt cache
print(" 1-2. Clearing Python prompt caches (LRU + rendered dict)...")
from app.providers.base import clear_all_prompt_caches
clear_all_prompt_caches()
print(" ✓ All Python prompt caches cleared")
# Layer 3: Service response cache (diskcache)
print(" 3. Clearing service response cache (diskcache)...")
cache_dir = Path("./cache/provider_responses_cache")
if cache_dir.exists():
shutil.rmtree(cache_dir)
print(f" ✓ Deleted {cache_dir}")
else:
print(f" - {cache_dir} does not exist (already clean)")
# Layer 4: Promptfoo evaluation cache
print(" 4. Clearing promptfoo evaluation cache...")
promptfoo_cache = Path("./.promptfoo/cache/cache.json")
if promptfoo_cache.exists():
promptfoo_cache.unlink()
print(f" ✓ Deleted {promptfoo_cache}")
else:
print(f" - {promptfoo_cache} does not exist (already clean)")
print("\n✅ All caches cleared!\n")
print("📋 Ready to run: promptfoo eval -c promptfoo/followup_questions_eval.yaml --no-cache")
print(" (The --no-cache flag is REQUIRED to bypass promptfoo's built-in caching)\n")
if __name__ == "__main__":
clear_all_caches()