|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""OpenAI/Codex API baseline for the Nullsec-S1 benchmark. |
| 3 | +
|
| 4 | +Requires OPENAI_API_KEY and OPENAI_MODEL (or --model). Generated reports and raw |
| 5 | +outputs are written under benchmarks/reports/baselines/openai/ and are not |
| 6 | +committed by default. |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import os |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 16 | + |
| 17 | +from benchmarks.baselines.api_common import run_api_baseline |
| 18 | + |
| 19 | + |
| 20 | +def require_env(name: str) -> str: |
| 21 | + value = os.environ.get(name) |
| 22 | + if not value: |
| 23 | + raise SystemExit(f"{name} is required. Export {name}=... before running this baseline.") |
| 24 | + return value |
| 25 | + |
| 26 | + |
| 27 | +def _extract_response_text(resp) -> str: |
| 28 | + text = getattr(resp, "output_text", None) |
| 29 | + if text: |
| 30 | + return text.strip() |
| 31 | + chunks = [] |
| 32 | + for item in getattr(resp, "output", []) or []: |
| 33 | + for content in getattr(item, "content", []) or []: |
| 34 | + if getattr(content, "type", None) in {"output_text", "text"}: |
| 35 | + chunks.append(getattr(content, "text", "")) |
| 36 | + return "\n".join(chunks).strip() |
| 37 | + |
| 38 | + |
| 39 | +def main() -> int: |
| 40 | + ap = argparse.ArgumentParser(description="OpenAI/Codex API baseline for the Nullsec-S1 benchmark") |
| 41 | + ap.add_argument("--dataset", default="detection.json") |
| 42 | + ap.add_argument("--limit", type=int, default=None, help="run only the first N cases (smoke test)") |
| 43 | + ap.add_argument("--model", default=os.environ.get("OPENAI_MODEL")) |
| 44 | + ap.add_argument("--resume", action="store_true", help="reuse cached raw_outputs.jsonl entries") |
| 45 | + ap.add_argument("--sleep", type=float, default=0.0, help="seconds to sleep between requests") |
| 46 | + ap.add_argument("--max-tokens", type=int, default=1536) |
| 47 | + args = ap.parse_args() |
| 48 | + |
| 49 | + api_key = require_env("OPENAI_API_KEY") |
| 50 | + if not args.model: |
| 51 | + raise SystemExit("OPENAI_MODEL is required (or pass --model).") |
| 52 | + |
| 53 | + try: |
| 54 | + from openai import OpenAI |
| 55 | + except ImportError as e: |
| 56 | + raise SystemExit("openai package is required. Install with: python -m pip install openai") from e |
| 57 | + |
| 58 | + client = OpenAI(api_key=api_key) |
| 59 | + |
| 60 | + def call(system_prompt: str, prompt: str) -> str: |
| 61 | + try: |
| 62 | + resp = client.responses.create( |
| 63 | + model=args.model, |
| 64 | + temperature=0, |
| 65 | + max_output_tokens=args.max_tokens, |
| 66 | + input=[ |
| 67 | + {"role": "system", "content": system_prompt}, |
| 68 | + {"role": "user", "content": prompt}, |
| 69 | + ], |
| 70 | + ) |
| 71 | + return _extract_response_text(resp) |
| 72 | + except AttributeError: |
| 73 | + # Compatibility with older SDKs that only expose chat.completions. |
| 74 | + resp = client.chat.completions.create( |
| 75 | + model=args.model, |
| 76 | + temperature=0, |
| 77 | + max_tokens=args.max_tokens, |
| 78 | + messages=[ |
| 79 | + {"role": "system", "content": system_prompt}, |
| 80 | + {"role": "user", "content": prompt}, |
| 81 | + ], |
| 82 | + ) |
| 83 | + return resp.choices[0].message.content.strip() |
| 84 | + |
| 85 | + run_api_baseline( |
| 86 | + provider_id="openai", |
| 87 | + system_name=f"OpenAI/Codex API baseline ({args.model})", |
| 88 | + dataset_name=args.dataset, |
| 89 | + model=args.model, |
| 90 | + limit=args.limit, |
| 91 | + resume=args.resume, |
| 92 | + sleep_s=args.sleep, |
| 93 | + call_model=call, |
| 94 | + provenance={"api": "openai_responses_or_chat", "max_tokens": args.max_tokens}, |
| 95 | + ) |
| 96 | + return 0 |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + raise SystemExit(main()) |
0 commit comments