import re from dspy.primitives.assertions import assert_transform_module, backtrack_handler
defcheck_format(action_items): """Check that the action items are a list of comma-separated action items""" iflen(action_items.split(",")) == 1: returnFalse match = re.search(r'(\d)\.\s.+?(\\n|$)', action_items, re.MULTILINE) ifmatch: returnFalse returnTrue
classGenerateExampleWithAssert(dspy.Module): def__init__(self): super().__init__() self.generate_example = dspy.ChainOfThought(TranscriptExample) defforward(self): ex = self.generate_example(**varying_temp()) dspy.Assert(check_format(ex.action_items), "Action Items should be a comma-separated list") return ex
classActionItemCompliance(dspy.Signature): """Check that all action items are included in the text""" text = dspy.InputField() action_items = dspy.InputField(desc="A comma-separated list of action items") comply : bool = dspy.OutputField(desc="True or False")
classGenerateExampleWith2Assert(dspy.Module): def__init__(self): super().__init__() self.generate_example = dspy.ChainOfThought(TranscriptExample) defforward(self): ex = self.generate_example(**varying_temp()) dspy.Assert(check_format(ex.action_items), "Action Items should be a comma-separated list") dspy.Assert(are_action_items_included(ex.transcript, ex.action_items), "Action Items should be included in the transcript") return ex generate_with_assert = assert_transform_module(GenerateExampleWith2Assert(), backtrack_handler)
example = generate_with_assert() print(example) # output: True
classGenerateExampleWith2Suggest(dspy.Module): def__init__(self): super().__init__() self.generate_example = dspy.ChainOfThought(TranscriptExample) defforward(self): ex = self.generate_example(**varying_temp()) dspy.Suggest(check_format(ex.action_items), "Action Items should be a comma-separated list") dspy.Suggest(are_action_items_included(ex.transcript, ex.action_items), "Action Items should be included in the transcript") return ex
examples = [] for i inrange(1, 20): with dspy.context(cache_turn_on=False): ex = generate_with_suggest() print(ex) examples.append({"transcription": ex.transcript, "action_items": ex.action_items})
withopen('examples.json', 'w') as f: json.dump(examples, f)