import dspy import re class Ruling(dspy.Signature): """ You are a strict lore consistency judge for a sci-fi game world. You will be given NPC dialogue, any lore warnings flagged by a rule-based checker, and the character's established profile. Your job is to determine if the dialogue is consistent with the established lore and the charecter's established profile. Be strict -- if a flag suggests a potential violation, investigate it carefully. Return PASS only if you are confident the dialogue is lore-consistent. Return FAIL with a specific reason if not. """ dialogue: str = dspy.InputField(desc="The NPC dialogue to evaluate") flags: str = dspy.InputField(desc="Lore consistency warnings flagged by the hard checker") char_profile: str = dspy.InputField(desc="The NPC's established personality, faction, and speaking style") justification: str = dspy.OutputField(desc="PASS if dialogue is lore-consistent, FAIL with specific reason if not") ruling: str = dspy.OutputField(desc = "Exactly one word: PASS or FAIL, consistent with the justification from above") class JudgeAgent(dspy.Module): def __init__(self): super().__init__() self.generate = dspy.ChainOfThought(Ruling) def forward(self, dialouge_str, flags, char_profile): result = self.generate(dialogue = dialouge_str, flags = flags, char_profile = char_profile) return result def mentions(term, text): return re.search(rf"(?