-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
50 lines (45 loc) · 1.68 KB
/
storage.py
File metadata and controls
50 lines (45 loc) · 1.68 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
import json
import pathlib
from typing import List
from config import STORE_PATH, ROOT_DIR
from embeddings import get_embedding
(ROOT_DIR / "success").mkdir(exist_ok=True)
(ROOT_DIR / "fail").mkdir(exist_ok=True)
def load_cache():
successes: List[dict] = []
failures: List[dict] = []
if STORE_PATH.exists():
with open(STORE_PATH, 'r', encoding='utf-8') as f:
for line in f:
rec = json.loads(line)
if rec.get("reward") == 1:
successes.append(rec)
else:
failures.append(rec)
for rec in successes + failures:
if "embedding" not in rec:
try:
rec["embedding"] = get_embedding(rec["prompt"])
except Exception:
rec["embedding"] = []
return successes, failures
successes, failures = load_cache()
def save_flow(prompt, code, reward, folder_name):
import datetime, re
folder = ROOT_DIR / folder_name
ts = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
slug = re.sub(r"[^a-z0-9]+", "-", prompt.lower()).strip("-")[:60] or "untitled"
fp = folder / f"{slug}__{ts}.py"
header = f"# Prompt: {prompt}\n# Outcome: {'success' if reward == 1 else 'fail'}\n\n"
fp.write_text(header + code, encoding="utf-8")
rec = {"prompt": prompt, "code": code, "reward": reward, "timestamp": datetime.datetime.now().isoformat()}
try:
rec["embedding"] = get_embedding(prompt)
except Exception:
rec["embedding"] = []
with open(STORE_PATH, 'a', encoding='utf-8') as f:
f.write(json.dumps(rec) + "\n")
if reward == 1:
successes.append(rec)
else:
failures.append(rec)