|
| 1 | +# TRM DAG Construction |
| 2 | + |
| 3 | +Build DAGs over reasoning traces. |
| 4 | + |
| 5 | +> Example / reference implementation. Defaults reflect one specific setup — swap pieces out for your own data and models. |
| 6 | +
|
| 7 | +## Pipeline |
| 8 | + |
| 9 | +Three steps, in order: |
| 10 | + |
| 11 | +```text |
| 12 | +(1) partition -> (2) build_dag -> (3) merge_view |
| 13 | +``` |
| 14 | + |
| 15 | +1. **`partition`** — split a raw `trace` into an ordered list of steps. |
| 16 | +2. **`build_dag`** — for each new step, ask an LLM to classify how it attaches to earlier steps as `continue` / `backtrack` / `merge`, then validate and majority-vote into parents. |
| 17 | +3. **`merge_view`** — collapse linear `continue` chains into super-nodes. |
| 18 | + |
| 19 | +## Quickstart |
| 20 | + |
| 21 | +```bash |
| 22 | +cd dag_construction |
| 23 | +pip install -e ".[dev]" |
| 24 | +cp .env.example .env |
| 25 | +# Fill DAG_CONSTRUCTION_API_KEY, DAG_CONSTRUCTION_MODEL, optional DAG_CONSTRUCTION_BASE_URL. |
| 26 | +python examples/run_single.py |
| 27 | +``` |
| 28 | + |
| 29 | +Batch JSONL: |
| 30 | + |
| 31 | +```bash |
| 32 | +python -m trm_dag build --input items.jsonl --output out.jsonl --num-threads 8 --resume |
| 33 | +``` |
| 34 | + |
| 35 | +## Customizing each step |
| 36 | + |
| 37 | +### 1. `partition` |
| 38 | + |
| 39 | +- Different keywords: `partition_keyword(..., keywords=("Step", "Stage", ...))` or CLI `--keywords ...`. |
| 40 | +- Skip it: pass rows with `"steps": [...]` directly to `build_dag_batch`. |
| 41 | +- Custom splitter: write your own `(steps, fillers)` function and feed `"steps"` in. See `trm_dag/partition.py` for the contract. |
| 42 | + |
| 43 | +### 2. `build_dag` |
| 44 | + |
| 45 | +- Prompt: edit `trm_dag/prompts/dag_construction.txt` (keep the `$current_id`, `$input_steps`, `$available_steps`, `$leaf_steps`, `$last_previous_step_id` placeholders). |
| 46 | +- LLM backend: any OpenAI-compatible endpoint via `DAG_CONSTRUCTION_BASE_URL` / `DAG_CONSTRUCTION_MODEL`. For non-OpenAI APIs, implement the `ChatClient` protocol in `trm_dag/client.py` and pass `client=` to `build_dag` / `build_dag_batch`. |
| 47 | +- Sampling: `OpenAIConfig(temperature, top_p, n, max_tokens)` and `DagParams(regen_limit)`, or matching CLI flags. |
| 48 | +- Attachment pool size: `DagParams(main_path_cap=..., other_leaf_cap=...)`. |
| 49 | +- Action set / voting: edit `parse_action_previous`, `_validate_candidate`, `majority_vote` in `trm_dag/components.py` (and the prompt). |
| 50 | +- Determinism: `DagParams(random_seed=0)`. |
| 51 | + |
| 52 | +### 3. `merge_view` |
| 53 | + |
| 54 | +- Different merge rule: edit `collapse_continue_chains` in `trm_dag/components.py` (`is_continue_edge`, `is_chain_head`). |
| 55 | +- Skip it: just read `result["dag_graph_raw"]`. |
| 56 | +- Per-super-node summarization: post-process `dag_graph_merged["merged_nodes"]` with your own summarizer. |
| 57 | + |
| 58 | +## Python API |
| 59 | + |
| 60 | +```python |
| 61 | +from trm_dag import DagParams, OpenAIConfig, build_dag |
| 62 | + |
| 63 | +result = build_dag( |
| 64 | + prompt, |
| 65 | + steps, |
| 66 | + openai_config=OpenAIConfig.from_env(), |
| 67 | + dag_params=DagParams(random_seed=0), |
| 68 | +) |
| 69 | + |
| 70 | +raw = result["dag_graph_raw"] |
| 71 | +merged = result["dag_graph_merged"] |
| 72 | +``` |
| 73 | + |
| 74 | +Batch: |
| 75 | + |
| 76 | +```python |
| 77 | +from trm_dag import DagParams, OpenAIConfig, build_dag_batch |
| 78 | + |
| 79 | +rows = [ |
| 80 | + {"prompt": "...", "steps": ["Step one.", "Then two."]}, |
| 81 | + {"prompt": "...", "trace": "Step one.\n\nThen two."}, |
| 82 | +] |
| 83 | + |
| 84 | +out_rows = build_dag_batch( |
| 85 | + rows, |
| 86 | + openai_config=OpenAIConfig.from_env(), |
| 87 | + dag_params=DagParams(regen_limit=5, random_seed=0), |
| 88 | + output_path="items_dag.jsonl", |
| 89 | + resume=True, |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | +## CLI |
| 94 | + |
| 95 | +```bash |
| 96 | +python -m trm_dag build --input items.jsonl --output items_dag.jsonl |
| 97 | +``` |
| 98 | + |
| 99 | +Build flags: `--input`, `--output`, `--num-threads`, `--resume` / `--no-resume` / `--rebuild`, `--keywords`, `--regen-limit`, `--main-path-cap`, `--other-leaf-cap`, `--random-seed`. |
| 100 | + |
| 101 | +LLM flags: `--api-key`, `--base-url`, `--model`, `--temperature`, `--top-p`, `--max-tokens`, `--n`, `--timeout`. |
| 102 | + |
| 103 | +## Input And Output |
| 104 | + |
| 105 | +Input rows can contain pre-split steps: |
| 106 | + |
| 107 | +```json |
| 108 | +{"prompt": "...", "steps": ["Step one.", "Then two."]} |
| 109 | +``` |
| 110 | + |
| 111 | +or a raw trace: |
| 112 | + |
| 113 | +```json |
| 114 | +{"prompt": "...", "trace": "Step one.\\n\\nThen two."} |
| 115 | +``` |
| 116 | + |
| 117 | +Output rows add `dag_graph_raw`, `dag_graph_merged`, `dag_parse_errors`, `dag_usage`. When input contains `trace`, `build_dag_batch` also keeps generated `steps` and `fillers` on the output row. |
0 commit comments