Thursday, May 16, 2024

DSPy Explained in Simple Words with Easy Example

 In this video, I am going to explain DSPy in simple words with a simple example. If, by end of the video, you are still unable to understand what DSPy is I will return your money. Guaranteed. 




Code Used:


!pip install dspy-ai
!pip install openai~=0.28.1

import dspy
class ShakespeareanTranslation(dspy.Signature):
    """Translate simple english to Shakespearean english."""
    simple_english = dspy.InputField()
    shakespearean_english = dspy.OutputField()
   
from google.colab import userdata
OPENAI_API_KEY=userdata.get('OPENAI_API_KEY')
turbo = dspy.OpenAI(model='gpt-3.5-turbo', max_tokens=1000, api_key=OPENAI_API_KEY)
dspy.settings.configure(lm=turbo)
from dspy.signatures.signature import signature_to_template
shakespeare_translation_as_template = signature_to_template(ShakespeareanTranslation)

class CoT(dspy.Module):
    def __init__(self):
        super().__init__()
        self.prog = dspy.ChainOfThought(ShakespeareanTranslation)
   
    def forward(self, simple_english):
        return self.prog(simple_english=simple_english)
c = CoT()

c.forward("You should relax and have fun while it lasts.")

No comments: