Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created April 9, 2024 19:03
Show Gist options
  • Save fsndzomga/c84ff6c5d9cbf9430232472b348230a6 to your computer and use it in GitHub Desktop.
Save fsndzomga/c84ff6c5d9cbf9430232472b348230a6 to your computer and use it in GitHub Desktop.
import dspy
import os
from dotenv import load_dotenv
load_dotenv()
llm = dspy.OpenAI(
model='gpt-4',
api_key=os.environ['OPENAI_API_KEY'],
max_tokens=100
)
dspy.settings.configure(lm=llm)
class check_logic(dspy.Signature):
"""Given the premises and the conclusions of an argument, check if the conclusions are logical."""
premises = dspy.InputField(desc="Premises of the argument")
conclusions = dspy.InputField(desc="Conclusion of the argument")
sound = dspy.InputField(desc="The premises are sound or not")
logical = dspy.OutputField(desc="Given the premises, the conclusion is logical or not")
class check_premises(dspy.Signature):
"""Given the premises of an argument, check if the premises are sound."""
premises = dspy.InputField(desc="Premises of the argument")
sound = dspy.OutputField(desc="The premises are sound or not")
class logical_reasoner(dspy.Module):
def __init__(self):
super().__init__()
self.logical_reasoner = dspy.Predict("text -> premises, conclusions")
self.check_prems = dspy.Predict(check_premises)
self.checker = dspy.ChainOfThought(check_logic)
def forward(self,text):
prediction = self.logical_reasoner(text=text)
sound = self.check_prems(premises=prediction.premises)
result = self.checker(premises=prediction.premises, conclusions=prediction.conclusions, sound=sound.sound)
return result
text="If it is raining, then the grass is wet. The grass is wet. Therefore, it is raining."
lr = logical_reasoner()
print(lr(text=text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment