Created
March 2, 2024 10:25
-
-
Save fsndzomga/55e43b8a9a6b6586e3952443eb2589ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Synthetic Prompt Optimization | |
from dspy.teleprompt import BootstrapFewShot | |
from dspy.evaluate import answer_exact_match | |
text = "Barack Obama was not President of the USA" | |
# define the fact as input to the lie detector | |
trainset = [x.with_inputs('fact') for x in few_shot_examples] | |
# define the signature to be used in by the lie detector module | |
# for the evaluation, you need to define an answer field | |
class Veracity(dspy.Signature): | |
"Evaluate the veracity of a statement" | |
fact = dspy.InputField(desc="a statement") | |
answer = dspy.OutputField(desc="an assessment of the veracity of the statement") | |
class lie_detector(dspy.Module): | |
def __init__(self): | |
super().__init__() | |
self.lie_identification = dspy.ChainOfThought(Veracity) | |
def forward(self, fact): | |
return self.lie_identification(fact=fact) | |
teleprompter = BootstrapFewShot(metric=answer_exact_match) | |
compiled_lie_detector = teleprompter.compile(lie_detector(), trainset=trainset) | |
response = compiled_lie_detector(fact=text) | |
print(f"The statement '{text}' is {response.answer}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment