dspy icon indicating copy to clipboard operation
dspy copied to clipboard

Assert transform module doesn't work?

Open chengyjonathan opened this issue 6 months ago • 1 comments

class LongFormQAWithAssertions(dspy.Module):
    def __init__(self, passages_per_hop=3, max_hops=2):
        super().__init__()
        self.generate_query = [dspy.ChainOfThought(QASignatures.GenerateSearchQuery) for _ in range(max_hops)]
        self.retrieve = dspy.Retrieve(k=passages_per_hop)
        self.generate_cited_paragraph = dspy.ChainOfThought(QASignatures.GenerateCitedParagraph)
        self.max_hops = max_hops
    
    def forward(self, question):
        context = []
        for hop in range(self.max_hops):
            query = self.generate_query[hop](context=context, question=question).query
            passages = self.retrieve(query).passages
            context = deduplicate(context + passages)
        pred = self.generate_cited_paragraph(context=context, question=question)
        pred = dspy.Prediction(context=context, paragraph=pred.paragraph)
        dspy.Suggest(citations.citations_check(pred.paragraph), f"Make sure every 1-2 sentences has citations. If any 1-2 sentences lack citations, add them in 'text... [x].' format.", target_module=QASignatures.GenerateCitedParagraph)
        _, unfaithful_outputs = citations.citation_faithfulness(None, pred, None)
        if unfaithful_outputs:
            unfaithful_pairs = [(output['text'], output['context']) for output in unfaithful_outputs]
            for _, context in unfaithful_pairs:
                dspy.Suggest(len(unfaithful_pairs) == 0, f"Make sure your output is based on the following context: '{context}'.", target_module=QASignatures.GenerateCitedParagraph)
        else:
            return pred
        return pred

uncompiled_cited_rag = assertions.assert_transform_module(
        rags.LongFormQAWithAssertions().map_named_predictors(Retry), 
        assertions.backtrack_handler
        )
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/joncheng/Desktop/repos/dspy_dnd/scripts/chroma_2_dspyCitedRAG.py", line 61, in <module>
    main()
  File "/Users/joncheng/Desktop/repos/dspy_dnd/scripts/chroma_2_dspyCitedRAG.py", line 34, in main
    pred = uncompiled_cited_rag(question=question)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/joncheng/opt/miniconda3/envs/dspy_test/lib/python3.12/site-packages/dspy/primitives/program.py", line 29, in __call__
    return self.forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/joncheng/opt/miniconda3/envs/dspy_test/lib/python3.12/site-packages/dspy/primitives/assertions.py", line 310, in forward
    return wrapped_forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/joncheng/opt/miniconda3/envs/dspy_test/lib/python3.12/site-packages/dspy/primitives/assertions.py", line 232, in wrapper
    dsp.settings.trace.clear()
    ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'clear'

I was trying to run the qa with assertions example, and I got this traceback.

chengyjonathan avatar Feb 21 '24 18:02 chengyjonathan

Did you call dspy.configure(lm=lm, trace=[])?

thomasahle avatar Feb 21 '24 18:02 thomasahle

Yes for assertions, we initialize the trace as empty to setup for the Retry modules that follow. Please set trace=[] when using assertions within your programs!

arnavsinghvi11 avatar Feb 22 '24 21:02 arnavsinghvi11

Ah interesting why don’t we set that globally @arnavsinghvi11 , should we add it

okhat avatar Feb 22 '24 21:02 okhat

Ohhh, i see now. Thank you

chengyjonathan avatar Feb 22 '24 22:02 chengyjonathan

Maybe have trace=() be the default instead of trace=None?

thomasahle avatar Feb 26 '24 18:02 thomasahle