dspy.BootstrapFewShot

dspy.BootstrapFewShot

dspy.BootstrapFewShot는 predictor의 프롬프트에 들어갈 데모/예시 세트를 구성하는 Teleprompter 클래스입니다. 이 데모들은 학습 세트의 라벨이 붙은 예시와 부트스트랩된 데모의 조합에서 만들어집니다.

출처: 문서

본문

dspy.BootstrapFewShot(
    metric=None,
    metric_threshold=None,
    teacher_settings: dict | None = None,
    max_bootstrapped_demos=4,
    max_labeled_demos=16,
    max_rounds=1,
    max_errors=None,
)
  • Bases: Teleprompter

predictor의 프롬프트에 들어갈 데모/예시 세트를 구성하는 Teleprompter 클래스입니다. 이 데모들은 학습 세트의 라벨이 붙은 예시와 부트스트랩된 데모의 조합에서 만들어집니다.

각 부트스트랩 라운드는 LM을 temperature=1.0에서 새 rollout_id로 복사해 캐시를 우회하고 다양한 trace를 수집합니다.

Parameters:

Name Type Description Default
metric Callable 기대값과 예측값을 비교해 그 결과를 출력하는 함수. None
metric_threshold float metric이 수치를 반환하면, 부트스트랩 예시를 받아들일지 결정할 때 이 threshold와 비교합니다. 기본값은 None. None
teacher_settings dict teacher 모델의 설정. 기본값은 None. None
max_bootstrapped_demos int 포함할 부트스트랩 데모의 최대 수. 기본값은 4. 4
max_labeled_demos int 포함할 라벨이 붙은 데모의 최대 수. 기본값은 16. 16
max_rounds int 학습 예시당 부트스트랩 시도 횟수. 첫 라운드 이후의 각 라운드는 temperature=1.0의 새 rollout을 사용해 캐시를 우회하고 다양한 trace를 수집합니다. 어느 라운드에서든 성공적인 부트스트랩을 찾으면 예시는 받아들여지고 옵티마이저가 다음 예시로 이동합니다. 기본값은 1. 1
max_errors Optional[int] 프로그램이 끝날 때까지의 최대 에러 수. None이면 dspy.settings.max_errors를 따릅니다. None

소스 코드는 dspy/teleprompt/bootstrap.py에 있습니다.

def __init__(
    self,
    metric=None,
    metric_threshold=None,
    teacher_settings: dict | None = None,
    max_bootstrapped_demos=4,
    max_labeled_demos=16,
    max_rounds=1,
    max_errors=None,
):
    """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.

    Each bootstrap round copies the LM with a new ``rollout_id`` at ``temperature=1.0`` to
    bypass caches and gather diverse traces.

    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): Maximum number of bootstrap attempts per training example.
            Each round after the first uses a fresh rollout with ``temperature=1.0``
            to bypass caches and gather diverse traces. If a successful bootstrap is
            found on any round, the example is accepted and the optimizer moves to the
            next one. Defaults to 1.
        max_errors (Optional[int]): Maximum number of errors until program ends.
            If ``None``, inherits from ``dspy.settings.max_errors``.
    """
    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()

Methods

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

student 프로그램을 학습 세트로 컴파일합니다. teacher가 없으면 student 자기 자신을 teacher로 사용해 데모를 부트스트랩합니다. metric을 통과한 예시만 데모로 채택됩니다.

get_params() -> dict[str, Any]

텔레프롬프터의 파라미터를 반환합니다. (자세한 구현은 Ensemble의 get_params 참고)

더 알아보기 (Learn more)