dspy-4-optimizers
Sub-skill of dspy: 4. Optimizers.
Best use case
dspy-4-optimizers is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Sub-skill of dspy: 4. Optimizers.
Teams using dspy-4-optimizers should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/4-optimizers/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How dspy-4-optimizers Compares
| Feature / Agent | dspy-4-optimizers | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Sub-skill of dspy: 4. Optimizers.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# 4. Optimizers
## 4. Optimizers
**BootstrapFewShot Optimizer:**
```python
from dspy.teleprompt import BootstrapFewShot
class ClassifyReport(dspy.Signature):
"""Classify engineering report type."""
report_text = dspy.InputField()
report_type = dspy.OutputField(
desc="Type: analysis, inspection, design, or incident"
)
class ReportClassifier(dspy.Module):
def __init__(self):
super().__init__()
self.classify = dspy.Predict(ClassifyReport)
def forward(self, report_text):
return self.classify(report_text=report_text)
# Create training data
trainset = [
dspy.Example(
report_text="The mooring analysis shows maximum tensions...",
report_type="analysis"
).with_inputs("report_text"),
dspy.Example(
report_text="Visual inspection of Line 3 revealed corrosion...",
report_type="inspection"
).with_inputs("report_text"),
dspy.Example(
report_text="The new platform design incorporates...",
report_type="design"
).with_inputs("report_text"),
dspy.Example(
report_text="At 14:32, the vessel experienced sudden offset...",
report_type="incident"
).with_inputs("report_text"),
# Add more examples...
]
# Define metric
def classification_accuracy(example, prediction, trace=None):
return example.report_type.lower() == prediction.report_type.lower()
# Optimize
optimizer = BootstrapFewShot(
metric=classification_accuracy,
max_bootstrapped_demos=4,
max_labeled_demos=8
)
# Compile optimized module
optimized_classifier = optimizer.compile(
ReportClassifier(),
trainset=trainset
)
# Use optimized classifier
result = optimized_classifier(
report_text="Fatigue analysis indicates remaining life of 15 years..."
)
print(f"Type: {result.report_type}")
```
**BootstrapFewShotWithRandomSearch:**
```python
from dspy.teleprompt import BootstrapFewShotWithRandomSearch
# More thorough optimization with search
optimizer = BootstrapFewShotWithRandomSearch(
metric=classification_accuracy,
max_bootstrapped_demos=4,
max_labeled_demos=8,
num_candidate_programs=10,
num_threads=4
)
# This searches for the best combination of examples
optimized = optimizer.compile(
ReportClassifier(),
trainset=trainset,
valset=valset # Optional validation set
)
```
**MIPRO Optimizer (Advanced):**
```python
from dspy.teleprompt import MIPRO
class ComplexQA(dspy.Module):
def __init__(self):
super().__init__()
self.qa = dspy.ChainOfThought("context, question -> answer")
def forward(self, context, question):
return self.qa(context=context, question=question)
# MIPRO optimizes both instructions and examples
optimizer = MIPRO(
metric=answer_quality_metric,
prompt_model=dspy.OpenAI(model="gpt-4"),
task_model=dspy.OpenAI(model="gpt-4.1-mini"),
num_candidates=10,
init_temperature=1.0
)
optimized_qa = optimizer.compile(
ComplexQA(),
trainset=trainset,
num_batches=5,
max_bootstrapped_demos=3,
max_labeled_demos=5,
eval_kwargs={"num_threads": 4}
)
```Related Skills
dspy
Build complex AI systems with declarative programming, optimize prompts automatically, create modular RAG systems and agents with DSPy - Stanford NLP's framework for systematic LM programming
dspy-optimization-not-improving
Sub-skill of dspy: Optimization Not Improving (+2).
dspy-integration-with-langchain
Sub-skill of dspy: Integration with LangChain (+1).
dspy-example-1-engineering-report-analysis-pipeline
Sub-skill of dspy: Example 1: Engineering Report Analysis Pipeline (+2).
dspy-dspy-philosophy
Sub-skill of dspy: DSPy Philosophy.
dspy-3-retrieval-augmented-generation
Sub-skill of dspy: 3. Retrieval-Augmented Generation.
dspy-2-modules
Sub-skill of dspy: 2. Modules.
dspy-1-start-simple-then-optimize
Sub-skill of dspy: 1. Start Simple, Then Optimize (+2).
dspy-1-signatures
Sub-skill of dspy: 1. Signatures.
test-oversized-skill
A test fixture skill that exceeds 200 lines with multiple H2/H3 sections for split testing.
interactive-report-generator
Generate interactive HTML reports with Plotly visualizations from data analysis results. Supports dashboards, charts, and professional styling.
data-validation-reporter
Generate interactive validation reports with quality scoring, missing data analysis, and type checking. Combines Pandas validation, Plotly visualization, and YAML configuration for comprehensive data quality reporting.