Spaces:
Running on Zero
Running on Zero
File size: 2,078 Bytes
3fbd1fd | 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 | """Load Spider examples and build prompts for the model."""
import json
import sqlite3
from contextlib import closing
from pathlib import Path
SPIDER_DIR = Path(__file__).resolve().parent.parent / "data" / "spider_data"
DB_DIR = SPIDER_DIR / "database"
SYSTEM_PROMPT = (
"You are a SQLite expert. Given a database schema and a question, "
"write one SQLite query that answers it. "
"Put only the query inside <sql></sql> tags."
)
SPLIT_FILES = {"train": "train_spider.json", "dev": "dev.json"}
def load_split(split: str) -> list[dict]:
with open(SPIDER_DIR / SPLIT_FILES[split]) as f:
raw = json.load(f)
return [
{"db_id": ex["db_id"], "question": ex["question"], "gold_sql": ex["query"]}
for ex in raw
]
def db_path(db_id: str) -> Path:
return DB_DIR / db_id / f"{db_id}.sqlite"
_schema_cache: dict[str, str] = {}
def get_schema(db_id: str) -> str:
"""Return the CREATE TABLE statements for a database (cached)."""
if db_id not in _schema_cache:
uri = f"file:{db_path(db_id)}?mode=ro"
with closing(sqlite3.connect(uri, uri=True)) as conn:
rows = conn.execute(
"SELECT sql FROM sqlite_master "
"WHERE type='table' AND sql IS NOT NULL "
"AND name NOT LIKE 'sqlite_%'"
).fetchall()
_schema_cache[db_id] = "\n\n".join(r[0].strip() for r in rows)
return _schema_cache[db_id]
def build_prompt(example: dict) -> list[dict]:
"""Chat-format prompt, the format GRPOTrainer expects."""
schema = get_schema(example["db_id"])
return [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {example['question']}"},
]
if __name__ == "__main__":
train, dev = load_split("train"), load_split("dev")
print(f"train: {len(train)} dev: {len(dev)}")
ex = dev[0]
print("\n--- PROMPT ---")
for msg in build_prompt(ex):
print(f"[{msg['role']}]\n{msg['content']}\n")
print("--- GOLD SQL ---\n", ex["gold_sql"]) |