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
