dspy
Included with Lifetime
$97 forever
Compile prompts into self-improving pipelines with signatures, modules, optimizers, and programmatic prompt engineering
ai-promptingdspyprompt-optimizationllmsignaturesmodulesoptimizersfew-shotchain-of-thought
What this skill does
# DSPy Skill
> Compile prompts into self-improving pipelines with programmatic prompt engineering.
## Quick Start
```bash
# Install DSPy
pip install dspy-ai
# Optional: For retrieval
pip install chromadb faiss-cpu
# Set API key
export OPENAI_API_KEY="your-api-key"
```
## When to Use This Skill
**USE when:**
- Need to optimize prompts programmatically rather than manually
- Building pipelines where prompt quality is critical to success
- Want reproducible, testable prompt engineering
- Working with complex multi-step reasoning tasks
- Need to automatically find effective few-shot examples
- Building systems that improve with more training data
- Require systematic evaluation and comparison of prompt strategies
- Want to abstract away prompt engineering from application logic
**DON'T USE when:**
- Simple single-shot prompts that work well as-is
- Need fine-grained control over exact prompt wording
- Building applications with minimal LLM interactions
- Prototyping where rapid iteration is more important than optimization
- Resource-constrained environments (optimization requires API calls)
## Prerequisites
```bash
# Core installation
pip install dspy-ai>=2.4.0
# For vector retrieval
pip install chromadb>=0.4.0 faiss-cpu>=1.7.0
# For different LLM providers
pip install openai>=1.0.0 anthropic>=0.5.0
# For evaluation
pip install pandas>=2.0.0 scikit-learn>=1.0.0
# Environment setup
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."
```
## Core Concepts
### DSPy Philosophy
DSPy treats prompts as **programs** rather than strings:
1. **Signatures** define input/output specifications
2. **Modules** implement reasoning patterns
3. **Optimizers** automatically improve prompts
4. **Metrics** evaluate performance
```
Traditional: "Write prompt" -> "Test" -> "Manually adjust" -> "Repeat"
DSPy: "Define signature" -> "Compile with optimizer" -> "Deploy optimized prompt"
```
## Core Capabilities
### 1. Signatures
**Basic Signatures:**
```python
import dspy
# Configure LLM
lm = dspy.OpenAI(model="gpt-4", max_tokens=1000)
dspy.settings.configure(lm=lm)
# Inline signature (simple)
classify = dspy.Predict("document -> category")
result = classify(document="The mooring line tension exceeded limits.")
print(result.category)
# Class-based signature (recommended)
class SentimentAnalysis(dspy.Signature):
"""Analyze the sentiment of engineering feedback."""
feedback = dspy.InputField(desc="Engineering feedback or review text")
sentiment = dspy.OutputField(desc="Sentiment: positive, negative, or neutral")
confidence = dspy.OutputField(desc="Confidence score 0-1")
# Use signature
analyzer = dspy.Predict(SentimentAnalysis)
result = analyzer(feedback="The mooring design passed all safety checks.")
print(f"Sentiment: {result.sentiment}, Confidence: {result.confidence}")
```
**Complex Signatures with Multiple Fields:**
```python
class EngineeringAnalysis(dspy.Signature):
"""Analyze an engineering report and extract key insights."""
report_text = dspy.InputField(
desc="Full text of the engineering report"
)
domain = dspy.InputField(
desc="Engineering domain (offshore, structural, mechanical)"
)
summary = dspy.OutputField(
desc="Concise 2-3 sentence summary of findings"
)
key_metrics = dspy.OutputField(
desc="List of key metrics mentioned with values"
)
risk_factors = dspy.OutputField(
desc="Identified risk factors and concerns"
)
recommendations = dspy.OutputField(
desc="Actionable recommendations from the report"
)
confidence_level = dspy.OutputField(
desc="Overall confidence in analysis: high, medium, or low"
)
# Create predictor
report_analyzer = dspy.Predict(EngineeringAnalysis)
# Analyze report
result = report_analyzer(
report_text="""
The mooring analysis for Platform Alpha shows maximum tensions
of 2,450 kN under 100-year storm conditions. Safety factors
range from 1.72 to 2.15 across all lines. Line 3 shows the
lowest margin at the fairlead connection. Fatigue life estimates
indicate 35-year service life, exceeding the 25-year requirement.
Chain wear measurements show 8% diameter loss after 5 years.
""",
domain="offshore"
)
print(f"Summary: {result.summary}")
print(f"Key Metrics: {result.key_metrics}")
print(f"Risk Factors: {result.risk_factors}")
print(f"Recommendations: {result.recommendations}")
```
### 2. Modules
**ChainOfThought for Complex Reasoning:**
```python
class TechnicalQA(dspy.Signature):
"""Answer technical engineering questions with reasoning."""
context = dspy.InputField(desc="Technical context and background")
question = dspy.InputField(desc="Technical question to answer")
answer = dspy.OutputField(desc="Detailed technical answer")
# ChainOfThought adds reasoning before answering
class TechnicalExpert(dspy.Module):
def __init__(self):
super().__init__()
self.answer_question = dspy.ChainOfThought(TechnicalQA)
def forward(self, context, question):
result = self.answer_question(context=context, question=question)
return result
# Usage
expert = TechnicalExpert()
result = expert(
context="""
Catenary mooring systems use the weight of the chain to provide
restoring force. The touchdown point moves as the vessel offsets.
Line tension is a function of the catenary geometry and pretension.
""",
question="How does water depth affect mooring line tension?"
)
print(f"Reasoning: {result.rationale}")
print(f"Answer: {result.answer}")
```
**Multi-Stage Pipeline Module:**
```python
class DocumentSummary(dspy.Signature):
"""Summarize a technical document."""
document = dspy.InputField()
summary = dspy.OutputField()
class KeyPointExtraction(dspy.Signature):
"""Extract key points from a summary."""
summary = dspy.InputField()
key_points = dspy.OutputField(desc="List of 3-5 key points")
class ActionItemGeneration(dspy.Signature):
"""Generate action items from key points."""
key_points = dspy.InputField()
action_items = dspy.OutputField(desc="List of actionable next steps")
class DocumentProcessor(dspy.Module):
"""Multi-stage document processing pipeline."""
def __init__(self):
super().__init__()
self.summarize = dspy.ChainOfThought(DocumentSummary)
self.extract_points = dspy.Predict(KeyPointExtraction)
self.generate_actions = dspy.Predict(ActionItemGeneration)
def forward(self, document):
# Stage 1: Summarize
summary_result = self.summarize(document=document)
# Stage 2: Extract key points
points_result = self.extract_points(summary=summary_result.summary)
# Stage 3: Generate actions
actions_result = self.generate_actions(key_points=points_result.key_points)
return dspy.Prediction(
summary=summary_result.summary,
key_points=points_result.key_points,
action_items=actions_result.action_items
)
# Usage
processor = DocumentProcessor()
result = processor(document="[Long engineering document text...]")
print(f"Summary: {result.summary}")
print(f"Key Points: {result.key_points}")
print(f"Actions: {result.action_items}")
```
**ReAct Module for Tool Use:**
```python
class CalculateTension(dspy.Signature):
"""Calculate mooring line tension."""
depth = dspy.InputField(desc="Water depth in meters")
line_length = dspy.InputField(desc="Line length in meters")
pretension = dspy.InputField(desc="Pretension in kN")
result = dspy.OutputField(desc="Tension calculation result")
class SearchStandards(dspy.Signature):
"""Search engineering standards database."""
query = dspy.InputField(desc="Search query")
standards = dspy.OutputField(desc="Relevant standards found")
class EngineeringReActAgent(dspy.Module):
"""Agent that can reason and act using tools."""
dRelated in ai-prompting
pandasai
IncludedConversational data analysis using natural language queries on DataFrames. Chat with your data using LLMs to generate insights, create visualizations, and explain code.
ai-prompting
agenta
IncludedLLM prompt management and evaluation platform. Version prompts, run A/B tests, evaluate with metrics, and deploy with confidence using Agenta's self-hosted solution.
ai-prompting
langchain
IncludedBuild production-ready LLM applications with chains, agents, memory, tools, and RAG pipelines using the LangChain framework
ai-prompting
prompt-engineering
IncludedComprehensive prompting techniques including chain-of-thought, few-shot, zero-shot, system prompts, persona design, and evaluation patterns
ai-prompting