Imp.Optimizer.InferRules (Imp v0.5.0)

Copy Markdown View Source

Induces natural-language rules from observed examples and selects them on a validation set.

This follows DSPy 3.2.1 InferRules' semantic loop: it first runs BootstrapFewShot, formats each predictor's observed input/output examples, asks a fresh rule-induction program for actionable rules, appends those rules to the predictor's instructions, and evaluates each candidate. Imp retains both the supplied source program and the bootstrapped baseline as safety candidates. The source is evaluated first, so neither bootstrapping nor rule induction can silently regress it on the selection set.

Candidate programs and signatures remain immutable and isolated, so a later proposal cannot rewrite an already selected candidate through shared Python signature-class state. Context-window failures retry with progressively fewer examples, matching upstream's user-visible recovery policy. Proposal and evaluation failures, including exhausted context retries, are retained in the optimizer report instead of aborting the whole compile. Imp also reuses a logical call's sequential rollout ID while its prompt shrinks; DSPy draws a fresh random rollout ID for every retry.

Operational route, cost, budget, transport, and cancellation guards are never candidate-local: they remain fatal through both rule induction and candidate evaluation.

Pass :rule_lm (or :prompt_lm) to keep rule induction separate from the task LM. Without one, the program's bound LM is used. :candidates accepts already-induced rule strings and is useful for deterministic replay.

Durable runs accept :max_operations, :checkpoint_fn, and :resume_state at compile time. Operations seal the bootstrap, each predictor's rule proposal, and each whole candidate evaluation. Anonymous or captured metrics require a stable JSON-safe :metric_identity; complete in-process runs remain available without one and explicitly produce no resume state.

Summary

Functions

new(metric, opts \\ [])