|
| 1 | +# Self Refinement |
| 2 | + |
| 3 | +> **Iterative candidate improvement with judge scores and a reflection trajectory (as a reusable subgraph)** |
| 4 | +
|
| 5 | +## Overview |
| 6 | + |
| 7 | +Self Refinement is a workflow pattern where SyGra: |
| 8 | + |
| 9 | +- Generates a candidate output for a given input prompt |
| 10 | +- Uses a separate *judge* step to score and critique the candidate |
| 11 | +- Optionally refines the candidate using the critique |
| 12 | +- Repeats until the candidate is accepted (meets a score threshold) or a maximum number of iterations is reached |
| 13 | + |
| 14 | +This feature is implemented as a reusable **subgraph recipe**: `sygra.recipes.self_refinement`. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## What you get |
| 19 | + |
| 20 | +When used in a task, the self-refinement subgraph produces: |
| 21 | + |
| 22 | +- `candidate`: the final accepted (or last) candidate |
| 23 | +- `self_refine_judge`: judge output containing score, pass/fail, critique, and raw parsed output |
| 24 | +- `reflection_trajectory`: an ordered list capturing each iteration’s candidate + judge result (useful for training / analysis) |
| 25 | + |
| 26 | +### Example output shape |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "candidate": "...final candidate...", |
| 31 | + "self_refine_judge": { |
| 32 | + "score": 4, |
| 33 | + "is_good": true, |
| 34 | + "critique": "...", |
| 35 | + "raw": {"score": 4, "is_good": true, "critique": "..."} |
| 36 | + }, |
| 37 | + "reflection_trajectory": [ |
| 38 | + { |
| 39 | + "iteration": 0, |
| 40 | + "candidate_key": "candidate", |
| 41 | + "candidate": "...candidate at iteration 0...", |
| 42 | + "judge": {"score": 2, "is_good": false, "critique": "...", "raw": {"...": "..."}} |
| 43 | + }, |
| 44 | + { |
| 45 | + "iteration": 1, |
| 46 | + "candidate_key": "candidate", |
| 47 | + "candidate": "...candidate at iteration 1...", |
| 48 | + "judge": {"score": 4, "is_good": true, "critique": "...", "raw": {"...": "..."}} |
| 49 | + } |
| 50 | + ] |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## How it works (recipe) |
| 57 | + |
| 58 | +The recipe lives at: |
| 59 | + |
| 60 | +- `sygra/recipes/self_refinement/graph_config.yaml` |
| 61 | +- `sygra/recipes/self_refinement/task_executor.py` |
| 62 | + |
| 63 | +### Nodes |
| 64 | + |
| 65 | +- `init_self_refine` (lambda) |
| 66 | + - Initializes internal loop state and `reflection_trajectory` |
| 67 | + - Uses internal state keys prefixed with `_self_refine_*` to minimize collision risk in host graphs |
| 68 | +- `generate_candidate` (llm) |
| 69 | + - Creates the initial candidate from `{prompt}` |
| 70 | +- `judge_candidate` (llm + post-process) |
| 71 | + - Returns JSON with `{score, is_good, critique}` |
| 72 | + - Post-processor normalizes and writes: |
| 73 | + - `self_refine_judge` |
| 74 | + - `self_refine_is_good` |
| 75 | + - `self_refine_critique` |
| 76 | +- `update_trajectory` (lambda) |
| 77 | + - Appends `{candidate, judge, iteration}` to `reflection_trajectory` |
| 78 | +- `refine_candidate` (llm) |
| 79 | + - Produces an improved candidate using `{self_refine_critique}` |
| 80 | + |
| 81 | +### Looping behavior |
| 82 | + |
| 83 | +A conditional edge (`SelfRefinementLoopCondition`) ends the subgraph when either: |
| 84 | + |
| 85 | +- The judge accepts (`self_refine_is_good == True`), or |
| 86 | +- The maximum number of refinement iterations is reached |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## Using self refinement as a subgraph in a task |
| 91 | + |
| 92 | +A typical pattern is to add a `subgraph` node in your task and point it at the recipe. |
| 93 | + |
| 94 | +The example task is located at: |
| 95 | + |
| 96 | +- `tasks/examples/self_refinement/graph_config.yaml` |
| 97 | +- `tasks/examples/self_refinement/input.json` |
| 98 | + |
| 99 | +### Example task config (high level) |
| 100 | + |
| 101 | +In `tasks/examples/self_refinement/graph_config.yaml`, the task defines a single node: |
| 102 | + |
| 103 | +- `self_refine`: |
| 104 | + - `node_type: subgraph` |
| 105 | + - `subgraph: sygra.recipes.self_refinement` |
| 106 | + - Overrides recipe node config via `node_config_map` (e.g., model selection, temperatures, and loop parameters) |
| 107 | + |
| 108 | +It also maps the key outputs into the final dataset using `output_config.output_map`: |
| 109 | + |
| 110 | +- `candidate` from `candidate` |
| 111 | +- `self_refine_judge` from `self_refine_judge` |
| 112 | +- `reflection_trajectory` from `reflection_trajectory` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## Running the example |
| 117 | + |
| 118 | +From the repo root: |
| 119 | + |
| 120 | +```bash |
| 121 | +python main.py --task examples.self_refinement --num_records 2 |
| 122 | +``` |
| 123 | + |
| 124 | +Artifacts: |
| 125 | + |
| 126 | +- Output records are written under `tasks/examples/self_refinement/` (typically `output_<timestamp>.json`) |
| 127 | +- Metadata is written under `tasks/examples/self_refinement/metadata/` (typically `metadata_tasks_examples_self_refinement_<timestamp>.json`) |
| 128 | + |
| 129 | +The metadata file includes per-node execution statistics (e.g., `self_refine.generate_candidate`, `self_refine.judge_candidate`) and aggregate token/cost summaries. |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +## Customization |
| 134 | + |
| 135 | +You can tune behavior either in the recipe or (recommended) from the task via `node_config_map`: |
| 136 | + |
| 137 | +- `init_self_refine.params.max_iterations` |
| 138 | +- `init_self_refine.params.score_threshold` |
| 139 | +- `judge_candidate.model.parameters.temperature` (usually `0` for consistent scoring) |
| 140 | +- Prompts for `generate_candidate`, `judge_candidate`, and `refine_candidate` |
| 141 | + |
| 142 | +If you need the recipe to write outputs under different keys (to avoid collisions in a larger host graph), you can override: |
| 143 | + |
| 144 | +- `update_trajectory.params.candidate_key` |
| 145 | +- `update_trajectory.params.judge_key` |
0 commit comments