A reward function scores a generated response so a DSPy module can decide whether to keep it or try again. BestOfN compares responses from repeated calls to one submodule; Refine uses scores to guide successive attempts. Higher scores indicate better responses, and a threshold can end the search early. section 7.3.2 section 7.3.4
The callable receives the inputs and a prediction, then returns a numeric score. It runs at inference time, so it cannot rely on a known correct output as a metric sometimes can. Choose a rule that can judge the response using information available when the program runs. section 7.3.2
Reward a one-word answer
For a question-answering program, `def one_word_answer(args, pred): return 1.0 if len(pred.answer.split()) == 1 else 0.0` scores the desired format without needing a ground-truth answer. Pass it as `reward_fn` to `dspy.Refine` with `N=3` and `threshold=1.0`. Refine can stop as soon as an answer earns 1.0; otherwise, it uses feedback to improve its next attempt. section 7.3.4
Figure 7.1 The workflow within the Refine module starts with the original prompt, which is used in a call to the LM. The response is collected, and Refine calls the reward method to evaluate it. At each iteration, the LM is also called to get feedback about the prompt and the response, including advice on how to improve the prompt. When asking for this advice, Refine also provides the LM with task and reward function information, a history of the previous prompts, and the reward function’s scores for their outputs. This advice is used as the hint field in the next call to the LM. Once the response is good enough (the reward function gives it a score above a specified threshold) or the process loops a specified number of times, the best response (as evaluated by the reward function) is returned.
The workflow shows where scoring fits: Refine evaluates each response, uses unsuccessful attempts to seek feedback, and returns the best response when it reaches the threshold or iteration limit. section 7.3.4
