๐ Setup Instructions
Get your DSPy environment ready in 15-30 minutes
Your Setup Roadmap
Python Check
Verify Python 3.9+
1-2 minProject Setup
Create directory
1 minVirtual Env
Isolate dependencies
2-3 minInstallation
Install DSPy
2-5 minAPI Config
Set up keys
3-5 minTesting
Verify setup
2-3 minReady!
Start learning
Step 1: Verify Python Installation
python3 --version
Expected outputs (any of these is good):
Python 3.9.0 โ
Minimum
required
Python 3.10.8 โ
Good
choice
Python 3.11.5 โ
Latest
stable
Python 3.12.0 โ
Cutting
edge
Pro Tip: If python3 doesn't work, try
python. Different systems
use different commands.
Step 2: Create Your Project Directory
# Create the directory
mkdir ~/dspy-learning
# Navigate into it
cd ~/dspy-learning
# Verify location
pwd
Success: You should see your DSPy project directory path!
Step 3: Set Up Virtual Environment
Why Virtual Environments?
python3 -m venv venv
Activate the Environment
Verify activation: Your prompt should now show
(venv) at the beginning!
Important: Always activate your virtual environment before working on DSPy projects!
Step 4: Install DSPy and Dependencies
pip install --upgrade pip
pip install dspy-ai
What you get:
- โ DSPy framework
- โ Core dependencies
- โ Language model adapters
python3 -c "import dspy; print(f'DSPy version: {dspy.__version__}')"
DSPy version: 2.5.x
pip install openai anthropic python-dotenv
Step 5: Configure API Access
Getting Your OpenAI API Key
- Visit: platform.openai.com
- Sign up or log in
- Navigate to API Keys section
- Create new secret key
- Copy your key (starts with
sk-)
OPENAI_API_KEY=sk-your-actual-api-key-here
touch .env
Security Warning: Never commit API keys to Git or share them publicly!
Step 6: Test Your Setup
The Moment of Truth!
"""
โจ DSPy Setup Verification Script โจ
Tests your installation and API connectivity
"""
import os
from dotenv import load_dotenv
import dspy
# Load environment variables
load_dotenv()
def test_setup():
print("๐ DSPy Setup Verification")
print("=" * 50)
# Check DSPy
print(f"โ
DSPy version: {dspy.__version__}")
# Check API key
api_key = os.getenv("OPENAI_API_KEY")
if api_key:
print("โ
OpenAI API key found")
# Configure and test
lm = dspy.LM(
model="openai/gpt-4o-mini",
api_key=api_key
)
dspy.configure(lm=lm)
# Simple test
class SimpleQA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
predictor = dspy.Predict(SimpleQA)
result = predictor(question="What is 2 + 2?")
print(f"โ
API Test: 2 + 2 = {result.answer}")
print("=" * 50)
print("๐ SUCCESS! Your DSPy environment is ready!")
else:
print("โ ๏ธ No API key found. Add to .env file.")
if __name__ == "__main__":
test_setup()
python3 test_setup.py
Expected Successful Output:
๐ DSPy Setup Verification ================================================== โ DSPy version: 2.5.x โ OpenAI API key found โ API Test: 2 + 2 = 4 ================================================== ๐ SUCCESS! Your DSPy environment is ready!
Troubleshooting Common Issues
source venv/bin/activate
Step 7: Get Book Examples (Optional)
# Go to your learning directory
cd ~/dspy-learning
# Clone the book repository
git clone https://github.com/dustinober1/Ebook_DSPy.git
# Enter the directory
cd Ebook_DSPy
# Install all dependencies
pip install -r requirements.txt
Repository Structure
Ebook_DSPy/ โโโ examples/ ๐ All code examples by chapter โ โโโ chapter01/ โ โโโ chapter02/ โ โโโ ... โโโ exercises/ โ๏ธ Exercise starter code & solutions โโโ assets/ ๐ Datasets and resources โโโ scripts/ ๐ ๏ธ Utility scripts
๐ Quick Reference Card
source venv/bin/activate
deactivate
pip install package-name
python3 script.py
python3 -c "import dspy; print(dspy.__version__)"
Congratulations!
Your DSPy environment is ready. You can now start Chapter 1!