# AUTOGENERATED! DO NOT EDIT! File to edit: ../../../nbs/dspy/dspy.module.sc.ipynb.

# %% auto 0
__all__ = ['log', 'SelfConsistency']

# %% ../../../nbs/dspy/dspy.module.sc.ipynb 3
from copy import deepcopy

import dspy
from dspy.primitives.program import Module

from ...logging import get_logger

log = get_logger(__name__)

# %% ../../../nbs/dspy/dspy.module.sc.ipynb 4
class SelfConsistency(Module):
    def __init__(self, predictors: list, field: str):
        super().__init__()
        assert len(predictors) > 1, "Self-consistency prompting requires at least 2 predictors"
        self.predictors = predictors
        self.field = field
    
    @classmethod
    def from_clones(cls, predictor, field: str, n: int):
        return cls(predictors=[deepcopy(predictor) for _ in range(n)], field=field)    

    def forward(self, **kwargs):
        predictions = [predictor(**kwargs) for predictor in self.predictors]
        final_prediction = dspy.majority(predictions, field=self.field)
        final_prediction.candidate_predictions = predictions
        return final_prediction
