Australia/Sydney
BlogMay 16, 2024

DSPy Explained in Simple Words with Easy Example

Fahd Mirza

 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.")
Share this post:
On this page

Let's Partner

If you are looking to build, deploy or scale AI solutions — whether you're just starting or facing production-scale challenges — let's chat.

Subscribe to Fahd's Newsletter

Weekly updates on AI, cloud engineering, and tech innovations