Skip to content

[Bug] Not getting expected labelled few shot examples using DSPy LabelledFewShot #7993

Open
@yash-raj-verma

Description

@yash-raj-verma

What happened?

We have a dataset with 1000 examples, each example consists of three numbers, two numbers and their sum as the third number. We need to find the best (top) 5 examples from the dataset without bootstrapping (since we have enough labelled examples).

  • Why is our forward() method not getting called?
  • compiled_rag does not contain any demo examples after the compile() call. Why is it so?

We are attaching a snippet of the code we have so far.

# LearnNumbers.py
import dspy
from dspy.teleprompt import LabeledFewShot

class Prediction(dspy.Signature):
    """Given two numbers give the addition of the two numbers"""
    numbers = dspy.InputField(desc='Two numbers to be added')
    sum = dspy.OutputField(desc='Addition of numbers')


#Assume defined trainset
class LearnSumNumbers(dspy.Module):
    def __init__(self):
        super().__init__()
        self.generate_answer = dspy.Predict(Prediction)

    #flow for answering questions using predictor and retrieval modules
    def forward(self, question):
        prediction = self.generate_answer(question=question)
        return dspy.Prediction(answer=prediction.answer)

#Define teleprompter
teleprompter = LabeledFewShot(k=5)

trainset = [
            dspy.Example(numbers='2 3', sum ='5').with_inputs("numbers"), 
            dspy.Example(numbers='6 6', sum ='12').with_inputs("numbers"), 
            dspy.Example(numbers='5 5', sum ='10').with_inputs("numbers"),
            dspy.Example(numbers='55 7', sum ='62').with_inputs("numbers")
....
# 1000 such examples
] 

compiled_rag = teleprompter.compile(student=LearnSumNumbers(), trainset=trainset)

Please guide us in this matter.

Steps to reproduce

We ran the code on the terminal using python. We used python3 (3.11 version) on Ubuntu (Linux) machine.

$ python3 LearnNumbers.py

DSPy version

2.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions