Datasets:
Download scripts/selftest.py from YukiKaze1337/cn-sqlite-sft: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/datasets/YukiKaze1337/cn-sqlite-sft/resolve/main/scripts/selftest.py
- Command line
-
hf download hf://datasets/YukiKaze1337/cn-sqlite-sft/scripts/selftest.py
-
curl -L -o selftest.py https://huggingface.co/datasets/YukiKaze1337/cn-sqlite-sft/resolve/main/scripts/selftest.py
5.62 kB
| """开 GPU 之前的自检:不用显卡、不联网,几分钟跑完。哪一项没过就别花钱开机器。 | |
| python scripts/selftest.py | |
| 查这些: | |
| 1. 四个测试集:标准 SQL 能跑、结果不空、没有并列导致的结果不唯一、没有重复题(check_testset.py) | |
| 2. 评分本身:标准答案当模型输出判对;故意改坏的 SQL 判错;多返回几列宽松判对、严格判错; | |
| ROUND 过的数字、'2024' 和 2024 判对;抽 SQL 能处理代码块、CTE、前后的废话 | |
| 3. 训练集能从 data/pairs + data/new 原样重建出来(数据、prompt、取值提示一个字都不差) | |
| 4. 训练集里没有测试库的题、没有跑不通的 SQL | |
| 5. 装了 sqlglot 的话:测试集标准答案没有「一对多连接后重复求和」 | |
| """ | |
| import json | |
| import re | |
| import sqlite3 | |
| import sys | |
| import tempfile | |
| from pathlib import Path | |
| import build_sft | |
| from check_testset import check | |
| from eval import ROOT, extract_sql, judge, load_dbs, same_result, superset_result | |
| from seed_suite import make_suite | |
| # 已知、可以接受的问题:价格最贵的课在生成的数据里经常没人约,结果是 0,题是对的,只是区分度低 | |
| KNOWN = {("gym", "#17 价格最贵的那个课程被预约了几次?")} | |
| failed = [] | |
| def ok(name: str, cond: bool, detail: str = "") -> None: | |
| print((" 通过 " if cond else " 失败 ") + name + (f"({detail})" if detail and not cond else "")) | |
| if not cond: | |
| failed.append(name) | |
| def main() -> None: | |
| print("1. 测试集体检") | |
| for t in ("library", "gym", "hard", "transit"): | |
| items = [json.loads(l) for l in (ROOT / f"data/test_{t}.jsonl").read_text(encoding="utf-8").splitlines() if l.strip()] | |
| problems, _ = check(items) | |
| real = [p for p in problems if not any(t == k and p.startswith(q) for k, q in KNOWN)] | |
| ok(f"test_{t}({len(items)} 题)", not real, "; ".join(p.splitlines()[0] for p in real[:3])) | |
| print("2. 评分") | |
| cases = { | |
| "SELECT a FROM t;": "SELECT a FROM t", | |
| "```sql\nWITH x AS (SELECT 1) SELECT * FROM x;\n```": "WITH x AS (SELECT 1) SELECT * FROM x", | |
| "SQL:SELECT a FROM t; -- 说明": "SELECT a FROM t", | |
| "我不会": "", | |
| } | |
| ok("抽 SQL", all(extract_sql(src) == want for src, want in cases.items())) | |
| ok("ROUND 过的数字判对", same_result([("A", 0.28125)], [("A", 0.2813)], False)) | |
| ok("'2024' 和 2024 判对", same_result([("2024", 3)], [(2024, 3)], False)) | |
| ok("差得多的数字判错", not same_result([("A", 0.28125)], [("A", 0.29)], False)) | |
| ok("多返回一列:严格错、宽松对", | |
| not same_result([("x",)], [(1, "x")], False) and superset_result([("x",)], [(1, "x")], False)) | |
| ok("少返回一列:宽松也错", not superset_result([("x", 1)], [("x",)], False)) | |
| for t in ("library", "gym", "hard", "transit"): | |
| items = [json.loads(l) for l in (ROOT / f"data/test_{t}.jsonl").read_text(encoding="utf-8").splitlines() if l.strip()] | |
| db = items[0]["db"] | |
| _, conns = load_dbs(db, make_suite(db, [i["sql"] for i in items], 5)) | |
| gold_ok = sum(judge(i["sql"], i["sql"], conns)[1] for i in items) | |
| ok(f"test_{t} 标准答案当输出全判对", gold_ok == len(items), f"{gold_ok}/{len(items)}") | |
| # 把第一个 WHERE / HAVING 条件换成永假,绝大多数应该判错 | |
| broken = [re.sub(r"(?i)\b(WHERE|HAVING)\b", r"\1 1 = 0 AND", i["sql"], count=1) for i in items] | |
| wrong = sum(judge(i["sql"], b, conns)[2] for i, b in zip(items, broken) if b != i["sql"]) | |
| ok(f"test_{t} 改坏的 SQL 基本判错", wrong <= 2, f"{wrong} 条改坏的被判对") | |
| print("3. 训练集能原样重建") | |
| with tempfile.TemporaryDirectory() as tmp: | |
| out = Path(tmp) / "train.jsonl" | |
| sys.argv = ["build_sft.py", "--out", str(out)] | |
| build_sft.main() | |
| same = out.read_bytes() == (ROOT / "data/train.jsonl").read_bytes() | |
| ok("build_sft 重建结果和 data/train.jsonl 一字不差", same, | |
| "数据或脚本改过,跑一遍 python scripts/build_sft.py") | |
| print("4. 训练集干净") | |
| rows = [json.loads(l) for l in (ROOT / "data/train.jsonl").read_text(encoding="utf-8").splitlines() if l.strip()] | |
| ok("没有测试库的题", not any(r["db"] in build_sft.TEST_DBS for r in rows)) | |
| broken = 0 | |
| for db in {r["db"] for r in rows}: | |
| conn = sqlite3.connect(":memory:") | |
| conn.executescript(build_sft.schema_path(db).read_text(encoding="utf-8")) | |
| for r in rows: | |
| if r["db"] == db: | |
| try: | |
| conn.execute(r["messages"][-1]["content"].rstrip(";")).fetchall() | |
| except sqlite3.Error: | |
| broken += 1 | |
| ok(f"{len(rows)} 条 SQL 都能在建表语句上跑", broken == 0, f"{broken} 条跑不通") | |
| print("5. 重复求和(需要 sqlglot)") | |
| try: | |
| from lint_fanout import fanout_problems | |
| except ImportError: | |
| print(" 跳过 没装 sqlglot(pip install sqlglot)") | |
| else: | |
| flagged = 0 | |
| for t in ("library", "gym", "hard", "transit"): | |
| for l in (ROOT / f"data/test_{t}.jsonl").read_text(encoding="utf-8").splitlines(): | |
| if l.strip(): | |
| it = json.loads(l) | |
| flagged += bool(fanout_problems(it["sql"], it["db"])) | |
| ok("测试集标准答案没有重复求和", flagged == 0, f"{flagged} 条") | |
| print("\n全部通过" if not failed else f"\n{len(failed)} 项没过:{failed}") | |
| sys.exit(1 if failed else 0) | |
| if __name__ == "__main__": | |
| main() | |