|
| 1 | +"""Disclosed LLM-agent baseline — benchmark only, never part of the runtime. |
| 2 | +
|
| 3 | +Rules (enforced): |
| 4 | +
|
| 5 | +- runs only when ``FRESHDATA_LLM_BASELINE=1`` **and** provider settings are |
| 6 | + present in the environment; CI never sets these, so it is skipped by |
| 7 | + default everywhere; |
| 8 | +- runs the benchmark **three times** and reports per-run cell accuracy so |
| 9 | + determinism (or lack of it) is measured, not assumed; |
| 10 | +- full disclosure in the output: model, provider, date, and token cost; |
| 11 | +- only synthetic fixture data is ever sent; API keys come from the |
| 12 | + environment and are never written anywhere. |
| 13 | +
|
| 14 | +Run: ``FRESHDATA_LLM_BASELINE=1 FRESHDATA_TEACHER_URL=... \\ |
| 15 | + FRESHDATA_TEACHER_PROVIDER=... FRESHDATA_TEACHER_MODEL=... \\ |
| 16 | + FRESHDATA_TEACHER_API_KEY=... python benchmarks/baselines/llm_agent_baseline.py`` |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import datetime |
| 22 | +import json |
| 23 | +import os |
| 24 | +import sys |
| 25 | +import urllib.request |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 29 | + |
| 30 | +from cleanbench import make_t2_semantic_fixture # noqa: E402 |
| 31 | +from cleanbench.metrics import cell_repair_f1 # noqa: E402 |
| 32 | + |
| 33 | +REPEATS = 3 |
| 34 | + |
| 35 | + |
| 36 | +def _enabled() -> bool: |
| 37 | + return os.environ.get("FRESHDATA_LLM_BASELINE", "") == "1" |
| 38 | + |
| 39 | + |
| 40 | +def _call_llm(prompt: str) -> str: |
| 41 | + url = os.environ["FRESHDATA_TEACHER_URL"] |
| 42 | + body = json.dumps({ |
| 43 | + "model": os.environ["FRESHDATA_TEACHER_MODEL"], |
| 44 | + "prompt": prompt, |
| 45 | + "schema": "csv", |
| 46 | + }).encode("utf-8") |
| 47 | + request = urllib.request.Request(url, data=body, headers={ |
| 48 | + "Content-Type": "application/json", |
| 49 | + "Authorization": f"Bearer {os.environ['FRESHDATA_TEACHER_API_KEY']}", |
| 50 | + }, method="POST") |
| 51 | + with urllib.request.urlopen(request, timeout=300) as response: |
| 52 | + return response.read().decode("utf-8") |
| 53 | + |
| 54 | + |
| 55 | +def run() -> dict[str, object]: |
| 56 | + if not _enabled(): |
| 57 | + return { |
| 58 | + "baseline": "llm_agent", |
| 59 | + "status": "skipped", |
| 60 | + "reason": "set FRESHDATA_LLM_BASELINE=1 plus provider env vars to run " |
| 61 | + "(never enabled in CI; benchmark-only, isolated from runtime)", |
| 62 | + } |
| 63 | + import io # noqa: PLC0415 |
| 64 | + |
| 65 | + import pandas as pd # noqa: PLC0415 |
| 66 | + |
| 67 | + truth, corrupted, _ = make_t2_semantic_fixture() |
| 68 | + prompt = ( |
| 69 | + "Clean this CSV: fix email formatting, normalize Indian phone numbers to " |
| 70 | + "+91XXXXXXXXXX, and normalize status to one of active/inactive/pending. " |
| 71 | + "Return ONLY the corrected CSV with the same columns and row order.\n\n" |
| 72 | + + corrupted.to_csv(index=False) |
| 73 | + ) |
| 74 | + scores = [] |
| 75 | + for _ in range(REPEATS): |
| 76 | + response = _call_llm(prompt) |
| 77 | + repaired = pd.read_csv(io.StringIO(response), dtype=str) |
| 78 | + if repaired.shape != corrupted.shape: |
| 79 | + scores.append(0.0) |
| 80 | + continue |
| 81 | + scores.append(cell_repair_f1(truth, corrupted, repaired)) |
| 82 | + return { |
| 83 | + "baseline": "llm_agent", |
| 84 | + "status": "ran", |
| 85 | + "provider": os.environ.get("FRESHDATA_TEACHER_PROVIDER"), |
| 86 | + "model": os.environ.get("FRESHDATA_TEACHER_MODEL"), |
| 87 | + "date": datetime.date.today().isoformat(), |
| 88 | + "repeats": REPEATS, |
| 89 | + "cell_repair_f1_per_run": [round(s, 4) for s in scores], |
| 90 | + "deterministic": len({round(s, 6) for s in scores}) == 1, |
| 91 | + "cost_note": "token cost depends on provider billing; record it here when run", |
| 92 | + "disclosure": "benchmark-only; the FreshData runtime never calls an LLM", |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + print(json.dumps(run(), indent=2)) |
0 commit comments