|
| 1 | +# Copyright 2025 The Flutter Authors. All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style license that can be |
| 3 | +# found in the LICENSE file. |
| 4 | + |
| 5 | +"""CLI entry point for running evaluations. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + run-evals --json ./eval_set.json |
| 9 | + run-evals --task my_task --model openai/gpt-4o --dataset samples.jsonl |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import logging |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from dotenv import load_dotenv |
| 18 | + |
| 19 | +# Import sandbox environments to register them with InspectAI |
| 20 | +# The @sandboxenv decorator registers the sandbox type when the module is imported |
| 21 | +import dash_evals.runner.sandboxes.podman.podman # noqa: F401 # Registers 'podman' |
| 22 | +from dash_evals.runner.args_runner import _run_from_args |
| 23 | +from dash_evals.runner.json_runner import run_from_json |
| 24 | + |
| 25 | +# Basic console logger for early startup messages |
| 26 | +logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 27 | +_startup_logger = logging.getLogger("startup") |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + """Parse command-line arguments and run evaluations.""" |
| 32 | + # Load .env from the repo root (walks up from cwd). |
| 33 | + # This populates os.environ with API keys, credentials, etc. |
| 34 | + # System env vars take precedence over .env values (python-dotenv default). |
| 35 | + load_dotenv(override=False) |
| 36 | + |
| 37 | + parser = argparse.ArgumentParser( |
| 38 | + description="Run Inspect AI evaluations for the Dart/Flutter plugin.", |
| 39 | + epilog="Example: run-evals --json ./eval_set.json", |
| 40 | + ) |
| 41 | + |
| 42 | + # ---------- JSON mode (mutually exclusive with direct args) ---------- |
| 43 | + parser.add_argument( |
| 44 | + "--json", |
| 45 | + type=Path, |
| 46 | + help="Path to eval_set.json (emitted by Dart CLI).", |
| 47 | + ) |
| 48 | + |
| 49 | + # ---------- Direct-args mode ---------- |
| 50 | + parser.add_argument( |
| 51 | + "--task", |
| 52 | + type=str, |
| 53 | + help="Task function name (e.g. 'flutter_code_gen' or dotted path).", |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--model", |
| 57 | + type=str, |
| 58 | + action="append", |
| 59 | + help="Model to evaluate (can be repeated). Example: openai/gpt-4o", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--dataset", |
| 63 | + type=Path, |
| 64 | + help="Path to a dataset file (JSON/JSONL/CSV).", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--log-dir", |
| 68 | + type=Path, |
| 69 | + help="Directory to write evaluation logs.", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--sandbox", |
| 73 | + type=str, |
| 74 | + nargs=2, |
| 75 | + metavar=("TYPE", "CONFIG"), |
| 76 | + help="Sandbox type and config path. Example: podman compose.yaml", |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "--max-connections", |
| 80 | + type=int, |
| 81 | + help="Maximum concurrent model connections.", |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + "--max-samples", |
| 85 | + type=int, |
| 86 | + help="Maximum concurrent samples per task.", |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + "--fail-on-error", |
| 90 | + type=float, |
| 91 | + help="Proportion of sample errors to tolerate (0.0-1.0).", |
| 92 | + ) |
| 93 | + |
| 94 | + args = parser.parse_args() |
| 95 | + |
| 96 | + # Ensure either --json or direct args are provided, but not both. |
| 97 | + direct_args_provided = any([args.task, args.model, args.dataset]) |
| 98 | + if args.json and direct_args_provided: |
| 99 | + parser.error( |
| 100 | + "Cannot combine --json with --task/--model/--dataset. Use one mode or the other." |
| 101 | + ) |
| 102 | + if not args.json and not direct_args_provided: |
| 103 | + parser.error("Provide either --json or at least --task and --model.") |
| 104 | + |
| 105 | + try: |
| 106 | + if args.json: |
| 107 | + has_failures = run_from_json(args.json) |
| 108 | + else: |
| 109 | + has_failures = _run_from_args(args) |
| 110 | + except Exception as e: |
| 111 | + _startup_logger.error(f"Failed to run evaluation: {e}") |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + sys.exit(1 if has_failures else 0) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments