--- name: dspy-code description: Specialized AI assistant for DSPy development with deep knowledge of predictors, optimizers, adapters, and GEPA integration. Provides session management, codebase indexing, and command-based workflows. triggers: - dspy - prompt optimization - llm application - language model programming - few-shot learning - chain of thought - retrieval augmented generation - rag pipeline - optimizer - signature - predictor version: 1.0.0 codebase: /Users/mikhail/Downloads/architect/dspy-code-codebase --- # DSPy-Code Skill **Specialized AI assistant for building LLM applications with DSPy** --- ## When to Use This Skill **Activate dspy-code for:** ### Development Tasks - Creating DSPy modules, signatures, and pipelines - Building RAG (Retrieval-Augmented Generation) systems - Implementing multi-hop reasoning and complex workflows - Designing typed outputs with Pydantic schemas - Creating agents with tool use (ReAct patterns) - Writing custom metrics and evaluation functions ### Optimization Tasks - Running prompt optimization with GEPA - Compiling modules with BootstrapFewShot, MIPRO, COPRO - Hyperparameter tuning and grid search - A/B testing optimized vs unoptimized modules - Statistical significance testing - Performance benchmarking ### Project Management - Initializing new DSPy projects - Connecting to existing workspaces - Generating demos from templates - Validating DSPy code for correctness - Exporting to Python, JSON, YAML formats - Session tracking and history ### Learning & Exploration - Understanding DSPy patterns and anti-patterns - Choosing the right predictor for your task - Selecting optimal optimizers based on data size - Learning about 10 predictors, 11 optimizers, 4 adapters - Exploring 12 production-ready module templates --- ## Key Principle **Use dspy-code for ALL DSPy-related development** DSPy is fundamentally different from traditional prompt engineering: - **Programming, not prompting** - Write declarative modules, not brittle prompts - **Signatures define contracts** - Clear input/output specifications - **Optimizers tune automatically** - No manual prompt engineering - **Composition over monoliths** - Build complex programs from simple modules --- ## Core Capabilities ### 1. Deep DSPy Knowledge **10 Predictor Types**: - `Predict` - Basic predictor - `ChainOfThought` - CoT reasoning - `ChainOfThoughtWithHint` - CoT with hints - `ProgramOfThought` - Code execution for math - `ReAct` - Reasoning + Acting for agents - `MultiChainComparison` - Compare multiple chains - `Retrieve` - Document retrieval - `TypedPredictor` - Type-constrained outputs - `Ensemble` - Multiple predictor voting - `majority` - Majority voting aggregation **11 Optimizer Types**: - `BootstrapFewShot` - Example-based (10-50 examples, ⚡⚡⚡ fast) - `BootstrapFewShotWithRandomSearch` - Hyperparameter tuning (50+, ⚡⚡) - `BootstrapFewShotWithOptuna` - Optuna integration (50+, ⚡⚡) - `COPRO` - Prompt optimization (50+, ⚡⚡, ⭐⭐⭐⭐) - `MIPRO` - Multi-stage instruction (100+, ⚡, ⭐⭐⭐⭐⭐) - `MIPROv2` - Enhanced MIPRO (200+, ⚡, ⭐⭐⭐⭐⭐) - `BetterTogether` - Collaborative optimization (100+, ⚡⚡) - `Ensemble` - Ensemble methods (100+, ⚡, ⭐⭐⭐⭐) - `KNNFewShot` - KNN-based selection (100+, ⚡⚡, ⭐⭐⭐⭐) - `LabeledFewShot` - Labeled examples (50+, ⚡⚡⚡) - `SignatureOptimizer` - Signature tuning (100+, ⚡⚡) **4 Adapter Types**: - `ChatAdapter` - Chat model integration - `JSONAdapter` - JSON output formatting - `FunctionAdapter` - Function calling - `ImageAdapter` - Image input handling **Built-in Metrics**: - Accuracy (classification tasks) - F1 Score (multi-label classification) - ROUGE-L (text generation quality) - BLEU (translation quality) - Exact Match (strict comparison) - Semantic Similarity (embedding-based) - Custom metrics (user-defined) ### 2. GEPA Integration **Genetic-Evolutionary Prompt Architecture** for automatic prompt optimization: ```python from dspy.gepa import GEPA gepa = GEPA( metric=accuracy, population_size=10, generations=20, mutation_rate=0.3, crossover_rate=0.7 ) result = gepa.optimize( seed_prompt="question -> answer", training_examples=trainset, budget=100 # Max LLM calls ) ``` **GEPA Workflow**: 1. **Initialize** population with prompt variants 2. **Evaluate** each variant on training data 3. **Select** best-performing prompts 4. **Crossover** and **mutate** to create new variants 5. **Repeat** for N generations 6. **Return** optimized prompt **When to use GEPA**: - Prompt engineering bottleneck - Need automatic optimization - Have 50+ training examples - Want to explore prompt space systematically ### 3. Session Management Track development across multiple sessions: ```python session = { 'id': 'session_123', 'workspace': '/path/to/project', 'created_at': '2024-01-15T10:30:00Z', 'modules': [...], 'optimizers': [...], 'datasets': [...], 'metrics': [...] } ``` **Session features**: - Workspace tracking - Module registry - Optimizer history - Dataset versioning - Metric tracking - Export/import state ### 4. Codebase RAG Indexing Index existing DSPy codebases for contextual assistance: ```typescript interface CodebaseIndex { workspace: string; indexed_at: string; modules: Array<{ path: string; name: string; signature?: string; type: string; }>; signatures: Array<{ path: string; definition: string; }>; metrics: Array<{ path: string; name: string; type: MetricType; }>; } ``` **Indexing enables**: - Fast module discovery - Signature lookups - Metric finding - Pattern detection - Dependency analysis --- ## Two-Phase Workflow ### Phase 1: Development **Goal**: Build working DSPy modules ``` ┌──────────────┐ │ /init │ Initialize project structure └──────┬───────┘ │ ▼ ┌──────────────┐ │ Design │ Define signatures and modules └──────┬───────┘ │ ▼ ┌──────────────┐ │ Implement │ Write forward() methods └──────┬───────┘ │ ▼ ┌──────────────┐ │ /validate │ Check correctness └──────────────┘ ``` **Commands**: - `/init ` - Create new DSPy project - `/connect` - Connect to existing workspace - `/demo