File size: 10,149 Bytes
c509967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""CLI entry point for ChemGraph evaluation benchmarks.

Usage::

    # Quick local evaluation using a profile
    chemgraph eval --profile quick --models gpt-4o-mini --judge-model gpt-4o

    # Standard evaluation with LLM judge
    chemgraph eval --profile standard --models gpt-4o-mini gemini-2.5-flash

    # Minimal invocation (uses bundled default dataset)
    chemgraph-eval --models gpt-4o-mini --judge-model gpt-4o

    # Explicit dataset override
    chemgraph-eval \\
        --models gpt-4o-mini gemini-2.5-flash \\
        --dataset path/to/custom_ground_truth.json \\
        --judge-model gpt-4o \\
        --workflows single_agent \\
        --output-dir eval_results

    # Profile + override
    chemgraph eval --profile quick --models gpt-4o --max-queries 3
"""

import argparse
import asyncio
import sys
from typing import Optional

from chemgraph.eval.config import BenchmarkConfig
from chemgraph.eval.runner import ModelBenchmarkRunner


def add_eval_args(parser: argparse.ArgumentParser) -> None:
    """Add evaluation-specific arguments to an argument parser.

    This function is used by both the standalone ``chemgraph-eval``
    entry point and the ``chemgraph eval`` subcommand so that the
    argument interface is consistent.

    Parameters
    ----------
    parser : argparse.ArgumentParser
        Parser or subparser to receive evaluation arguments.
    """
    parser.add_argument(
        "--models",
        nargs="+",
        required=True,
        help="LLM model names to evaluate.",
    )
    parser.add_argument(
        "--judge-model",
        type=str,
        default=None,
        help=(
            "LLM model name for the judge. Required when "
            "--judge-type is 'llm' or 'both'."
        ),
    )
    parser.add_argument(
        "--profile",
        type=str,
        default=None,
        help=(
            "Evaluation profile name from config.toml [eval.profiles.*] "
            "(e.g. 'quick', 'standard'). Requires --config. "
            "CLI arguments override profile values."
        ),
    )
    parser.add_argument(
        "--dataset",
        type=str,
        default=None,
        help=(
            "Path to ground-truth JSON file. "
            "Defaults to the bundled dataset shipped with the package."
        ),
    )
    parser.add_argument(
        "--workflows",
        nargs="+",
        default=None,
        help="Workflow types to test (default: single_agent).",
    )
    parser.add_argument(
        "--output-dir",
        type=str,
        default="eval_results",
        help="Output directory for results (default: eval_results).",
    )
    parser.add_argument(
        "--report",
        choices=["json", "markdown", "console", "all"],
        default="all",
        help="Report format (default: all).",
    )
    parser.add_argument(
        "--no-structured-output",
        action="store_true",
        help="Disable structured output on the agent.",
    )
    parser.add_argument(
        "--judge-type",
        type=str,
        choices=["llm", "structured", "both"],
        default=None,
        help=(
            "Judge strategy: 'llm' (LLM-as-judge), 'structured' "
            "(deterministic structured-output comparison), or 'both' "
            "(run both judges). Default: llm."
        ),
    )
    parser.add_argument(
        "--recursion-limit",
        type=int,
        default=None,
        help="Max LangGraph recursion steps per query (default: 50).",
    )
    parser.add_argument(
        "--max-queries",
        type=int,
        default=None,
        help="Max number of queries to evaluate (0 = all, default: all).",
    )
    parser.add_argument(
        "--tags",
        nargs="*",
        default=[],
        help="Optional tags for the run metadata.",
    )
    parser.add_argument(
        "--resume",
        action="store_true",
        help=(
            "Resume from per-query checkpoint files, skipping "
            "already-completed (model, workflow, query) combinations."
        ),
    )
    parser.add_argument(
        "--config",
        type=str,
        default=None,
        help=(
            "Path to a TOML configuration file (e.g. config.toml). "
            "Provides model base_url, argo_user, and eval profiles."
        ),
    )


def _resolve_profile(args: argparse.Namespace) -> Optional[str]:
    """Resolve the eval profile name from CLI args and config file.

    If ``--profile`` is explicitly set, use it.  Otherwise, if
    ``--config`` is provided and the config file defines
    ``[eval] default_profile``, use that as the profile name.

    Returns ``None`` if no profile should be used.

    Parameters
    ----------
    args : argparse.Namespace
        Parsed evaluation arguments.

    Returns
    -------
    str or None
        Selected profile name, or ``None`` when no profile applies.
    """
    if args.profile:
        return args.profile

    if args.config:
        import toml
        from pathlib import Path

        p = Path(args.config)
        if p.exists():
            with open(p) as fh:
                raw = toml.load(fh)
            default = raw.get("eval", {}).get("default_profile")
            if default:
                profiles = raw.get("eval", {}).get("profiles", {})
                if default in profiles:
                    return default

    return None


def build_config_from_args(args: argparse.Namespace) -> BenchmarkConfig:
    """Build a ``BenchmarkConfig`` from parsed CLI arguments.

    Handles both profile-based and explicit-argument construction.
    When ``--config`` is provided without ``--profile``, the
    ``[eval] default_profile`` from the config file is used
    automatically if it exists.

    Parameters
    ----------
    args : argparse.Namespace
        Parsed evaluation arguments.

    Returns
    -------
    BenchmarkConfig
        Validated benchmark configuration.
    """
    profile = _resolve_profile(args)

    if profile:
        # Profile mode: requires --config
        config_file = args.config
        if not config_file:
            print(
                "Error: --config is required when using --profile.",
                file=sys.stderr,
            )
            sys.exit(1)

        # Collect CLI overrides (None values will be skipped by from_profile)
        overrides = {
            "output_dir": args.output_dir,
            "tags": args.tags or None,
        }
        if args.dataset is not None:
            overrides["dataset"] = args.dataset
        if args.workflows is not None:
            overrides["workflow_types"] = args.workflows
        if args.judge_model is not None:
            overrides["judge_model"] = args.judge_model
        if args.recursion_limit is not None:
            overrides["recursion_limit"] = args.recursion_limit
        if args.max_queries is not None:
            overrides["max_queries"] = args.max_queries
        if args.no_structured_output:
            overrides["structured_output"] = False
        if args.judge_type is not None:
            overrides["judge_type"] = args.judge_type
        if args.resume:
            overrides["resume"] = True

        config = BenchmarkConfig.from_profile(
            profile_name=profile,
            models=args.models,
            config_file=config_file,
            **overrides,
        )
    else:
        # Explicit mode: dataset defaults to the bundled ground truth
        # when --dataset is not provided.
        kwargs: dict = {
            "models": args.models,
            "workflow_types": args.workflows or ["single_agent"],
            "output_dir": args.output_dir,
            "structured_output": not args.no_structured_output,
            "recursion_limit": args.recursion_limit or 50,
            "tags": args.tags or [],
            "max_queries": args.max_queries or 0,
            "config_file": args.config,
            "judge_type": args.judge_type or "llm",
            "resume": args.resume,
        }
        if args.judge_model is not None:
            kwargs["judge_model"] = args.judge_model
        if args.dataset is not None:
            kwargs["dataset"] = args.dataset

        config = BenchmarkConfig(**kwargs)

    return config


def run_eval(args: argparse.Namespace) -> None:
    """Execute an evaluation benchmark from parsed CLI arguments.

    Parameters
    ----------
    args : argparse.Namespace
        Parsed evaluation arguments.
    """
    config = build_config_from_args(args)
    runner = ModelBenchmarkRunner(config)

    print("ChemGraph Evaluation Benchmark")
    if args.profile:
        print(f"  Profile:      {args.profile}")
    print(f"  Models:       {config.models}")
    print(f"  Workflows:    {config.workflow_types}")
    print(f"  Dataset:      {config.dataset}")
    print(f"  Judge Type:   {config.judge_type}")
    if config.judge_model:
        print(f"  Judge Model:  {config.judge_model}")
    if config.max_queries > 0:
        print(f"  Max Queries:  {config.max_queries}")
    if config.resume:
        print("  Resume:       enabled")
    if config.config_file:
        print(f"  Config:       {config.config_file}")
    print(f"  Output:       {config.output_dir}")
    print()

    asyncio.run(runner.run_all())
    runner.report(format=args.report)


def parse_args(argv=None) -> argparse.Namespace:
    """Parse arguments for the standalone ``chemgraph-eval`` command.

    Parameters
    ----------
    argv : list[str], optional
        Argument list to parse. Uses ``sys.argv`` when omitted.

    Returns
    -------
    argparse.Namespace
        Parsed command-line arguments.
    """
    parser = argparse.ArgumentParser(
        prog="chemgraph-eval",
        description="Run ChemGraph multi-model evaluation benchmarks.",
    )
    add_eval_args(parser)
    return parser.parse_args(argv)


def main(argv=None) -> None:
    """Standalone entry point for ``chemgraph-eval``.

    Parameters
    ----------
    argv : list[str], optional
        Argument list to parse. Uses ``sys.argv`` when omitted.
    """
    args = parse_args(argv)
    run_eval(args)


if __name__ == "__main__":
    main()