dspy.Refine
dspy.Refine(module: Module, N: int, reward_fn: Callable[[dict, Prediction], float], threshold: float, fail_count: Optional[int] = None)
Bases: Module
Refines a module by running it up to N times with different temperatures and returns the best prediction.
This module runs the provided module multiple times with varying temperature settings and selects either the first prediction that exceeds the specified threshold or the one with the highest reward. If no prediction meets the threshold, it automatically generates feedback to improve future predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
Module
|
The module to refine. |
required |
N
|
int
|
The number of times to run the module. must |
required |
reward_fn
|
Callable
|
The reward function. |
required |
threshold
|
float
|
The threshold for the reward function. |
required |
fail_count
|
Optional[int]
|
The number of times the module can fail before raising an error |
None
|
Example
import dspy
dspy.settings.configure(lm=dspy.LM("openai/gpt-4o-mini"))
# Define a QA module with chain of thought
qa = dspy.ChainOfThought("question -> answer")
# Define a reward function that checks for one-word answers
def one_word_answer(args, pred):
return 1.0 if len(pred.answer.split()) == 1 else 0.0
# Create a refined module that tries up to 3 times
best_of_3 = dspy.Refine(module=qa, N=3, reward_fn=one_word_answer, threshold=1.0)
# Use the refined module
result = best_of_3(question="What is the capital of Belgium?").answer
# Returns: Brussels