跳到内容

dspy.BootstrapFewShot

dspy.BootstrapFewShot(metric=None, metric_threshold=None, teacher_settings: Optional[Dict] = None, max_bootstrapped_demos=4, max_labeled_demos=16, max_rounds=1, max_errors=5)

基类: Teleprompter

这是一个 Teleprompter 类,用于组合一组演示/示例,放入预测器的提示中。这些演示来自训练集中的标注示例和自举(bootstrapped)演示的组合。

参数

名称 类型 描述 默认值
metric Callable

一个用于比较预期值和预测值,并输出比较结果的函数。

None
metric_threshold float

如果评估指标产生数值,则在决定是否接受自举示例时,将该值与此阈值进行比较。默认为 None。

None
teacher_settings dict

teacher 模型的设置。默认为 None。

None
max_bootstrapped_demos int

要包含的自举演示的最大数量。默认为 4。

4
max_labeled_demos int

要包含的标注演示的最大数量。默认为 16。

16
max_rounds int

尝试生成所需自举示例的迭代次数。如果在达到 max_rounds 次后仍未成功,程序将结束。默认为 1。

1
max_errors int

程序结束前的最大错误数。默认为 5。

5
源码位于 dspy/teleprompt/bootstrap.py
def __init__(
    self,
    metric=None,
    metric_threshold=None,
    teacher_settings: Optional[Dict] = None,
    max_bootstrapped_demos=4,
    max_labeled_demos=16,
    max_rounds=1,
    max_errors=5,
):
    """A Teleprompter class that composes a set of demos/examples to go into a predictor's prompt.
    These demos come from a combination of labeled examples in the training set, and bootstrapped demos.

    Args:
        metric (Callable): A function that compares an expected value and predicted value,
            outputting the result of that comparison.
        metric_threshold (float, optional): If the metric yields a numerical value, then check it
            against this threshold when deciding whether or not to accept a bootstrap example.
            Defaults to None.
        teacher_settings (dict, optional): Settings for the `teacher` model.
            Defaults to None.
        max_bootstrapped_demos (int): Maximum number of bootstrapped demonstrations to include.
            Defaults to 4.
        max_labeled_demos (int): Maximum number of labeled demonstrations to include.
            Defaults to 16.
        max_rounds (int): Number of iterations to attempt generating the required bootstrap
            examples. If unsuccessful after `max_rounds`, the program ends. Defaults to 1.
        max_errors (int): Maximum number of errors until program ends. Defaults to 5.
    """
    self.metric = metric
    self.metric_threshold = metric_threshold
    self.teacher_settings = {} if teacher_settings is None else teacher_settings

    self.max_bootstrapped_demos = max_bootstrapped_demos
    self.max_labeled_demos = max_labeled_demos
    self.max_rounds = max_rounds
    self.max_errors = max_errors
    self.error_count = 0
    self.error_lock = threading.Lock()

函数

compile(student, *, teacher=None, trainset)

源码位于 dspy/teleprompt/bootstrap.py
def compile(self, student, *, teacher=None, trainset):
    self.trainset = trainset

    self._prepare_student_and_teacher(student, teacher)
    self._prepare_predictor_mappings()
    self._bootstrap()

    self.student = self._train()
    self.student._compiled = True

    # set assert_failures and suggest_failures as attributes of student w/ value 0
    self.student._assert_failures = 0
    self.student._suggest_failures = 0

    return self.student

get_params() -> dict[str, Any]

获取 teleprompter 的参数。

返回值

类型 描述
dict[str, Any]

teleprompter 的参数。

源码位于 dspy/teleprompt/teleprompt.py
def get_params(self) -> dict[str, Any]:
    """
    Get the parameters of the teleprompter.

    Returns:
        The parameters of the teleprompter.
    """
    return self.__dict__