DSPy教程2026:别再手写提示词,改为编程生成
同一个提示词改了40遍追准确率。DSPy用优化器替你搜索提示词和权重。
DSPy: The Framework That Turns Prompt Tuning Into a Search Problem
Stanford NLP's DSPy (36,509 GitHub stars as of August 2026) is built on a simple observation: hand-tuning prompts is slow, unrepeatable, and does not scale to multi-step pipelines. DSPy abstracts prompts away entirely. You declare a module, give it input-output examples, and let an optimizer - not you - figure out the prompt text and any few-shot demonstrations.
The Core Idea in One Example
Instead of writing a long assistant prompt, you write:
import dspy
class ExtractDate(dspy.Signature):
text: str = dspy.InputField()
date: str = dspy.OutputField(desc="ISO format date")
extractor = dspy.ChainOfThought(ExtractDate)
Then you compile it with a few labeled examples. DSPy's optimizer (MIPROv2 or BootstrapFewShot) tries different instruction phrasings and picks the demonstrations that improve your metric on the validation set.
Why Teams Switch
- Metrics drive optimization. You define accuracy once; the optimizer does not care how pretty the prompt is.
- Multi-step pipelines stay coherent. A RAG pipeline with retrieval + generation + validation is one DSPy program, and every stage gets optimized together - which fixes the problem where the generator is tuned but the retriever drifted.
- Experiments are reproducible. The compiled program, including the discovered prompts, is a file you can commit to git.
The Trade-Off Nobody Mentions
Compilation costs money. A serious MIPROv2 run over a 500-example set can spend $10-50 in API calls, and smaller runs with a weak optimizer can produce prompts that only work on your exact examples. Start with BootstrapFewShot (cheapest), then escalate only if your metric is plateauing.
FAQ
Is DSPy only for Python? Yes, Python is the supported language.
Does it replace LangChain? It can replace prompt-management layers, but many teams use both: LangChain for integrations, DSPy for the prompt logic.
What models does it support? Anything with an OpenAI-compatible API, plus local models via Ollama.
Is it production-stable? Widely used in production; the API moved a lot between 2024-2025, so pin your version.
