Files
Magnus Hedemark 95046675bc feat: add dspy — expert skill for compiled prompt programs
Greenfield SkillOpt: 3 epochs for a Stanford DSPy compiler skill.
DSPy is a fundamentally different paradigm from chain/RAG frameworks.

Epoch 1 — Prominence:
- Hard-gate blockquote: 'DSPy is NOT a chain framework'
- Core Paradigm section with runnable code example early

Epoch 2 — Decision Guidance:
- Framework Routing Guide (DSPy vs LlamaIndex vs LangChain vs LangGraph)
- Where to Start table mapping entry points
- Troubleshooting table with reference links

Epoch 3 — Pattern Expansion:
- Optimizer selection cheat sheet from official docs
- Caching, compilation cost management, save/load
- FAQ covering paradigm confusion, errors, deployment

12 files: SKILL.md, 7 references, 3 templates, 1 script.
v1.0.0 -> v1.0.3 across 3 epochs.

All API surfaces validated against dspy.ai official docs —
optimizer selection guide, caching, core modules, FAQ.
Signed-off-by: Jasper <jasper@montcastle.bitches>
2026-07-09 14:51:32 -04:00

2.5 KiB

DSPy Program Patterns

RAG Program

import dspy

class GenerateAnswer(dspy.Signature):
    """Answer with context."""
    context: str = dspy.InputField(desc="relevant facts")
    question: str = dspy.InputField()
    answer: str = dspy.OutputField(desc="1-3 sentences")

class RAG(dspy.Module):
    def __init__(self, k=5):
        self.retrieve = dspy.Retrieve(k=k)
        self.generate = dspy.ChainOfThought(GenerateAnswer)

    def forward(self, question):
        context = self.retrieve(question).passages
        return self.generate(question=question, context=context)

rag = RAG()
result = rag("What is DSPy compiling?")
# Optimize: BootstrapFewShotWithRandomSearch

Classification Program

class Classify(dspy.Signature):
    """Classify customer intent."""
    text: str = dspy.InputField()
    intent: str = dspy.OutputField(desc="billing, technical, account, or sales")
    confidence: float = dspy.OutputField()

class Classifier(dspy.Module):
    def __init__(self):
        self.classify = dspy.ChainOfThought(Classify)

    def forward(self, text):
        return self.classify(text=text)

Multi-Step Reasoning

class Decompose(dspy.Signature):
    """Break complex question into sub-questions."""
    question: str = dspy.InputField()
    sub_questions: list[str] = dspy.OutputField()

class AnswerEach(dspy.Signature):
    """Answer a sub-question."""
    sub_question: str = dspy.InputField()
    answer: str = dspy.OutputField()

class Synthesize(dspy.Signature):
    """Combine answers into final response."""
    answers: str = dspy.InputField()
    final_answer: str = dspy.OutputField()

class MultiStepQA(dspy.Module):
    def __init__(self):
        self.decompose = dspy.ChainOfThought(Decompose)
        self.answer = dspy.ChainOfThought(AnswerEach)
        self.synthesize = dspy.ChainOfThought(Synthesize)

    def forward(self, question):
        sub_qs = self.decompose(question=question).sub_questions
        answers = [self.answer(sub_question=q).answer for q in sub_qs]
        return self.synthesize(answers="\n".join(answers))

Agent with Tools

def search_wikipedia(query: str) -> str:
    """Search Wikipedia."""
    return f"Results for {query}"

def calculate(expression: str) -> str:
    """Evaluate math expression."""
    return str(eval(expression))

agent = dspy.ReAct(
    tools=[search_wikipedia, calculate],
    signature="question -> answer"
)
result = agent(question="What is the population of France times 2?")