chore: remove stale scripts/build_validator_sft_v3_balanced.py
Browse files
scripts/build_validator_sft_v3_balanced.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
"""Build v3 validator SFT data with balanced all-OK + critique rows.
|
| 2 |
-
|
| 3 |
-
v2 had 8.1% all-OK rows → validator hallucinates critiques at inference.
|
| 4 |
-
v3 supplements v2 with ~5000 all-OK rows mined from real planner_correct
|
| 5 |
-
trajectories on BIRD-TRAIN, so the validator learns to stay silent when
|
| 6 |
-
the planner SQL is already correct.
|
| 7 |
-
|
| 8 |
-
Output: data/multi-agents/fixed/sft-validator-diverse-v3
|
| 9 |
-
"""
|
| 10 |
-
import json
|
| 11 |
-
import random
|
| 12 |
-
from datasets import load_from_disk, Dataset, DatasetDict
|
| 13 |
-
|
| 14 |
-
OK_TEMPLATES = [
|
| 15 |
-
"""<select>
|
| 16 |
-
SELECT.
|
| 17 |
-
No issues with SELECT.
|
| 18 |
-
</select>
|
| 19 |
-
|
| 20 |
-
<condition>
|
| 21 |
-
CONDITION.
|
| 22 |
-
No issues with WHERE/HAVING.
|
| 23 |
-
</condition>
|
| 24 |
-
|
| 25 |
-
<join>
|
| 26 |
-
JOIN.
|
| 27 |
-
Tables and join keys look correct.
|
| 28 |
-
</join>
|
| 29 |
-
|
| 30 |
-
<order>
|
| 31 |
-
ORDER BY.
|
| 32 |
-
None
|
| 33 |
-
</order>""",
|
| 34 |
-
"""<select>
|
| 35 |
-
SELECT.
|
| 36 |
-
The SELECT clause is correct.
|
| 37 |
-
</select>
|
| 38 |
-
|
| 39 |
-
<condition>
|
| 40 |
-
CONDITION.
|
| 41 |
-
Filter conditions look correct.
|
| 42 |
-
</condition>
|
| 43 |
-
|
| 44 |
-
<join>
|
| 45 |
-
JOIN.
|
| 46 |
-
No issues with JOIN.
|
| 47 |
-
</join>
|
| 48 |
-
|
| 49 |
-
<order>
|
| 50 |
-
ORDER BY.
|
| 51 |
-
None
|
| 52 |
-
</order>""",
|
| 53 |
-
"""<select>
|
| 54 |
-
SELECT.
|
| 55 |
-
None
|
| 56 |
-
</select>
|
| 57 |
-
|
| 58 |
-
<condition>
|
| 59 |
-
CONDITION.
|
| 60 |
-
None
|
| 61 |
-
</condition>
|
| 62 |
-
|
| 63 |
-
<join>
|
| 64 |
-
JOIN.
|
| 65 |
-
None
|
| 66 |
-
</join>
|
| 67 |
-
|
| 68 |
-
<order>
|
| 69 |
-
ORDER BY.
|
| 70 |
-
None
|
| 71 |
-
</order>""",
|
| 72 |
-
"""<select>
|
| 73 |
-
SELECT.
|
| 74 |
-
The projection list matches the question.
|
| 75 |
-
</select>
|
| 76 |
-
|
| 77 |
-
<condition>
|
| 78 |
-
CONDITION.
|
| 79 |
-
WHERE/HAVING clauses are correct.
|
| 80 |
-
</condition>
|
| 81 |
-
|
| 82 |
-
<join>
|
| 83 |
-
JOIN.
|
| 84 |
-
Tables and join keys are correct.
|
| 85 |
-
</join>
|
| 86 |
-
|
| 87 |
-
<order>
|
| 88 |
-
ORDER BY.
|
| 89 |
-
The ordering is correct.
|
| 90 |
-
</order>""",
|
| 91 |
-
]
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
def main():
|
| 95 |
-
rng = random.Random(42)
|
| 96 |
-
|
| 97 |
-
# Load existing v2 (force plain-dict copy; drop "messages" because v2 stores it as a non-list dict that breaks arrow)
|
| 98 |
-
v2 = load_from_disk("/home/datht/mats-sql-tist/data/multi-agents/fixed/sft-validator-diverse-v2")
|
| 99 |
-
v2_train = [{"prompt": r["prompt"], "completion": r["completion"]} for r in v2["train"]]
|
| 100 |
-
v2_test = [{"prompt": r["prompt"], "completion": r["completion"]} for r in v2["test"]]
|
| 101 |
-
|
| 102 |
-
# Mine all-OK rows from K=4 train rollouts (planner_correct trajectories)
|
| 103 |
-
src = "/home/datht/mats-sql-tist/data/rollouts/scaleup_bird_train_2stage_K4.jsonl"
|
| 104 |
-
ok_rows = []
|
| 105 |
-
seen_prompts = set()
|
| 106 |
-
with open(src) as f:
|
| 107 |
-
for line in f:
|
| 108 |
-
s = json.loads(line)
|
| 109 |
-
for t in s.get("trajectories", []):
|
| 110 |
-
if not t.get("is_planner_correct"):
|
| 111 |
-
continue
|
| 112 |
-
vp = (t.get("validator_prompt") or "").strip()
|
| 113 |
-
if not vp:
|
| 114 |
-
# rebuild from planner_prompt
|
| 115 |
-
pp = (t.get("planner_prompt") or "").strip()
|
| 116 |
-
psql = (t.get("planner_sql") or "").strip()
|
| 117 |
-
if not pp or not psql:
|
| 118 |
-
continue
|
| 119 |
-
vp = pp + "\n\nSQL query:\n" + psql
|
| 120 |
-
# dedup on full vp
|
| 121 |
-
if vp in seen_prompts:
|
| 122 |
-
continue
|
| 123 |
-
seen_prompts.add(vp)
|
| 124 |
-
ok_rows.append(vp)
|
| 125 |
-
|
| 126 |
-
rng.shuffle(ok_rows)
|
| 127 |
-
|
| 128 |
-
# Aim: balance such that all-OK ≈ critique. v2 has ~5208 critique rows.
|
| 129 |
-
target_ok = 5200
|
| 130 |
-
ok_rows = ok_rows[:target_ok]
|
| 131 |
-
|
| 132 |
-
# Add additional sft-style critique training: use v2 + new all-OK
|
| 133 |
-
new_rows = []
|
| 134 |
-
for vp in ok_rows:
|
| 135 |
-
completion = rng.choice(OK_TEMPLATES)
|
| 136 |
-
new_rows.append({"prompt": vp, "completion": completion})
|
| 137 |
-
|
| 138 |
-
# Test split: keep v2 test + small mined sample
|
| 139 |
-
test_ok = ok_rows[target_ok:target_ok + 100] if len(ok_rows) > target_ok else []
|
| 140 |
-
new_test_rows = []
|
| 141 |
-
for vp in test_ok:
|
| 142 |
-
completion = rng.choice(OK_TEMPLATES)
|
| 143 |
-
new_test_rows.append({"prompt": vp, "completion": completion})
|
| 144 |
-
|
| 145 |
-
# Combine
|
| 146 |
-
train_combined = v2_train + new_rows
|
| 147 |
-
test_combined = v2_test + new_test_rows
|
| 148 |
-
rng.shuffle(train_combined)
|
| 149 |
-
|
| 150 |
-
dd = DatasetDict({
|
| 151 |
-
"train": Dataset.from_list(train_combined),
|
| 152 |
-
"test": Dataset.from_list(test_combined),
|
| 153 |
-
})
|
| 154 |
-
|
| 155 |
-
out_dir = "/home/datht/mats-sql-tist/data/multi-agents/fixed/sft-validator-diverse-v3"
|
| 156 |
-
dd.save_to_disk(out_dir)
|
| 157 |
-
|
| 158 |
-
# Stats
|
| 159 |
-
n_train = len(train_combined)
|
| 160 |
-
n_train_ok = sum(1 for r in train_combined if "No issues" in r["completion"] or r["completion"].count("None") >= 3)
|
| 161 |
-
print(f"v3 built:")
|
| 162 |
-
print(f" train: {n_train} ({n_train_ok} all-OK, {n_train - n_train_ok} critique)")
|
| 163 |
-
print(f" test: {len(test_combined)}")
|
| 164 |
-
print(f" Saved to {out_dir}")
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
if __name__ == "__main__":
|
| 168 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|