dspy.MultiChainComparison
Constructor
The constructor initializes the MultiChainComparison
class and sets up its attributes. It inherits from the Predict
class and adds specific functionality for multiple chain comparisons.
The class incorporates multiple student attempt reasonings and concludes with the selected best reasoning path out of the available attempts.
from .predict import Predict
from ..primitives.program import Module
import dsp
class MultiChainComparison(Module):
def __init__(self, signature, M=3, temperature=0.7, **config):
super().__init__()
self.M = M
signature = Predict(signature).signature
*keys, last_key = signature.kwargs.keys()
extended_kwargs = {key: signature.kwargs[key] for key in keys}
for idx in range(M):
candidate_type = dsp.Type(prefix=f"Student Attempt #{idx+1}:", desc="${reasoning attempt}")
extended_kwargs.update({f'reasoning_attempt_{idx+1}': candidate_type})
rationale_type = dsp.Type(prefix="Accurate Reasoning: Thank you everyone. Let's now holistically", desc="${corrected reasoning}")
extended_kwargs.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})
signature = dsp.Template(signature.instructions, **extended_kwargs)
self.predict = Predict(signature, temperature=temperature, **config)
self.last_key = last_key
Parameters:
signature
(Any): Signature of predictive model.M
(int, optional): Number of student reasoning attempts. Defaults to3
.temperature
(float, optional): Temperature parameter for prediction. Defaults to0.7
.**config
(dict): Additional configuration parameters for model.
Method
forward(self, completions, **kwargs)
This method aggregates all the student reasoning attempts and calls the predict method with extended signatures to get the best reasoning.
Parameters:
completions
: List of completion objects which include student reasoning attempts.**kwargs
: Additional keyword arguments.
Returns:
- The result of the
predict
method for the best reasoning.