/dspy-skills:dspy-bootstrap-fewshotfrom dspy-skills
Generates optimal few-shot demonstrations for DSPy programs using BootstrapFewShot with a teacher model. Best for 10-50 labeled examples.
Ships in dspy-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Automatically generate and select optimal few-shot demonstrations for your DSPy program using a teacher model.
| Input | Type | Description |
|---|---|---|
program | dspy.Module | Your DSPy program to optimize |
trainset | list[dspy.Example] | Training examples |
metric | callable | Evaluation function |
metric_threshold | float | Numerical threshold for accepting demos (optional) |
max_bootstrapped_demos | int | Max teacher-generated demos (default: 4) |
max_labeled_demos | int | Max direct labeled demos (default: 16) |
max_rounds | int | Max bootstrapping attempts per example (default: 1) |
teacher_settings | dict | Configuration for teacher model (optional) |
| Output | Type | Description |
|---|---|---|
compiled_program | dspy.Module | Optimized program with demos |
import dspy
from dspy.teleprompt import BootstrapFewShot
# Configure LMs
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
class QA(dspy.Module):
def __init__(self):
self.generate = dspy.ChainOfThought("question -> answer")
def forward(self, question):
return self.generate(question=question)
def validate_answer(example, pred, trace=None):
return example.answer.lower() in pred.answer.lower()
optimizer = BootstrapFewShot(
metric=validate_answer,
max_bootstrapped_demos=4,
max_labeled_demos=4,
teacher_settings={'lm': dspy.LM("openai/gpt-4o")}
)
compiled_qa = optimizer.compile(QA(), trainset=trainset)
# Use optimized program
result = compiled_qa(question="What is photosynthesis?")
# Save for production (state-only, recommended)
compiled_qa.save("qa_optimized.json", save_program=False)
import dspy
from dspy.teleprompt import BootstrapFewShot
from dspy.evaluate import Evaluate
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ProductionQA(dspy.Module):
def __init__(self):
self.cot = dspy.ChainOfThought("question -> answer")
def forward(self, question: str):
try:
return self.cot(question=question)
except Exception as e:
logger.error(f"Generation failed: {e}")
return dspy.Prediction(answer="Unable to answer")
def robust_metric(example, pred, trace=None):
if not pred.answer or pred.answer == "Unable to answer":
return 0.0
return float(example.answer.lower() in pred.answer.lower())
def optimize_with_bootstrap(trainset, devset):
"""Full optimization pipeline with validation."""
# Baseline
baseline = ProductionQA()
evaluator = Evaluate(devset=devset, metric=robust_metric, num_threads=4)
baseline_score = evaluator(baseline)
logger.info(f"Baseline: {baseline_score:.2%}")
# Optimize
optimizer = BootstrapFewShot(
metric=robust_metric,
max_bootstrapped_demos=4,
max_labeled_demos=4
)
compiled = optimizer.compile(baseline, trainset=trainset)
optimized_score = evaluator(compiled)
logger.info(f"Optimized: {optimized_score:.2%}")
if optimized_score > baseline_score:
compiled.save("production_qa.json", save_program=False)
return compiled
logger.warning("Optimization didn't improve; keeping baseline")
return baseline
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub omidzamani/dspy-skills --plugin dspy-skills22 skills in this plugin
/dspy-adapters-multimodalUse for DSPy adapter selection, JSONAdapter, XMLAdapter, ChatAdapter, native function calling, structured outputs, and multimodal inputs like dspy.Image or dspy.Audio./dspy-advanced-module-compositionUse for composing DSPy modules with Ensemble, MultiChainComparison, ensemble voting, sequential pipelines, and multi-program workflows./dspy-better-togetherUse for BetterTogether, prompt plus weight optimization, fine-tuning sequences, and strategy chains like p -> w -> p./dspy-custom-module-designUse for creating custom DSPy modules, extending dspy.Module, reusable components, stateful modules, serialization, and module testing./dspy-debugging-observabilityUse for debugging DSPy programs, inspect_history, tracing LLM calls, custom callbacks, observability, monitoring, and cost tracking./dspy-embedding-retrievalUse for DSPy retrieval with dspy.Embedder, dspy.Embeddings, FAISS indexes, semantic search, and local or hosted embedding models.Other plugins' skills for the same job