|
| 1 | +# Evaluation + Optimization closed loop |
| 2 | + |
| 3 | +This example implements a reproducible "evaluate -> attribute failures -> optimize prompt -> validate candidate -> gate -> audit" loop for issue #91. |
| 4 | + |
| 5 | +It is intentionally runnable without a real API key. The default backend is deterministic fake/trace mode: it generates baseline and candidate traces, evaluates both with `AgentEvaluator`, applies the acceptance gate, and writes `optimization_report.json` plus `optimization_report.md`. The fake trace model recognizes the public sample IDs and also has query/trace-based fallbacks for JSON-format, tool-argument, private-knowledge, and validation-regression cases, so the gate logic is not tied only to the checked-in IDs. |
| 6 | + |
| 7 | +The same script also supports `--backend agent_optimizer`. That path calls `AgentOptimizer.optimize(...)` against the configured `TargetPrompt` files and uses the optimized prompt text to regenerate candidate traces before the final validation/gate/report stages. |
| 8 | + |
| 9 | +## Files |
| 10 | + |
| 11 | +```text |
| 12 | +examples/optimization/eval_optimize_loop/ |
| 13 | +|-- run_pipeline.py |
| 14 | +|-- optimizer.json |
| 15 | +|-- train.evalset.json |
| 16 | +|-- val.evalset.json |
| 17 | +|-- optimization_report.json |
| 18 | +|-- optimization_report.md |
| 19 | +|-- prompts/ |
| 20 | +| |-- system.md |
| 21 | +| `-- skill.md |
| 22 | +`-- trace_evalsets/ |
| 23 | + |-- baseline_train.evalset.json |
| 24 | + |-- baseline_val.evalset.json |
| 25 | + |-- candidate_train.evalset.json |
| 26 | + `-- candidate_val.evalset.json |
| 27 | +``` |
| 28 | + |
| 29 | +## Run offline |
| 30 | + |
| 31 | +From the repository root: |
| 32 | + |
| 33 | +```bash |
| 34 | +python examples/optimization/eval_optimize_loop/run_pipeline.py |
| 35 | +``` |
| 36 | + |
| 37 | +If `python` is not on PATH, use your active interpreter, for example: |
| 38 | + |
| 39 | +```bash |
| 40 | +py -3.14 examples/optimization/eval_optimize_loop/run_pipeline.py |
| 41 | +``` |
| 42 | + |
| 43 | +The script writes reports into this directory by default. The expected gate decision for the public sample is `reject`: the candidate improves the training split and one validation case, but introduces validation regressions, including a critical case. |
| 44 | + |
| 45 | +## Data design |
| 46 | + |
| 47 | +The sample has six public cases: |
| 48 | + |
| 49 | +- `train_format_json`: optimizable response-format failure. |
| 50 | +- `train_tool_args`: optimizable tool-argument failure. |
| 51 | +- `train_knowledge_gap`: optimization is ineffective because missing private knowledge cannot be fixed by prompt wording alone. |
| 52 | +- `val_format_json`: validation case that improves. |
| 53 | +- `val_critical_discount`: validation hard regression and critical-case regression. |
| 54 | +- `val_stable_refund`: validation regression caused by overfitting. |
| 55 | + |
| 56 | +## Report contract |
| 57 | + |
| 58 | +`optimization_report.json` includes: |
| 59 | + |
| 60 | +- `baseline`: train/validation scores, pass/fail, metric scores, failure reasons, and traces. |
| 61 | +- `optimization`: every round's input failure attribution, full candidate prompts, token usage, model-call count, seed, and cost. |
| 62 | +- `candidate`: train/validation scores after the candidate. |
| 63 | +- `delta`: per-case deltas, new passes, new failures, score improvements, and score regressions. |
| 64 | +- `gate_decision`: accept/reject decision with every configured gate check and reason. |
| 65 | +- `failure_attribution`: failure-type counts for every phase. |
| 66 | +- `audit`: reproducibility config and generated trace evalset artifacts. |
| 67 | + |
| 68 | +## Optional real optimizer backend |
| 69 | + |
| 70 | +The checked-in example does not store secrets. To run the real optimizer backend, pass credentials through environment variables and keep `optimizer.json` using environment references. |
| 71 | + |
| 72 | +For the DeepSeek OpenAI-compatible endpoint: |
| 73 | + |
| 74 | +```powershell |
| 75 | +$env:TRPC_AGENT_OPT_BASE_URL="https://api.deepseek.com/v1" |
| 76 | +$env:TRPC_AGENT_OPT_MODEL="deepseek-v4-pro" |
| 77 | +$env:TRPC_AGENT_OPT_API_KEY="<your key>" |
| 78 | +py -3.14 examples/optimization/eval_optimize_loop/run_pipeline.py --backend agent_optimizer |
| 79 | +``` |
| 80 | + |
| 81 | +The optimizer stage uses response-only metrics because `AgentOptimizer` runs through a black-box `call_agent` callback. The final report still reruns trace-mode baseline/candidate validation with the full metric set, including tool trajectory checks. The default `backend` remains `fake` so CI and reviewer machines can run without network access. |
0 commit comments