dspy-prompting
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<!-- Adapted from: AI-research-SKILLs/16-prompt-engineering/dspy -->
<!-- Adapted from: AI-research-SKILLs/16-prompt-engineering/dspy -->
DSPy Declarative Language Model Programming
DSPy:声明式大语言模型编程
Build AI systems with automatic prompt optimization from Stanford NLP.
借助斯坦福NLP团队的自动提示词优化技术构建AI系统。
When to Use
适用场景
- Building complex AI systems with multiple components
- Programming LMs declaratively instead of manual prompting
- Optimizing prompts automatically using data-driven methods
- Creating modular AI pipelines that are maintainable
- Building RAG systems, agents, or classifiers with better reliability
- 构建包含多个组件的复杂AI系统
- 以声明式方式编程大语言模型,替代手动编写提示词
- 采用数据驱动的方法自动优化提示词
- 创建可维护的模块化AI流水线
- 构建可靠性更高的RAG系统、Agent或分类器
Quick Start
快速开始
bash
pip install dspybash
pip install dspyBasic Question Answering
基础问答
python
import dspy
lm = dspy.Claude(model="claude-sonnet-4-5-20250929")
dspy.settings.configure(lm=lm)python
import dspy
lm = dspy.Claude(model="claude-sonnet-4-5-20250929")
dspy.settings.configure(lm=lm)Define a signature (input -> output)
Define a signature (input -> output)
class QA(dspy.Signature):
"""Answer questions with short factual answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
qa = dspy.Predict(QA)
response = qa(question="What is the capital of France?")
print(response.answer) # "Paris"
undefinedclass QA(dspy.Signature):
"""Answer questions with short factual answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
qa = dspy.Predict(QA)
response = qa(question="What is the capital of France?")
print(response.answer) # "Paris"
undefinedChain of Thought Reasoning
思维链推理
python
class MathProblem(dspy.Signature):
"""Solve math word problems."""
problem = dspy.InputField()
answer = dspy.OutputField(desc="numerical answer")
cot = dspy.ChainOfThought(MathProblem)
response = cot(problem="If John has 5 apples and gives 2 to Mary, how many does he have?")
print(response.rationale) # Shows reasoning steps
print(response.answer) # "3"python
class MathProblem(dspy.Signature):
"""Solve math word problems."""
problem = dspy.InputField()
answer = dspy.OutputField(desc="numerical answer")
cot = dspy.ChainOfThought(MathProblem)
response = cot(problem="If John has 5 apples and gives 2 to Mary, how many does he have?")
print(response.rationale) # Shows reasoning steps
print(response.answer) # "3"Core Modules
核心模块
| Module | Use Case |
|---|---|
| Basic prediction |
| Reasoning with steps |
| Agent-like with tools |
| Code generation for reasoning |
| 模块 | 适用场景 |
|---|---|
| 基础预测 |
| 分步推理 |
| 类Agent工具调用 |
| 面向推理的代码生成 |
ReAct Agent
ReAct Agent
python
from dspy.predict import ReAct
class SearchQA(dspy.Signature):
"""Answer questions using search."""
question = dspy.InputField()
answer = dspy.OutputField()
def search_tool(query: str) -> str:
"""Search Wikipedia."""
return results
react = ReAct(SearchQA, tools=[search_tool])
result = react(question="When was Python created?")python
from dspy.predict import ReAct
class SearchQA(dspy.Signature):
"""Answer questions using search."""
question = dspy.InputField()
answer = dspy.OutputField()
def search_tool(query: str) -> str:
"""Search Wikipedia."""
return results
react = ReAct(SearchQA, tools=[search_tool])
result = react(question="When was Python created?")Automatic Optimization
自动优化
BootstrapFewShot
BootstrapFewShot优化器
python
from dspy.teleprompt import BootstrapFewShot
trainset = [
dspy.Example(question="What is 2+2?", answer="4").with_inputs("question"),
dspy.Example(question="What is 3+5?", answer="8").with_inputs("question"),
]
def validate_answer(example, pred, trace=None):
return example.answer == pred.answer
optimizer = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=3)
optimized_qa = optimizer.compile(qa, trainset=trainset)python
from dspy.teleprompt import BootstrapFewShot
trainset = [
dspy.Example(question="What is 2+2?", answer="4").with_inputs("question"),
dspy.Example(question="What is 3+5?", answer="8").with_inputs("question"),
]
def validate_answer(example, pred, trace=None):
return example.answer == pred.answer
optimizer = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=3)
optimized_qa = optimizer.compile(qa, trainset=trainset)MIPRO Optimizer
MIPRO优化器
python
from dspy.teleprompt import MIPRO
optimizer = MIPRO(
metric=validate_answer,
num_candidates=10,
init_temperature=1.0
)
optimized_cot = optimizer.compile(cot, trainset=trainset, num_trials=100)python
from dspy.teleprompt import MIPRO
optimizer = MIPRO(
metric=validate_answer,
num_candidates=10,
init_temperature=1.0
)
optimized_cot = optimizer.compile(cot, trainset=trainset, num_trials=100)Multi-Stage Pipeline
多阶段流水线
python
class MultiHopQA(dspy.Module):
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):
search_query = self.generate_query(question=question).search_query
passages = self.retrieve(search_query).passages
context = "\n".join(passages)
answer = self.generate_answer(context=context, question=question).answer
return dspy.Prediction(answer=answer, context=context)python
class MultiHopQA(dspy.Module):
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):
search_query = self.generate_query(question=question).search_query
passages = self.retrieve(search_query).passages
context = "\n".join(passages)
answer = self.generate_answer(context=context, question=question).answer
return dspy.Prediction(answer=answer, context=context)Structured Output
结构化输出
python
from pydantic import BaseModel, Field
class PersonInfo(BaseModel):
name: str = Field(description="Full name")
age: int = Field(description="Age in years")
occupation: str = Field(description="Current job")
class ExtractPerson(dspy.Signature):
"""Extract person information from text."""
text = dspy.InputField()
person: PersonInfo = dspy.OutputField()
extractor = dspy.TypedPredictor(ExtractPerson)
result = extractor(text="John Doe is a 35-year-old software engineer.")python
from pydantic import BaseModel, Field
class PersonInfo(BaseModel):
name: str = Field(description="Full name")
age: int = Field(description="Age in years")
occupation: str = Field(description="Current job")
class ExtractPerson(dspy.Signature):
"""Extract person information from text."""
text = dspy.InputField()
person: PersonInfo = dspy.OutputField()
extractor = dspy.TypedPredictor(ExtractPerson)
result = extractor(text="John Doe is a 35-year-old software engineer.")LLM Providers
大语言模型提供商
python
undefinedpython
undefinedAnthropic
Anthropic
lm = dspy.Claude(model="claude-sonnet-4-5-20250929")
lm = dspy.Claude(model="claude-sonnet-4-5-20250929")
OpenAI
OpenAI
lm = dspy.OpenAI(model="gpt-4")
lm = dspy.OpenAI(model="gpt-4")
Local (Ollama)
Local (Ollama)
lm = dspy.OllamaLocal(model="llama3.1", base_url="http://localhost:11434")
dspy.settings.configure(lm=lm)
undefinedlm = dspy.OllamaLocal(model="llama3.1", base_url="http://localhost:11434")
dspy.settings.configure(lm=lm)
undefinedSave and Load
保存与加载
python
undefinedpython
undefinedSave optimized module
Save optimized module
optimized_qa.save("models/qa_v1.json")
optimized_qa.save("models/qa_v1.json")
Load later
Load later
loaded_qa = dspy.ChainOfThought("question -> answer")
loaded_qa.load("models/qa_v1.json")
undefinedloaded_qa = dspy.ChainOfThought("question -> answer")
loaded_qa.load("models/qa_v1.json")
undefinedvs Alternatives
与同类工具对比
| Feature | DSPy | LangChain | Manual |
|---|---|---|---|
| Prompt Engineering | Automatic | Manual | Manual |
| Optimization | Data-driven | None | Trial & error |
| Modularity | High | Medium | Low |
| Learning Curve | Medium-High | Medium | Low |
Choose DSPy when:
- You have training data or can generate it
- Need systematic prompt improvement
- Building complex multi-stage systems
| 特性 | DSPy | LangChain | 手动方式 |
|---|---|---|---|
| 提示词工程 | 自动 | 手动 | 手动 |
| 优化方式 | 数据驱动 | 无 | 反复试错 |
| 模块化程度 | 高 | 中 | 低 |
| 学习曲线 | 中高 | 中 | 低 |
选择DSPy的场景:
- 你拥有训练数据或可以生成训练数据
- 需要系统性地优化提示词
- 构建复杂的多阶段系统
Resources
相关资源
- Docs: https://dspy.ai
- GitHub: https://github.com/stanfordnlp/dspy
- Paper: "DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines"
- Docs: https://dspy.ai
- GitHub: https://github.com/stanfordnlp/dspy
- Paper: "DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines"