File size: 4,241 Bytes
82ae30c | 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 | """Build v3 validator SFT data with balanced all-OK + critique rows.
v2 had 8.1% all-OK rows → validator hallucinates critiques at inference.
v3 supplements v2 with ~5000 all-OK rows mined from real planner_correct
trajectories on BIRD-TRAIN, so the validator learns to stay silent when
the planner SQL is already correct.
Output: data/multi-agents/fixed/sft-validator-diverse-v3
"""
import json
import random
from datasets import load_from_disk, Dataset, DatasetDict
OK_TEMPLATES = [
"""<select>
SELECT.
No issues with SELECT.
</select>
<condition>
CONDITION.
No issues with WHERE/HAVING.
</condition>
<join>
JOIN.
Tables and join keys look correct.
</join>
<order>
ORDER BY.
None
</order>""",
"""<select>
SELECT.
The SELECT clause is correct.
</select>
<condition>
CONDITION.
Filter conditions look correct.
</condition>
<join>
JOIN.
No issues with JOIN.
</join>
<order>
ORDER BY.
None
</order>""",
"""<select>
SELECT.
None
</select>
<condition>
CONDITION.
None
</condition>
<join>
JOIN.
None
</join>
<order>
ORDER BY.
None
</order>""",
"""<select>
SELECT.
The projection list matches the question.
</select>
<condition>
CONDITION.
WHERE/HAVING clauses are correct.
</condition>
<join>
JOIN.
Tables and join keys are correct.
</join>
<order>
ORDER BY.
The ordering is correct.
</order>""",
]
def main():
rng = random.Random(42)
# Load existing v2 (force plain-dict copy; drop "messages" because v2 stores it as a non-list dict that breaks arrow)
v2 = load_from_disk("/home/datht/mats-sql-tist/data/multi-agents/fixed/sft-validator-diverse-v2")
v2_train = [{"prompt": r["prompt"], "completion": r["completion"]} for r in v2["train"]]
v2_test = [{"prompt": r["prompt"], "completion": r["completion"]} for r in v2["test"]]
# Mine all-OK rows from K=4 train rollouts (planner_correct trajectories)
src = "/home/datht/mats-sql-tist/data/rollouts/scaleup_bird_train_2stage_K4.jsonl"
ok_rows = []
seen_prompts = set()
with open(src) as f:
for line in f:
s = json.loads(line)
for t in s.get("trajectories", []):
if not t.get("is_planner_correct"):
continue
vp = (t.get("validator_prompt") or "").strip()
if not vp:
# rebuild from planner_prompt
pp = (t.get("planner_prompt") or "").strip()
psql = (t.get("planner_sql") or "").strip()
if not pp or not psql:
continue
vp = pp + "\n\nSQL query:\n" + psql
# dedup on full vp
if vp in seen_prompts:
continue
seen_prompts.add(vp)
ok_rows.append(vp)
rng.shuffle(ok_rows)
# Aim: balance such that all-OK ≈ critique. v2 has ~5208 critique rows.
target_ok = 5200
ok_rows = ok_rows[:target_ok]
# Add additional sft-style critique training: use v2 + new all-OK
new_rows = []
for vp in ok_rows:
completion = rng.choice(OK_TEMPLATES)
new_rows.append({"prompt": vp, "completion": completion})
# Test split: keep v2 test + small mined sample
test_ok = ok_rows[target_ok:target_ok + 100] if len(ok_rows) > target_ok else []
new_test_rows = []
for vp in test_ok:
completion = rng.choice(OK_TEMPLATES)
new_test_rows.append({"prompt": vp, "completion": completion})
# Combine
train_combined = v2_train + new_rows
test_combined = v2_test + new_test_rows
rng.shuffle(train_combined)
dd = DatasetDict({
"train": Dataset.from_list(train_combined),
"test": Dataset.from_list(test_combined),
})
out_dir = "/home/datht/mats-sql-tist/data/multi-agents/fixed/sft-validator-diverse-v3"
dd.save_to_disk(out_dir)
# Stats
n_train = len(train_combined)
n_train_ok = sum(1 for r in train_combined if "No issues" in r["completion"] or r["completion"].count("None") >= 3)
print(f"v3 built:")
print(f" train: {n_train} ({n_train_ok} all-OK, {n_train - n_train_ok} critique)")
print(f" test: {len(test_combined)}")
print(f" Saved to {out_dir}")
if __name__ == "__main__":
main()
|