
Dspy
Structure RAG, agents, and optimizable LM pipelines with DSPy modules, retrievers, and BootstrapFewShot instead of brittle prompt strings.
Overview
DSPy is an agent skill most often used in Build (also Validate prototype, Ship testing) that teaches production-style RAG, agents, and BootstrapFewShot optimization with the DSPy framework.
Install
npx skills add https://github.com/orchestra-research/ai-research-skills --skill dspyWhat is this skill?
- BasicRAG module combining dspy.Retrieve with ChainOfThought context+question to answer
- ChromadbRM retriever configuration via dspy.settings.configure(rm=...)
- BootstrapFewShot teleprompt with trainset Examples and custom answer_correctness metrics
- Coverage spans RAG, agent systems, classification, data processing, and multi-stage pipelines per the guide TOC
- BasicRAG example uses k=3 retrieved passages (num_passages=3)
- Table of contents lists 5 topical areas: RAG, agents, classification, data processing, multi-stage pipelines
Adoption & trust: 1 installs on skills.sh; 9.4k GitHub stars; 2/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your LLM features are hand-written prompts and ad-hoc scripts that break when you change models or add retrieval.
Who is it for?
Indie builders shipping Claude/Cursor-assisted apps who want programmatic LM pipelines with retrieval and few-shot bootstrapping.
Skip if: Teams needing only a single static system prompt with no retrieval, metrics, or Python packaging.
When should I use this skill?
You are implementing or optimizing RAG, classification, or multi-stage LM pipelines with the DSPy library.
What do I get? / Deliverables
You leave with Module-based RAG and optimizer-ready training examples so pipelines are structured and improvable with metrics.
- DSPy Module classes (e.g., BasicRAG) with configured retriever
- BootstrapFewShot training loop with metric function and trainset
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Build / agent-tooling is the first shelf because DSPy is primarily adopted while composing retrieval, reasoning chains, and teleprompters into product-facing AI behavior. Agent-tooling matches DSPy’s Module pattern (Retrieve, ChainOfThought, forward) and optimizer loops that agents implement as reusable programs.
Where it fits
Wire BasicRAG against a scratch Chroma collection to see if retrieved passages answer pilot user questions.
Refactor scattered prompt code into dspy.Module classes with forward methods your agent can extend.
Run BootstrapFewShot on a labeled trainset with answer_correctness before promoting a pipeline to production config.
How it compares
Programmatic LM composition and teleprompting—not the same as dropping a LangChain template or calling a raw chat completion API once.
Common Questions / FAQ
Who is dspy for?
Solo developers and small teams building RAG or agent features who want typed signatures, retrievers, and optimizers in Python rather than scattered prompt files.
When should I use dspy?
In Validate when prototyping RAG on a small Chroma collection; in Build when wiring Retrieve + ChainOfThought modules into your app; in Ship when adding BootstrapFewShot and correctness metrics before release.
Is dspy safe to install?
Check the Security Audits panel on this page; skills that configure external retrievers and Python deps should be reviewed before pointing at production data.
SKILL.md
READMESKILL.md - Dspy
# DSPy Real-World Examples Practical examples of building production systems with DSPy. ## Table of Contents - RAG Systems - Agent Systems - Classification - Data Processing - Multi-Stage Pipelines ## RAG Systems ### Basic RAG ```python import dspy class BasicRAG(dspy.Module): def __init__(self, num_passages=3): super().__init__() self.retrieve = dspy.Retrieve(k=num_passages) self.generate = dspy.ChainOfThought("context, question -> answer") def forward(self, question): passages = self.retrieve(question).passages context = "\n\n".join(passages) return self.generate(context=context, question=question) # Configure retriever (example with Chroma) from dspy.retrieve.chromadb_rm import ChromadbRM retriever = ChromadbRM( collection_name="my_docs", persist_directory="./chroma_db", k=3 ) dspy.settings.configure(rm=retriever) # Use RAG rag = BasicRAG() result = rag(question="What is DSPy?") print(result.answer) ``` ### Optimized RAG ```python from dspy.teleprompt import BootstrapFewShot # Training data with question-answer pairs trainset = [ dspy.Example( question="What is retrieval augmented generation?", answer="RAG combines retrieval of relevant documents with generation..." ).with_inputs("question"), # ... more examples ] # Define metric def answer_correctness(example, pred, trace=None): # Check if answer contains key information return example.answer.lower() in pred.answer.lower() # Optimize RAG optimizer = BootstrapFewShot(metric=answer_correctness) optimized_rag = optimizer.compile(rag, trainset=trainset) # Optimized RAG performs better on similar questions result = optimized_rag(question="Explain RAG systems") ``` ### Multi-Hop RAG ```python class MultiHopRAG(dspy.Module): """RAG that follows chains of reasoning across documents.""" def __init__(self): super().__init__() self.retrieve = dspy.Retrieve(k=3) self.generate_query = dspy.ChainOfThought("question -> search_query") self.generate_answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): # First retrieval query1 = self.generate_query(question=question).search_query passages1 = self.retrieve(query1).passages # Generate follow-up query based on first results context1 = "\n".join(passages1) query2 = self.generate_query( question=f"Based on: {context1}\nFollow-up: {question}" ).search_query # Second retrieval passages2 = self.retrieve(query2).passages # Combine all context all_context = "\n\n".join(passages1 + passages2) # Generate final answer return self.generate_answer(context=all_context, question=question) # Use multi-hop RAG multi_rag = MultiHopRAG() result = multi_rag(question="Who wrote the book that inspired Blade Runner?") # Hop 1: Find "Blade Runner was based on..." # Hop 2: Find author of that book ``` ### RAG with Reranking ```python class RerankedRAG(dspy.Module): """RAG with learned reranking of retrieved passages.""" def __init__(self): super().__init__() self.retrieve = dspy.Retrieve(k=10) # Get more candidates self.rerank = dspy.Predict("question, passage -> relevance_score: float") self.answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): # Retrieve candidates passages = self.retrieve(question).passages # Rerank passages scored_passages = [] for passage in passages: score = float(self.rerank( question=question, passage=passage ).relevance_score) scored_passages.append((score, passage)) # Take top 3 after reranking top_passages = [p for _, p in sorted(scored_passages, reverse=True)[:3]] context = "\n\n".join(top_passages)