liveBook · Manning

InferRules

As explained in Building LLM Applications with DSPy

InferRules is a DSPy optimizer that adds demonstrations to prompts and generates rules from training examples to update prompt instructions.

InferRules optimizes DSPy instructions by adding demonstrations and generating rules from patterns in training examples. Because a rule can summarize patterns that take many examples to show, rules may reduce prompt tokens—if they can be discovered accurately and reliably. section 6.3

A classification rule

A generated rule might classify ground-transportation questions as “Ground Transportation Inquiry,” except questions about fees, which become “Ground Transportation Cost Inquiry.” section 6.3

Unlike MIPROv2, InferRules places generated rules in the task instructions; MIPROv2 uses grounding information to guide prompt generation instead. InferRules accepts settings for the number of candidate rule sets, rules per candidate, and threads. One reported run took about nine minutes on Google Colab, using roughly 1.9 million tokens and 1,500 language-model calls. section 6.3

"Given a set of examples, extract a list of {num_rules} concise and non-redundant natural language rules that provide clear guidance for performing the task. All rules should be actionable for a well-specified scope of examples of this general kind of task."

The rule-generation prompt asks for concise, nonredundant, actionable rules derived from examples. section 6.3

Listing 6.9 Calling the InferRules optimizer
from dspy.teleprompt import InferRules

infer_rules = InferRules(metric=validate_answer, num_candidates=5,
                         num_rules=10, num_threads=5)
prog_infer_rules = infer_rules.compile(student=intent_classifier, 
                                       trainset=train_set, valset=val_set)

A DSPy call configures InferRules with a metric and candidate settings, then compiles a student using training and validation sets. section 6.3

How Building LLM Applications with DSPy covers it

How deep it goes
Explains rule generation from input-output patterns and reports compilation settings and run costs, without detailing the optimizer’s full internals.
Examples
Uses a ground-transportation classification rule and a DSPy compilation call with training and validation sets.
What it assumes
Requires a DSPy student, metric, and training data; the demonstrated call also supplies validation data.
What you can do after
Apply it via DSPy’s InferRules API to compile a student program using training and validation sets.
How it connects
Compares its rule-bearing instructions with MIPROv2 grounding and situates both within DSPy prompt-instruction optimization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.