Skip to content

dspy.KNNFewShot

dspy.KNNFewShot(k: int, trainset: list, vectorizer=None, **few_shot_bootstrap_args)

Bases: Teleprompter

Source code in dspy/teleprompt/knn_fewshot.py
def __init__(self, k: int, trainset: list, vectorizer=None, **few_shot_bootstrap_args):
    self.KNN = KNN(k, trainset, vectorizer=vectorizer)
    self.few_shot_bootstrap_args = few_shot_bootstrap_args

Functions

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

Source code in dspy/teleprompt/knn_fewshot.py
def compile(self, student, *, teacher=None, trainset=None, valset=None):
    student_copy = student.reset_copy()

    def forward_pass(_, **kwargs):
        knn_trainset = self.KNN(**kwargs)
        few_shot_bootstrap = BootstrapFewShot(**self.few_shot_bootstrap_args)
        compiled_program = few_shot_bootstrap.compile(
            student,
            teacher=teacher,
            trainset=knn_trainset,
        )
        return compiled_program(**kwargs)

    student_copy.forward = types.MethodType(forward_pass, student_copy)
    return student_copy