Skip to content

Assert transform module doesn't work? #432

@chengyjonathan

Description

@chengyjonathan
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions