Skip to content

Commit 3e464c3

Browse files
author
张志鹏
committed
Add eval optimize loop example
1 parent 099b571 commit 3e464c3

17 files changed

Lines changed: 5084 additions & 3 deletions
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Design note
2+
3+
This example runs a closed loop:
4+
5+
1. Convert train and validation references into trace evalsets.
6+
2. Evaluate baseline traces with `AgentEvaluator`.
7+
3. Attribute failures by response mismatch, tool call error, argument error, rubric failure, knowledge recall gap, and format noncompliance.
8+
4. Optimize `TargetPrompt` candidates.
9+
5. Regenerate candidate traces from the candidate prompt text.
10+
6. Compare baseline and candidate per case, apply the configured gate, and write audit artifacts.
11+
12+
The default backend is `fake`, so the example stays runnable without network access or API keys. The fake optimizer appends deterministic prompt rules such as `STRICT_JSON_OUTPUT`, `STRICT_TOOL_ARGUMENTS`, and `TRAINING_PATTERN_BIAS`.
13+
14+
The optional `agent_optimizer` backend calls `AgentOptimizer.optimize(...)` with the same `TargetPrompt` files. It uses response-only metrics during GEPA black-box optimization, then reruns the full trace-mode report metrics afterward, including tool trajectory. Secrets are read from environment variables only and are not written to checked-in config or reports.

0 commit comments

Comments
 (0)