dspy.BootstrapFewShotWithRandomSearch

dspy.BootstrapFewShotWithRandomSearch

dspy.BootstrapFewShotWithRandomSearch는 BootstrapFewShot를 확장한 옵티마이저로, 여러 후보 프로그램 세트를 만들어 무작위 탐색을 통해 가장 좋은 후보를 선택합니다.

출처: 문서

본문

dspy.BootstrapFewShotWithRandomSearch(
    metric,
    teacher_settings=None,
    max_bootstrapped_demos=4,
    max_labeled_demos=16,
    max_rounds=1,
    num_candidate_programs=16,
    num_threads=None,
    max_errors=None,
    stop_at_score=None,
    metric_threshold=None,
)
  • Bases: Teleprompter

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

def __init__(
    self,
    metric,
    teacher_settings=None,
    max_bootstrapped_demos=4,
    max_labeled_demos=16,
    max_rounds=1,
    num_candidate_programs=16,
    num_threads=None,
    max_errors=None,
    stop_at_score=None,
    metric_threshold=None,
):
    self.metric = metric
    self.teacher_settings = teacher_settings or {}
    self.max_rounds = max_rounds

    self.num_threads = num_threads
    self.stop_at_score = stop_at_score
    self.metric_threshold = metric_threshold
    self.min_num_samples = 1
    self.max_num_samples = max_bootstrapped_demos
    self.max_errors = max_errors
    self.num_candidate_sets = num_candidate_programs
    self.max_labeled_demos = max_labeled_demos

    print(f"Going to sample between {self.min_num_samples} and {self.max_num_samples} traces per predictor.")
    print(f"Will attempt to bootstrap {self.num_candidate_sets} candidate sets.")

Methods

compile(student, *, teacher=None, trainset, valset=None, restrict=None, labeled_sample=True)

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

compile은 여러 시드(seed)에 대해 후보 프로그램을 무작위로 생성·평가합니다. num_candidate_programs개의 후보 세트를 만들어 각각을 평가한 뒤, 가장 좋은 점수를 낸 프로그램을 반환합니다. restrict가 주어지면 지정된 시드만 평가하며, restrict가 range(-3, num_candidate_sets)와 겹치지 않으면 ValueError를 발생시킵니다. stop_at_score에 도달하면 조기 종료합니다.

def compile(self, student, *, teacher=None, trainset, valset=None, restrict=None, labeled_sample=True):
    self.trainset = trainset
    self.valset = valset or trainset  # TODO: FIXME: Note this choice.

    if restrict is not None:
        restrict = set(restrict)
        if not restrict.intersection(range(-3, self.num_candidate_sets)):
            raise ValueError(
                f"`restrict` {restrict!r} does not match any candidate seed in "
                f"range(-3, {self.num_candidate_sets}); no candidate programs would be evaluated."
            )

    effective_max_errors = self.max_errors if self.max_errors is not None else dspy.settings.max_errors

    scores = []
    all_subscores = []
    score_data = []

    for seed in range(-3, self.num_candidate_sets):
        if (restrict is not None) and (seed not in restrict):
            continue

        trainset_copy = list(self.trainset)
        ...

get_params() -> dict[str, Any]

텔레프롬프터의 파라미터를 반환합니다.

더 알아보기 (Learn more)