| """Build fixer ORPO iter-2 data biased toward CONSERVATIVE behavior. |
| |
| Source: BIRD-TRAIN 3-stage rollouts (multiple files merged). |
| - Bad flips (P correct, F wrong): chosen=planner_sql, rejected=fixed_sql → teaches "don't mangle correct SQL" |
| - Good flips (P wrong, F correct): chosen=fixed_sql, rejected=planner_sql → teaches "do fix when needed" |
| - Same-correct synthetic pairs: when both P and F end up correct but F differs from P, use planner_sql as chosen |
| (slight preference for the simpler / closer-to-input SQL). |
| |
| Total: targeting ~500-1000 pairs. |
| |
| Output: data/llm_alignment/scaleup_iter2_v2/hf_fixer_conservative |
| """ |
| import json |
| import os |
| from datasets import Dataset, DatasetDict |
|
|
| OUT_DIR = "/home/datht/mats-sql-tist/data/llm_alignment/scaleup_iter2_v2/hf_fixer_conservative" |
|
|
| SRC_PATHS = [ |
| "/home/datht/mats-sql-tist/data/rollouts/bird_train_3stage_K4.jsonl", |
| "/home/datht/mats-sql-tist/data/rollouts/scaleup_bird_train_3stage_K4.jsonl", |
| "/home/datht/mats-sql-tist/data/rollouts/iter2_bird_train_3stage_K8.jsonl", |
| ] |
|
|
|
|
| def normalize_sql(sql): |
| return " ".join(sql.split()).lower().strip() |
|
|
|
|
| def main(): |
| bad_flip_pairs = [] |
| good_flip_pairs = [] |
| same_correct_pairs = [] |
| seen_prompts = set() |
|
|
| for p in SRC_PATHS: |
| if not os.path.exists(p): |
| continue |
| with open(p) as f: |
| for line in f: |
| s = json.loads(line) |
| for t in s.get("trajectories", []): |
| pc = t.get("is_planner_correct", False) |
| fc = t.get("is_fixed_correct", False) |
| fixer_prompt = (t.get("fixer_prompt") or "").strip() |
| planner_sql = (t.get("planner_sql") or "").strip() |
| fixed_sql = (t.get("fixed_sql") or "").strip() |
| if not fixer_prompt or not planner_sql or not fixed_sql: |
| continue |
| if normalize_sql(planner_sql) == normalize_sql(fixed_sql): |
| continue |
| |
| key = fixer_prompt[:1000] + "|" + planner_sql[:200] |
| if key in seen_prompts: |
| continue |
| seen_prompts.add(key) |
|
|
| chosen_planner = f"```sql\n{planner_sql}\n```" |
| chosen_fix = f"```sql\n{fixed_sql}\n```" |
| base = { |
| "prompt": fixer_prompt, |
| "db_path": s.get("db_path", ""), |
| "question": s.get("question", ""), |
| "db_id": s.get("db_id", ""), |
| } |
| if pc and (not fc): |
| |
| bad_flip_pairs.append({**base, "chosen": chosen_planner, "rejected": chosen_fix}) |
| elif (not pc) and fc: |
| |
| good_flip_pairs.append({**base, "chosen": chosen_fix, "rejected": chosen_planner}) |
| elif pc and fc: |
| |
| same_correct_pairs.append({**base, "chosen": chosen_planner, "rejected": chosen_fix}) |
|
|
| |
| target_same = max(0, 600 - len(bad_flip_pairs) - 3 * len(good_flip_pairs)) |
| same_correct_pairs = same_correct_pairs[:target_same] |
| |
| good_flip_aug = good_flip_pairs * 3 |
| |
| bad_flip_aug = bad_flip_pairs * 3 |
|
|
| new_pairs = bad_flip_aug + good_flip_aug + same_correct_pairs |
|
|
| |
| from datasets import load_from_disk |
| try: |
| existing = load_from_disk("/home/datht/mats-sql-tist/data/llm_alignment/scaleup_iter2/hf_fixer_shared") |
| for split in ("train_dpo", "test_dpo"): |
| for r in existing[split]: |
| new_pairs.append({ |
| "prompt": r["prompt"], |
| "chosen": r["chosen"], |
| "rejected": r["rejected"], |
| "db_path": r.get("db_path", ""), |
| "question": r.get("question", ""), |
| "db_id": r.get("db_id", ""), |
| }) |
| print(f" Merged {len(existing['train_dpo']) + len(existing['test_dpo'])} pairs from scaleup_iter2/hf_fixer_shared") |
| except Exception as e: |
| print(f" WARN: could not merge existing data: {e}") |
|
|
| import random |
| rng = random.Random(42) |
| rng.shuffle(new_pairs) |
|
|
| n_test = max(20, len(new_pairs) // 30) |
| test = new_pairs[:n_test] |
| train = new_pairs[n_test:] |
| all_pairs = new_pairs |
|
|
| dd = DatasetDict({ |
| "train_dpo": Dataset.from_list(train), |
| "test_dpo": Dataset.from_list(test), |
| }) |
| dd.save_to_disk(OUT_DIR) |
|
|
| print(f"=== Fixer ORPO iter-2 conservative dataset built ===") |
| print(f" bad_flip_pairs: {len(bad_flip_pairs)}") |
| print(f" good_flip_pairs: {len(good_flip_pairs)} (x3 → {len(good_flip_aug)})") |
| print(f" same_correct pairs: {len(same_correct_pairs)}") |
| print(f" Total train: {len(train)}, test: {len(test)}") |
| print(f" Saved to {OUT_DIR}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|