|
| 1 | +# How RuleChef Works |
| 2 | + |
| 3 | +RuleChef turns labeled examples into fast, deterministic rules. The key idea: **an LLM writes the rules, but the rules run without any LLM**. |
| 4 | + |
| 5 | +```mermaid |
| 6 | +flowchart LR |
| 7 | + A[Your Examples] -->|Learning time| B[LLM writes rules] |
| 8 | + B --> C[Regex / Code / spaCy rules] |
| 9 | + C -->|Inference time| D["Fast local execution\n< 1ms, no API calls"] |
| 10 | +``` |
| 11 | + |
| 12 | +This page explains the core architecture. For API details, see the [Quick Start](quickstart.md). |
| 13 | + |
| 14 | +## The Pipeline |
| 15 | + |
| 16 | +When you call `chef.learn_rules()`, here's what happens: |
| 17 | + |
| 18 | +```mermaid |
| 19 | +flowchart TD |
| 20 | + A["1. Buffer\n(collected examples)"] --> B["2. Commit\n(buffer → dataset)"] |
| 21 | + B --> C["3. Synthesis\n(LLM generates rules)"] |
| 22 | + C --> D["4. Evaluation\n(test rules vs examples)"] |
| 23 | + D -->|Failures exist| E["5. Refinement\n(LLM patches rules)"] |
| 24 | + E --> D |
| 25 | + D -->|Good enough| F["6. Persist\n(save rules to disk)"] |
| 26 | +``` |
| 27 | + |
| 28 | +Each step in detail: |
| 29 | + |
| 30 | +### 1. Buffer |
| 31 | + |
| 32 | +When you call `add_example()` or `add_correction()`, data goes into a **buffer** — not directly into the dataset. This lets you collect many examples before learning, and gives the [coordinator](../guide/coordinators.md) a chance to decide *when* and *how* to learn. |
| 33 | + |
| 34 | +```python |
| 35 | +chef.add_example(input1, output1) # → buffer |
| 36 | +chef.add_example(input2, output2) # → buffer |
| 37 | +chef.add_correction(input3, wrong, correct) # → buffer (high priority) |
| 38 | + |
| 39 | +# Nothing has been learned yet — examples are waiting in the buffer |
| 40 | +``` |
| 41 | + |
| 42 | +### 2. Commit |
| 43 | + |
| 44 | +When `learn_rules()` is called, buffered examples are committed to the dataset. The dataset is the permanent store — it persists to disk and accumulates across learning rounds. |
| 45 | + |
| 46 | +### 3. Synthesis |
| 47 | + |
| 48 | +The LLM receives a prompt containing: |
| 49 | + |
| 50 | +- **Task description** — what you're trying to do |
| 51 | +- **Training examples** — input/output pairs from the dataset |
| 52 | +- **Data evidence** — regex pattern hints from [grex](../guide/advanced.md#using-grex-for-regex-suggestions) (if enabled) |
| 53 | +- **User feedback** — any guidance you've added |
| 54 | +- **Format instructions** — how to write regex/code/spaCy rules |
| 55 | + |
| 56 | +The LLM returns a set of rules in JSON format. Each rule has a pattern (regex, code function, or spaCy matcher) and an output template. |
| 57 | + |
| 58 | +For multi-class tasks (NER, classification), synthesis can run **per-class** — generating rules for each label separately for better coverage. |
| 59 | + |
| 60 | +### 4. Evaluation |
| 61 | + |
| 62 | +Rules are tested against the dataset. RuleChef computes: |
| 63 | + |
| 64 | +- Per-example: did the rules produce the correct output? |
| 65 | +- Per-class: precision, recall, F1 for each label |
| 66 | +- Overall: micro/macro F1, exact match accuracy |
| 67 | + |
| 68 | +Any examples where rules fail are collected as **failures**. |
| 69 | + |
| 70 | +### 5. Refinement |
| 71 | + |
| 72 | +If failures exist and iterations remain, RuleChef sends the LLM a **patch prompt** containing: |
| 73 | + |
| 74 | +- Current rules (what's already working) |
| 75 | +- Specific failures (what's broken, with expected vs actual output) |
| 76 | +- Coordinator guidance (which classes to focus on) |
| 77 | + |
| 78 | +The LLM generates **patch rules** targeted at the failures. These are merged into the existing ruleset — stable rules are preserved. |
| 79 | + |
| 80 | +### 6. Persist |
| 81 | + |
| 82 | +The final rules are saved to disk as JSON. When you create a new `RuleChef` with the same `dataset_name` and `storage_path`, rules load automatically. |
| 83 | + |
| 84 | +## What Are Rules? |
| 85 | + |
| 86 | +A rule is a pattern that maps input text to structured output. There are three formats: |
| 87 | + |
| 88 | +### Regex Rules |
| 89 | + |
| 90 | +A regex pattern with an output template. When the pattern matches, the template fills in the output: |
| 91 | + |
| 92 | +``` |
| 93 | +Pattern: (?i)\b(\d+\s*mg)\b |
| 94 | +Template: {"text": "$0", "type": "DOSAGE", "start": "$start", "end": "$end"} |
| 95 | +``` |
| 96 | + |
| 97 | +`$0` is the full match, `$1`/`$2` are capture groups, `$start`/`$end` are character offsets. The rule engine computes positions — the LLM never predicts offsets. |
| 98 | + |
| 99 | +### Code Rules |
| 100 | + |
| 101 | +A Python function that takes input data and returns structured output: |
| 102 | + |
| 103 | +```python |
| 104 | +def extract(input_data): |
| 105 | + import re |
| 106 | + text = input_data["text"] |
| 107 | + spans = [] |
| 108 | + for m in re.finditer(r'\b[A-Z][a-z]+ine\b', text): |
| 109 | + spans.append({"text": m.group(), "start": m.start(), "end": m.end(), "type": "DRUG"}) |
| 110 | + return spans |
| 111 | +``` |
| 112 | + |
| 113 | +Code rules run in a [restricted sandbox](../guide/advanced.md#code-rule-security) — no imports, no file access, no network calls. |
| 114 | + |
| 115 | +### spaCy Rules |
| 116 | + |
| 117 | +Token or dependency matcher patterns using linguistic attributes (POS tags, lemmas, dependency relations). Requires `pip install rulechef[spacy]`. |
| 118 | + |
| 119 | +## Schemas |
| 120 | + |
| 121 | +### Task Definition |
| 122 | + |
| 123 | +A `Task` tells RuleChef what you're trying to do: |
| 124 | + |
| 125 | +```python |
| 126 | +task = Task( |
| 127 | + name="Medical NER", |
| 128 | + description="Extract drugs, dosages, and conditions", |
| 129 | + input_schema={"text": "str"}, |
| 130 | + output_schema={"entities": "List[{text: str, start: int, end: int, type: DRUG|DOSAGE|CONDITION}]"}, |
| 131 | + type=TaskType.NER, |
| 132 | + text_field="text", |
| 133 | +) |
| 134 | +``` |
| 135 | + |
| 136 | +- **`input_schema`** — tells the LLM what input fields exist (so it writes `input_data["text"]` in code rules) |
| 137 | +- **`output_schema`** — tells the LLM what output structure to produce (entity fields, label names) |
| 138 | +- **`type`** — determines evaluation logic and prompt templates |
| 139 | +- **`text_field`** — which input field regex/spaCy rules match against |
| 140 | + |
| 141 | +Schemas are documentation for the LLM, not executed code. You can also use [Pydantic models](../guide/advanced.md#pydantic-output-schemas) for type-safe validation. |
| 142 | + |
| 143 | +## Coordinators |
| 144 | + |
| 145 | +A coordinator decides **when** to learn and **how** to guide refinement: |
| 146 | + |
| 147 | +| Coordinator | How It Decides | |
| 148 | +|-------------|---------------| |
| 149 | +| `SimpleCoordinator` | Threshold-based: learn after N examples, refine after M corrections | |
| 150 | +| `AgenticCoordinator` | LLM-guided: analyzes per-class metrics, focuses on weak classes, stops when performance plateaus | |
| 151 | + |
| 152 | +The agentic coordinator also supports [rule pruning](../guide/coordinators.md#rule-pruning) — merging redundant rules and removing noise after learning. |
| 153 | + |
| 154 | +See [Coordinators](../guide/coordinators.md) for details. |
| 155 | + |
| 156 | +## The LLM's Role |
| 157 | + |
| 158 | +The LLM is used **only during learning** — never during extraction: |
| 159 | + |
| 160 | +| Operation | LLM Used? | When | |
| 161 | +|-----------|-----------|------| |
| 162 | +| `add_example()` | No | Just stores in buffer | |
| 163 | +| `learn_rules()` | **Yes** | Synthesis + refinement prompts | |
| 164 | +| `extract()` | No | Runs rules locally | |
| 165 | +| `evaluate()` | No | Runs rules locally | |
| 166 | + |
| 167 | +After learning, the rules are self-contained. You can serialize them, ship them to a different machine, and run them without any LLM access. |
| 168 | + |
| 169 | +## Next Steps |
| 170 | + |
| 171 | +- [Quick Start](quickstart.md) — code examples for all task types |
| 172 | +- [Learning & Refinement](../guide/learning.md) — buffer architecture, sampling strategies, incremental patching |
| 173 | +- [Coordinators](../guide/coordinators.md) — simple vs agentic, rule pruning |
| 174 | +- [Evaluation & Feedback](../guide/evaluation.md) — metrics, corrections, custom matchers |
0 commit comments