π Welcome to DSPy
Your journey from prompt engineering to prompt programming starts here
What You'll Learn
By the end of this chapter, you will:
Chapter Overview
This chapter covers the essential foundations you need to start building with DSPy:
What is DSPy?
Learn what DSPy is, why it was created, and how it differs from traditional prompt engineering approaches.
Programming vs. Prompting
Understand the fundamental paradigm shift from manual prompt engineering to programmatic LM pipelines.
Your First DSPy Program
Write and run a complete DSPy application from scratch.
Language Models
Learn how to configure and work with different LM providers (OpenAI, Anthropic, local models).
Exercises
Practice what you've learned with hands-on exercises.
What Makes DSPy Different?
Before diving in, here's a quick preview of what makes DSPy special:
β Traditional Prompting
# Manual prompt engineering
prompt = """
You are a helpful assistant. Answer the question clearly.
Question: What is the capital of France?
Answer:
"""
response = openai.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
β DSPy Approach
import dspy
# Define the task signature
class QuestionAnswer(dspy.Signature):
"""Answer questions clearly."""
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Use it with automatic prompting
qa = dspy.Predict(QuestionAnswer)
response = qa(question="What is the capital of France?")
Prerequisites
Before starting this chapter, ensure you have:
Need help with prerequisites? Review Chapter 0: Prerequisites
Learning Approach
This chapter uses a hands-on approach:
Concepts
Clear explanations of core ideas
Examples
Working code you can run
Practice
Exercises to reinforce learning
Experimentation
Encouragement to modify and explore
Pro Tip: Don't just readβrun every example and complete the exercises!
Estimated Time
Feel free to spread this over multiple sessions!
Key Takeaways (Preview)
By the end of this chapter, you'll understand:
Let's Begin!
Ready to learn DSPy? Start with understanding what DSPy is and why it matters.
Remember: Learning is a journey. Take your time, experiment freely, and don't hesitate to ask questions!