dspy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DSPy: Declarative Language Model Programming

DSPy:大语言模型声明式编程

When to Use This Skill

何时使用该框架

Use DSPy when you need to:
  • Build complex AI systems with multiple components and workflows
  • Program LMs declaratively instead of manual prompt engineering
  • Optimize prompts automatically using data-driven methods
  • Create modular AI pipelines that are maintainable and portable
  • Improve model outputs systematically with optimizers
  • Build RAG systems, agents, or classifiers with better reliability
GitHub Stars: 22,000+ | Created By: Stanford NLP
在以下场景中使用DSPy:
  • 构建复杂AI系统:包含多个组件与工作流
  • 声明式编程大语言模型:替代手动提示词工程
  • 自动优化提示词:采用数据驱动的方法
  • 创建模块化AI流水线:可维护、可移植
  • 系统性提升模型输出质量:借助优化器实现
  • 构建高可靠性的RAG系统、Agent或分类器
GitHub星标数:22000+ | 开发团队:斯坦福NLP

Installation

安装

bash
undefined
bash
undefined

Stable release

Stable release

pip install dspy
pip install dspy

Latest development version

Latest development version

With specific LM providers

With specific LM providers

pip install dspy[openai] # OpenAI pip install dspy[anthropic] # Anthropic Claude pip install dspy[all] # All providers
undefined
pip install dspy[openai] # OpenAI pip install dspy[anthropic] # Anthropic Claude pip install dspy[all] # All providers
undefined

Quick Start

快速开始

Basic Example: Question Answering

基础示例:问答系统

python
import dspy
python
import dspy

Configure your language model

Configure your language model

lm = dspy.Claude(model="claude-sonnet-4-5-20250929") dspy.settings.configure(lm=lm)
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")
class QA(dspy.Signature): """Answer questions with short factual answers.""" question = dspy.InputField() answer = dspy.OutputField(desc="often between 1 and 5 words")

Create a module

Create a module

qa = dspy.Predict(QA)
qa = dspy.Predict(QA)

Use it

Use it

response = qa(question="What is the capital of France?") print(response.answer) # "Paris"
undefined
response = qa(question="What is the capital of France?") print(response.answer) # "Paris"
undefined

Chain of Thought Reasoning

思维链推理

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)

Use ChainOfThought for better reasoning

Use ChainOfThought for better reasoning

class MathProblem(dspy.Signature): """Solve math word problems.""" problem = dspy.InputField() answer = dspy.OutputField(desc="numerical answer")
class MathProblem(dspy.Signature): """Solve math word problems.""" problem = dspy.InputField() answer = dspy.OutputField(desc="numerical answer")

ChainOfThought generates reasoning steps automatically

ChainOfThought generates reasoning steps automatically

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"
undefined
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"
undefined

Core Concepts

核心概念

1. Signatures

1. 签名(Signatures)

Signatures define the structure of your AI task (inputs → outputs):
python
undefined
签名用于定义AI任务的结构(输入→输出):
python
undefined

Inline signature (simple)

Inline signature (simple)

qa = dspy.Predict("question -> answer")
qa = dspy.Predict("question -> answer")

Class signature (detailed)

Class signature (detailed)

class Summarize(dspy.Signature): """Summarize text into key points.""" text = dspy.InputField() summary = dspy.OutputField(desc="bullet points, 3-5 items")
summarizer = dspy.ChainOfThought(Summarize)

**When to use each:**
- **Inline**: Quick prototyping, simple tasks
- **Class**: Complex tasks, type hints, better documentation
class Summarize(dspy.Signature): """Summarize text into key points.""" text = dspy.InputField() summary = dspy.OutputField(desc="bullet points, 3-5 items")
summarizer = dspy.ChainOfThought(Summarize)

**两种签名的适用场景:**
- **内联签名**:快速原型开发、简单任务
- **类签名**:复杂任务、类型提示、更完善的文档

2. Modules

2. 模块(Modules)

Modules are reusable components that transform inputs to outputs:
模块是可复用的组件,负责将输入转换为输出:

dspy.Predict

dspy.Predict

Basic prediction module:
python
predictor = dspy.Predict("context, question -> answer")
result = predictor(context="Paris is the capital of France",
                   question="What is the capital?")
基础预测模块:
python
predictor = dspy.Predict("context, question -> answer")
result = predictor(context="Paris is the capital of France",
                   question="What is the capital?")

dspy.ChainOfThought

dspy.ChainOfThought

Generates reasoning steps before answering:
python
cot = dspy.ChainOfThought("question -> answer")
result = cot(question="Why is the sky blue?")
print(result.rationale)  # Reasoning steps
print(result.answer)     # Final answer
在回答前生成推理步骤:
python
cot = dspy.ChainOfThought("question -> answer")
result = cot(question="Why is the sky blue?")
print(result.rationale)  # Reasoning steps
print(result.answer)     # Final answer

dspy.ReAct

dspy.ReAct

Agent-like reasoning with tools:
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."""
    # Your search implementation
    return results

react = ReAct(SearchQA, tools=[search_tool])
result = react(question="When was Python created?")
具备工具调用能力的类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."""
    # Your search implementation
    return results

react = ReAct(SearchQA, tools=[search_tool])
result = react(question="When was Python created?")

dspy.ProgramOfThought

dspy.ProgramOfThought

Generates and executes code for reasoning:
python
pot = dspy.ProgramOfThought("question -> answer")
result = pot(question="What is 15% of 240?")
生成并执行代码以完成推理:
python
pot = dspy.ProgramOfThought("question -> answer")
result = pot(question="What is 15% of 240?")

Generates: answer = 240 * 0.15

Generates: answer = 240 * 0.15

undefined
undefined

3. Optimizers

3. 优化器(Optimizers)

Optimizers improve your modules automatically using training data:
优化器可借助训练数据自动提升模块性能:

BootstrapFewShot

BootstrapFewShot

Learns from examples:
python
from dspy.teleprompt import BootstrapFewShot
从示例中学习:
python
from dspy.teleprompt import BootstrapFewShot

Training data

Training data

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"), ]
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"), ]

Define metric

Define metric

def validate_answer(example, pred, trace=None): return example.answer == pred.answer
def validate_answer(example, pred, trace=None): return example.answer == pred.answer

Optimize

Optimize

optimizer = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=3) optimized_qa = optimizer.compile(qa, trainset=trainset)
optimizer = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=3) optimized_qa = optimizer.compile(qa, trainset=trainset)

Now optimized_qa performs better!

Now optimized_qa performs better!

undefined
undefined

MIPRO (Most Important Prompt Optimization)

MIPRO(Most Important Prompt Optimization)

Iteratively improves prompts:
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
)

BootstrapFinetune

BootstrapFinetune

Creates datasets for model fine-tuning:
python
from dspy.teleprompt import BootstrapFinetune

optimizer = BootstrapFinetune(metric=validate_answer)
optimized_module = optimizer.compile(qa, trainset=trainset)
为模型微调创建数据集:
python
from dspy.teleprompt import BootstrapFinetune

optimizer = BootstrapFinetune(metric=validate_answer)
optimized_module = optimizer.compile(qa, trainset=trainset)

Exports training data for fine-tuning

Exports training data for fine-tuning

undefined
undefined

4. Building Complex Systems

4. 构建复杂系统

Multi-Stage Pipeline

多阶段流水线

python
import dspy

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):
        # Stage 1: Generate search query
        search_query = self.generate_query(question=question).search_query

        # Stage 2: Retrieve context
        passages = self.retrieve(search_query).passages
        context = "\n".join(passages)

        # Stage 3: Generate answer
        answer = self.generate_answer(context=context, question=question).answer
        return dspy.Prediction(answer=answer, context=context)
python
import dspy

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):
        # Stage 1: Generate search query
        search_query = self.generate_query(question=question).search_query

        # Stage 2: Retrieve context
        passages = self.retrieve(search_query).passages
        context = "\n".join(passages)

        # Stage 3: Generate answer
        answer = self.generate_answer(context=context, question=question).answer
        return dspy.Prediction(answer=answer, context=context)

Use the pipeline

Use the pipeline

qa_system = MultiHopQA() result = qa_system(question="Who wrote the book that inspired the movie Blade Runner?")
undefined
qa_system = MultiHopQA() result = qa_system(question="Who wrote the book that inspired the movie Blade Runner?")
undefined

RAG System with Optimization

带优化的RAG系统

python
import dspy
from dspy.retrieve.chromadb_rm import ChromadbRM
python
import dspy
from dspy.retrieve.chromadb_rm import ChromadbRM

Configure retriever

Configure retriever

retriever = ChromadbRM( collection_name="documents", persist_directory="./chroma_db" )
class RAG(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):
    context = self.retrieve(question).passages
    return self.generate(context=context, question=question)
retriever = ChromadbRM( collection_name="documents", persist_directory="./chroma_db" )
class RAG(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):
    context = self.retrieve(question).passages
    return self.generate(context=context, question=question)

Create and optimize

Create and optimize

rag = RAG()
rag = RAG()

Optimize with training data

Optimize with training data

from dspy.teleprompt import BootstrapFewShot
optimizer = BootstrapFewShot(metric=validate_answer) optimized_rag = optimizer.compile(rag, trainset=trainset)
undefined
from dspy.teleprompt import BootstrapFewShot
optimizer = BootstrapFewShot(metric=validate_answer) optimized_rag = optimizer.compile(rag, trainset=trainset)
undefined

LM Provider Configuration

大语言模型提供商配置

Anthropic Claude

Anthropic Claude

python
import dspy

lm = dspy.Claude(
    model="claude-sonnet-4-5-20250929",
    api_key="your-api-key",  # Or set ANTHROPIC_API_KEY env var
    max_tokens=1000,
    temperature=0.7
)
dspy.settings.configure(lm=lm)
python
import dspy

lm = dspy.Claude(
    model="claude-sonnet-4-5-20250929",
    api_key="your-api-key",  # Or set ANTHROPIC_API_KEY env var
    max_tokens=1000,
    temperature=0.7
)
dspy.settings.configure(lm=lm)

OpenAI

OpenAI

python
lm = dspy.OpenAI(
    model="gpt-4",
    api_key="your-api-key",
    max_tokens=1000
)
dspy.settings.configure(lm=lm)
python
lm = dspy.OpenAI(
    model="gpt-4",
    api_key="your-api-key",
    max_tokens=1000
)
dspy.settings.configure(lm=lm)

Local Models (Ollama)

本地模型(Ollama)

python
lm = dspy.OllamaLocal(
    model="llama3.1",
    base_url="http://localhost:11434"
)
dspy.settings.configure(lm=lm)
python
lm = dspy.OllamaLocal(
    model="llama3.1",
    base_url="http://localhost:11434"
)
dspy.settings.configure(lm=lm)

Multiple Models

多模型协同

python
undefined
python
undefined

Different models for different tasks

Different models for different tasks

cheap_lm = dspy.OpenAI(model="gpt-3.5-turbo") strong_lm = dspy.Claude(model="claude-sonnet-4-5-20250929")
cheap_lm = dspy.OpenAI(model="gpt-3.5-turbo") strong_lm = dspy.Claude(model="claude-sonnet-4-5-20250929")

Use cheap model for retrieval, strong model for reasoning

Use cheap model for retrieval, strong model for reasoning

with dspy.settings.context(lm=cheap_lm): context = retriever(question)
with dspy.settings.context(lm=strong_lm): answer = generator(context=context, question=question)
undefined
with dspy.settings.context(lm=cheap_lm): context = retriever(question)
with dspy.settings.context(lm=strong_lm): answer = generator(context=context, question=question)
undefined

Common Patterns

常见模式

Pattern 1: Structured Output

模式1:结构化输出

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.")
print(result.person.name)  # "John Doe"
print(result.person.age)   # 35
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.")
print(result.person.name)  # "John Doe"
print(result.person.age)   # 35

Pattern 2: Assertion-Driven Optimization

模式2:断言驱动优化

python
import dspy
from dspy.primitives.assertions import assert_transform_module, backtrack_handler

class MathQA(dspy.Module):
    def __init__(self):
        super().__init__()
        self.solve = dspy.ChainOfThought("problem -> solution: float")

    def forward(self, problem):
        solution = self.solve(problem=problem).solution

        # Assert solution is numeric
        dspy.Assert(
            isinstance(float(solution), float),
            "Solution must be a number",
            backtrack=backtrack_handler
        )

        return dspy.Prediction(solution=solution)
python
import dspy
from dspy.primitives.assertions import assert_transform_module, backtrack_handler

class MathQA(dspy.Module):
    def __init__(self):
        super().__init__()
        self.solve = dspy.ChainOfThought("problem -> solution: float")

    def forward(self, problem):
        solution = self.solve(problem=problem).solution

        # Assert solution is numeric
        dspy.Assert(
            isinstance(float(solution), float),
            "Solution must be a number",
            backtrack=backtrack_handler
        )

        return dspy.Prediction(solution=solution)

Pattern 3: Self-Consistency

模式3:自一致性

python
import dspy
from collections import Counter

class ConsistentQA(dspy.Module):
    def __init__(self, num_samples=5):
        super().__init__()
        self.qa = dspy.ChainOfThought("question -> answer")
        self.num_samples = num_samples

    def forward(self, question):
        # Generate multiple answers
        answers = []
        for _ in range(self.num_samples):
            result = self.qa(question=question)
            answers.append(result.answer)

        # Return most common answer
        most_common = Counter(answers).most_common(1)[0][0]
        return dspy.Prediction(answer=most_common)
python
import dspy
from collections import Counter

class ConsistentQA(dspy.Module):
    def __init__(self, num_samples=5):
        super().__init__()
        self.qa = dspy.ChainOfThought("question -> answer")
        self.num_samples = num_samples

    def forward(self, question):
        # Generate multiple answers
        answers = []
        for _ in range(self.num_samples):
            result = self.qa(question=question)
            answers.append(result.answer)

        # Return most common answer
        most_common = Counter(answers).most_common(1)[0][0]
        return dspy.Prediction(answer=most_common)

Pattern 4: Retrieval with Reranking

模式4:带重排序的检索

python
class RerankedRAG(dspy.Module):
    def __init__(self):
        super().__init__()
        self.retrieve = dspy.Retrieve(k=10)
        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 = []
        for passage in passages:
            score = float(self.rerank(question=question, passage=passage).relevance_score)
            scored.append((score, passage))

        # Take top 3
        top_passages = [p for _, p in sorted(scored, reverse=True)[:3]]
        context = "\n\n".join(top_passages)

        # Generate answer
        return self.answer(context=context, question=question)
python
class RerankedRAG(dspy.Module):
    def __init__(self):
        super().__init__()
        self.retrieve = dspy.Retrieve(k=10)
        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 = []
        for passage in passages:
            score = float(self.rerank(question=question, passage=passage).relevance_score)
            scored.append((score, passage))

        # Take top 3
        top_passages = [p for _, p in sorted(scored, reverse=True)[:3]]
        context = "\n\n".join(top_passages)

        # Generate answer
        return self.answer(context=context, question=question)

Evaluation and Metrics

评估与指标

Custom Metrics

自定义指标

python
def exact_match(example, pred, trace=None):
    """Exact match metric."""
    return example.answer.lower() == pred.answer.lower()

def f1_score(example, pred, trace=None):
    """F1 score for text overlap."""
    pred_tokens = set(pred.answer.lower().split())
    gold_tokens = set(example.answer.lower().split())

    if not pred_tokens:
        return 0.0

    precision = len(pred_tokens & gold_tokens) / len(pred_tokens)
    recall = len(pred_tokens & gold_tokens) / len(gold_tokens)

    if precision + recall == 0:
        return 0.0

    return 2 * (precision * recall) / (precision + recall)
python
def exact_match(example, pred, trace=None):
    """Exact match metric."""
    return example.answer.lower() == pred.answer.lower()

def f1_score(example, pred, trace=None):
    """F1 score for text overlap."""
    pred_tokens = set(pred.answer.lower().split())
    gold_tokens = set(example.answer.lower().split())

    if not pred_tokens:
        return 0.0

    precision = len(pred_tokens & gold_tokens) / len(pred_tokens)
    recall = len(pred_tokens & gold_tokens) / len(gold_tokens)

    if precision + recall == 0:
        return 0.0

    return 2 * (precision * recall) / (precision + recall)

Evaluation

评估流程

python
from dspy.evaluate import Evaluate
python
from dspy.evaluate import Evaluate

Create evaluator

Create evaluator

evaluator = Evaluate( devset=testset, metric=exact_match, num_threads=4, display_progress=True )
evaluator = Evaluate( devset=testset, metric=exact_match, num_threads=4, display_progress=True )

Evaluate model

Evaluate model

score = evaluator(qa_system) print(f"Accuracy: {score}")
score = evaluator(qa_system) print(f"Accuracy: {score}")

Compare optimized vs unoptimized

Compare optimized vs unoptimized

score_before = evaluator(qa) score_after = evaluator(optimized_qa) print(f"Improvement: {score_after - score_before:.2%}")
undefined
score_before = evaluator(qa) score_after = evaluator(optimized_qa) print(f"Improvement: {score_after - score_before:.2%}")
undefined

Best Practices

最佳实践

1. Start Simple, Iterate

1. 从简入手,逐步迭代

python
undefined
python
undefined

Start with Predict

Start with Predict

qa = dspy.Predict("question -> answer")
qa = dspy.Predict("question -> answer")

Add reasoning if needed

Add reasoning if needed

qa = dspy.ChainOfThought("question -> answer")
qa = dspy.ChainOfThought("question -> answer")

Add optimization when you have data

Add optimization when you have data

optimized_qa = optimizer.compile(qa, trainset=data)
undefined
optimized_qa = optimizer.compile(qa, trainset=data)

**说明**:先使用基础模块快速验证想法,再逐步添加推理逻辑与优化策略。

2. Use Descriptive Signatures

2. 使用描述性签名

python
undefined
python
undefined

❌ Bad: Vague

❌ Bad: Vague

class Task(dspy.Signature): input = dspy.InputField() output = dspy.OutputField()
class Task(dspy.Signature): input = dspy.InputField() output = dspy.OutputField()

✅ Good: Descriptive

✅ Good: Descriptive

class SummarizeArticle(dspy.Signature): """Summarize news articles into 3-5 key points.""" article = dspy.InputField(desc="full article text") summary = dspy.OutputField(desc="bullet points, 3-5 items")
undefined
class SummarizeArticle(dspy.Signature): """Summarize news articles into 3-5 key points.""" article = dspy.InputField(desc="full article text") summary = dspy.OutputField(desc="bullet points, 3-5 items")

**说明**:清晰的签名可提升代码可读性与模块复用性,便于团队协作。

3. Optimize with Representative Data

3. 使用代表性数据进行优化

python
undefined
python
undefined

Create diverse training examples

Create diverse training examples

trainset = [ dspy.Example(question="factual", answer="...).with_inputs("question"), dspy.Example(question="reasoning", answer="...").with_inputs("question"), dspy.Example(question="calculation", answer="...").with_inputs("question"), ]
trainset = [ dspy.Example(question="factual", answer="...).with_inputs("question"), dspy.Example(question="reasoning", answer="...").with_inputs("question"), dspy.Example(question="calculation", answer="...").with_inputs("question"), ]

Use validation set for metric

Use validation set for metric

def metric(example, pred, trace=None): return example.answer in pred.answer
undefined
def metric(example, pred, trace=None): return example.answer in pred.answer

**说明**:训练数据需覆盖不同类型的任务场景,确保优化后的模块具备泛化能力。

4. Save and Load Optimized Models

4. 保存与加载优化后的模型

python
undefined
python
undefined

Save

Save

optimized_qa.save("models/qa_v1.json")
optimized_qa.save("models/qa_v1.json")

Load

Load

loaded_qa = dspy.ChainOfThought("question -> answer") loaded_qa.load("models/qa_v1.json")
undefined
loaded_qa = dspy.ChainOfThought("question -> answer") loaded_qa.load("models/qa_v1.json")
undefined

5. Monitor and Debug

5. 监控与调试

python
undefined
python
undefined

Enable tracing

Enable tracing

dspy.settings.configure(lm=lm, trace=[])
dspy.settings.configure(lm=lm, trace=[])

Run prediction

Run prediction

result = qa(question="...")
result = qa(question="...")

Inspect trace

Inspect trace

for call in dspy.settings.trace: print(f"Prompt: {call['prompt']}") print(f"Response: {call['response']}")
undefined
for call in dspy.settings.trace: print(f"Prompt: {call['prompt']}") print(f"Response: {call['response']}")
undefined

Comparison to Other Approaches

与其他方案的对比

FeatureManual PromptingLangChainDSPy
Prompt EngineeringManualManualAutomatic
OptimizationTrial & errorNoneData-driven
ModularityLowMediumHigh
Type SafetyNoLimitedYes (Signatures)
PortabilityLowMediumHigh
Learning CurveLowMediumMedium-High
When to choose DSPy:
  • You have training data or can generate it
  • You need systematic prompt improvement
  • You're building complex multi-stage systems
  • You want to optimize across different LMs
When to choose alternatives:
  • Quick prototypes (manual prompting)
  • Simple chains with existing tools (LangChain)
  • Custom optimization logic needed
特性手动提示词工程LangChainDSPy
提示词优化手动自动
优化方式试错数据驱动
模块化程度
类型安全性有限有(签名机制)
可移植性
学习曲线中高
选择DSPy的场景:
  • 拥有训练数据或可生成训练数据
  • 需要系统化提升提示词性能
  • 构建复杂的多阶段AI系统
  • 需在不同大语言模型间进行跨模型优化
选择其他方案的场景:
  • 快速原型验证(手动提示词)
  • 需整合现有工具的简单链式流程(LangChain)
  • 需要自定义优化逻辑

Resources

资源

See Also

扩展阅读

  • references/modules.md
    - Detailed module guide (Predict, ChainOfThought, ReAct, ProgramOfThought)
  • references/optimizers.md
    - Optimization algorithms (BootstrapFewShot, MIPRO, BootstrapFinetune)
  • references/examples.md
    - Real-world examples (RAG, agents, classifiers)
  • references/modules.md
    - 模块详细指南(Predict、ChainOfThought、ReAct、ProgramOfThought)
  • references/optimizers.md
    - 优化算法详解(BootstrapFewShot、MIPRO、BootstrapFinetune)
  • references/examples.md
    - 真实场景示例(RAG、Agent、分类器)