Spaces:
Sleeping
Sleeping
Melika Kheirieh
commited on
Commit
Β·
ed681b1
1
Parent(s):
598536c
feat(benchmarks): align Spider eval with config-driven Pipeline and native Safety; log per-stage trace; add CSV summary
Browse files- benchmarks/evaluate_spider.py +180 -103
benchmarks/evaluate_spider.py
CHANGED
|
@@ -1,17 +1,22 @@
|
|
| 1 |
-
|
| 2 |
-
Evaluate NL2SQL pipeline performance on Spider-like queries.
|
| 3 |
-
Uses config-driven Pipeline, native Safety checks, and per-stage latency tracing.
|
| 4 |
-
Outputs: JSONL (detailed logs), JSON (metrics summary), and CSV (for README).
|
| 5 |
-
"""
|
| 6 |
|
| 7 |
-
import json
|
| 8 |
import csv
|
|
|
|
|
|
|
| 9 |
import time
|
| 10 |
from pathlib import Path
|
| 11 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
"list all customers",
|
| 16 |
"show total invoices per country",
|
| 17 |
"top 3 albums by total sales",
|
|
@@ -19,107 +24,179 @@ DATASET = [
|
|
| 19 |
"number of employees per city",
|
| 20 |
]
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
print(f"\nπ§ Query: {q}")
|
| 44 |
-
start = time.perf_counter()
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
"query": q,
|
| 69 |
-
"ok":
|
| 70 |
-
"latency_ms":
|
| 71 |
"trace": stages,
|
| 72 |
"error": None,
|
| 73 |
}
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
{
|
| 80 |
"query": q,
|
| 81 |
"ok": False,
|
| 82 |
-
"latency_ms":
|
| 83 |
"trace": [],
|
| 84 |
-
"error": str(
|
| 85 |
}
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
#
|
| 90 |
-
avg_latency =
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
import csv
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
import time
|
| 7 |
from pathlib import Path
|
| 8 |
+
from typing import Any, Dict, List, Optional
|
| 9 |
+
|
| 10 |
+
# Reuse existing factories from your FastAPI router (no new DI needed)
|
| 11 |
+
from app.routers.nl2sql import ( # type: ignore
|
| 12 |
+
_pipeline as DEFAULT_PIPELINE,
|
| 13 |
+
_build_pipeline,
|
| 14 |
+
_select_adapter,
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# -------------------- Config --------------------
|
| 18 |
+
|
| 19 |
+
DATASET: List[str] = [
|
| 20 |
"list all customers",
|
| 21 |
"show total invoices per country",
|
| 22 |
"top 3 albums by total sales",
|
|
|
|
| 24 |
"number of employees per city",
|
| 25 |
]
|
| 26 |
|
| 27 |
+
# DB id/mode follows your router convention; adjust if needed
|
| 28 |
+
DB_ID: str = os.getenv("DB_MODE", "sqlite")
|
| 29 |
+
|
| 30 |
+
# Results directory with timestamped subfolder (keeps previous runs)
|
| 31 |
+
RESULT_ROOT = Path("benchmarks") / "results"
|
| 32 |
+
TIMESTAMP = time.strftime("%Y%m%d-%H%M%S")
|
| 33 |
+
RESULT_DIR = RESULT_ROOT / TIMESTAMP
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# -------------------- Helpers --------------------
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def _int_ms(start: float) -> int:
|
| 40 |
+
return int((time.perf_counter() - start) * 1000)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _derive_schema_preview_safe(pipeline_obj: Any) -> Optional[str]:
|
| 44 |
+
"""
|
| 45 |
+
Try to derive schema preview from the adapter/executor if such a method exists.
|
| 46 |
+
Kept intentionally permissive to avoid tight coupling.
|
| 47 |
+
"""
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
+
# common places the adapter might live
|
| 50 |
+
candidates: List[Any] = [
|
| 51 |
+
getattr(pipeline_obj, "executor", None),
|
| 52 |
+
getattr(pipeline_obj, "adapter", None),
|
| 53 |
+
]
|
| 54 |
+
for c in candidates:
|
| 55 |
+
if c and hasattr(c, "derive_schema_preview"):
|
| 56 |
+
return c.derive_schema_preview() # type: ignore[no-any-return, call-arg]
|
| 57 |
+
except Exception:
|
| 58 |
+
pass
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _to_stage_list(trace_obj: Any) -> List[Dict[str, Any]]:
|
| 63 |
+
"""
|
| 64 |
+
Normalize pipeline trace (list of dataclass or dict) to a list of dicts:
|
| 65 |
+
[{ "stage": str, "ms": int }, ...]
|
| 66 |
+
"""
|
| 67 |
+
stages: List[Dict[str, Any]] = []
|
| 68 |
+
if not isinstance(trace_obj, list):
|
| 69 |
+
return stages
|
| 70 |
+
|
| 71 |
+
for t in trace_obj:
|
| 72 |
+
if isinstance(t, dict):
|
| 73 |
+
stage = t.get("stage", "?")
|
| 74 |
+
ms = t.get("duration_ms", 0)
|
| 75 |
+
else:
|
| 76 |
+
stage = getattr(t, "stage", "?")
|
| 77 |
+
ms = getattr(t, "duration_ms", 0)
|
| 78 |
+
try:
|
| 79 |
+
stages.append({"stage": str(stage), "ms": int(ms)})
|
| 80 |
+
except Exception:
|
| 81 |
+
stages.append({"stage": str(stage), "ms": 0})
|
| 82 |
+
return stages
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# -------------------- Main --------------------
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def main() -> None:
|
| 89 |
+
RESULT_DIR.mkdir(parents=True, exist_ok=True)
|
| 90 |
+
|
| 91 |
+
# Build pipeline from router factories (no new DI required)
|
| 92 |
+
try:
|
| 93 |
+
adapter = _select_adapter(DB_ID) # e.g., "sqlite" / "postgres"
|
| 94 |
+
pipeline = _build_pipeline(adapter)
|
| 95 |
+
using_default = False
|
| 96 |
+
except Exception:
|
| 97 |
+
pipeline = DEFAULT_PIPELINE
|
| 98 |
+
using_default = True
|
| 99 |
+
|
| 100 |
+
print(
|
| 101 |
+
f"β
Pipeline ready "
|
| 102 |
+
f"(db_id={DB_ID}, source={'default' if using_default else 'custom adapter'})"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Optional schema preview
|
| 106 |
+
schema_preview = _derive_schema_preview_safe(pipeline)
|
| 107 |
+
if schema_preview:
|
| 108 |
+
print("π Derived schema preview β")
|
| 109 |
+
else:
|
| 110 |
+
print("βΉοΈ No schema preview (adapter does not expose it or not needed)")
|
| 111 |
+
|
| 112 |
+
# Evaluate
|
| 113 |
+
records: List[Dict[str, Any]] = []
|
| 114 |
+
for q in DATASET:
|
| 115 |
+
print(f"\nπ§ Query: {q}")
|
| 116 |
+
t0 = time.perf_counter()
|
| 117 |
+
try:
|
| 118 |
+
result = pipeline.run(
|
| 119 |
+
user_query=q,
|
| 120 |
+
schema_preview=schema_preview or "", # <- force str
|
| 121 |
+
)
|
| 122 |
+
latency_ms = _int_ms(t0)
|
| 123 |
+
|
| 124 |
+
# ok flag -> coerce to bool for mypy and consistency
|
| 125 |
+
ok_flag = bool(getattr(result, "ok", True))
|
| 126 |
+
stages = _to_stage_list(getattr(result, "trace", None))
|
| 127 |
+
|
| 128 |
+
rec: Dict[str, Any] = {
|
| 129 |
"query": q,
|
| 130 |
+
"ok": ok_flag,
|
| 131 |
+
"latency_ms": latency_ms,
|
| 132 |
"trace": stages,
|
| 133 |
"error": None,
|
| 134 |
}
|
| 135 |
+
records.append(rec)
|
| 136 |
+
print(f"β
Success ({latency_ms} ms)")
|
| 137 |
+
except Exception as exc:
|
| 138 |
+
latency_ms = _int_ms(t0)
|
| 139 |
+
rec = {
|
|
|
|
| 140 |
"query": q,
|
| 141 |
"ok": False,
|
| 142 |
+
"latency_ms": latency_ms,
|
| 143 |
"trace": [],
|
| 144 |
+
"error": str(exc),
|
| 145 |
}
|
| 146 |
+
records.append(rec)
|
| 147 |
+
print(f"β Failed: {exc!s} ({latency_ms} ms)")
|
| 148 |
+
|
| 149 |
+
# Aggregate metrics
|
| 150 |
+
avg_latency = (
|
| 151 |
+
round(sum(r["latency_ms"] for r in records) / max(len(records), 1), 1)
|
| 152 |
+
if records
|
| 153 |
+
else 0.0
|
| 154 |
+
)
|
| 155 |
+
success_rate = (
|
| 156 |
+
sum(1 for r in records if bool(r.get("ok"))) / max(len(records), 1)
|
| 157 |
+
if records
|
| 158 |
+
else 0.0
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
summary: Dict[str, Any] = {
|
| 162 |
+
"queries_total": len(records),
|
| 163 |
+
"success_rate": success_rate,
|
| 164 |
+
"avg_latency_ms": avg_latency,
|
| 165 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 166 |
+
"db_id": DB_ID,
|
| 167 |
+
"pipeline_source": "default" if using_default else "adapter",
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
# Persist outputs
|
| 171 |
+
jsonl_path = RESULT_DIR / "spider_eval.jsonl"
|
| 172 |
+
with jsonl_path.open("w", encoding="utf-8") as f:
|
| 173 |
+
for r in records:
|
| 174 |
+
json.dump(r, f, ensure_ascii=False)
|
| 175 |
+
f.write("\n")
|
| 176 |
+
|
| 177 |
+
summary_path = RESULT_DIR / "metrics_summary.json"
|
| 178 |
+
with summary_path.open("w", encoding="utf-8") as f:
|
| 179 |
+
json.dump(summary, f, indent=2)
|
| 180 |
+
|
| 181 |
+
csv_path = RESULT_DIR / "results.csv"
|
| 182 |
+
with csv_path.open("w", newline="", encoding="utf-8") as f:
|
| 183 |
+
writer = csv.DictWriter(f, fieldnames=["query", "ok", "latency_ms"])
|
| 184 |
+
writer.writeheader()
|
| 185 |
+
for r in records:
|
| 186 |
+
writer.writerow(
|
| 187 |
+
{
|
| 188 |
+
"query": r["query"],
|
| 189 |
+
"ok": "β
" if bool(r["ok"]) else "β",
|
| 190 |
+
"latency_ms": int(r["latency_ms"]),
|
| 191 |
+
}
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
print(
|
| 195 |
+
"\nπΎ Saved outputs:\n"
|
| 196 |
+
f"- {jsonl_path}\n- {summary_path}\n- {csv_path}\n"
|
| 197 |
+
f"π Avg latency: {avg_latency} ms | Success rate: {success_rate:.0%}"
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
|
| 201 |
+
if __name__ == "__main__":
|
| 202 |
+
main()
|