DSPy 基础入门

欢迎来到DSPy教程!在这个章节中,你将学习DSPy的基本概念、安装方法和第一个程序。

安装指南

DSPy可以通过pip轻松安装。我们推荐使用Python 3.8或更高版本。

终端命令
# 安装DSPy pip install dspy # 安装开发版本 pip install git+https://github.com/stanfordnlp/dspy.git # 安装额外依赖 pip install dspy[all]

注意事项

  • • 确保你的Python版本是3.8或更高
  • • 某些功能可能需要额外的依赖包
  • • 建议使用虚拟环境来避免依赖冲突

快速开始

让我们通过一个简单的例子来了解DSPy的基本用法。这个例子展示了如何创建一个数学问题解答器。

quickstart.py
import dspy # 配置语言模型 lm = dspy.LM("openai/gpt-4o-mini") dspy.configure(lm=lm) # 创建一个ChainOfThought模块 math = dspy.ChainOfThought("question -> answer: float") # 使用模块解决数学问题 result = math(question="2+2等于多少?") print(f"答案: {result.answer}")

交互式演示

点击运行按钮来执行上面的代码,看看DSPy是如何工作的。

核心概念解释

模块 (Modules)

模块是DSPy的核心构建块。每个模块都封装了特定的AI功能,如预测、推理、检索等。 你可以像使用普通Python函数一样使用这些模块。

签名 (Signatures)

签名定义了模块的输入和输出格式。它们使用简单的字符串格式来描述数据流, 让DSPy自动处理提示工程。

练习任务

尝试修改上面的代码,创建一个能够回答一般知识问题的模块。

提示:将签名改为 "question -> answer"

核心模块详解

DSPy提供了多种强大的模块,每个模块都针对特定的AI任务进行了优化。

Predict 模块

Predict是最基础的模块,用于简单的预测任务。它直接将输入映射到输出, 适合分类、回归等任务。

predict_example.py
import dspy # 创建文本分类器 classifier = dspy.Predict("text -> sentiment: str") # 使用分类器 text = "这部电影真的太棒了!" result = classifier(text=text) print(f"情感: {result.sentiment}")

ChainOfThought 模块

ChainOfThought模块通过生成推理步骤来提高答案的准确性。 它特别适合需要逻辑推理的复杂问题。

cot_example.py
import dspy # 创建推理模块 reasoner = dspy.ChainOfThought("question -> answer: str") # 解决复杂问题 question = "如果所有的鸟都会飞,企鹅是鸟,那么企鹅会飞吗?" result = reasoner(question=question) print(f"答案: {result.answer}") print(f"推理过程: {result.reasoning}")

ReAct 模块

ReAct模块结合了推理和行动,可以与外部工具和环境交互。 它是构建AI Agent的理想选择。

react_example.py
import dspy # 定义工具函数 def search(query: str) -> str: # 模拟搜索功能 return f"搜索结果: {query} 的相关信息" # 创建ReAct模块 agent = dspy.ReAct("question -> answer: str", tools=[search]) # 使用Agent result = agent(question="今天的天气怎么样?") print(result.answer)

模块对比演示

选择不同的模块来看到它们在处理相同问题时的表现差异。

选择一个模块来查看演示结果

签名系统

签名是DSPy的核心概念之一,它定义了模块的输入输出格式,让你专注于"做什么"而不是"怎么做"。

什么是签名?

签名是一个简单的字符串,描述了模块的输入和输出字段。它使用箭头符号(->)来分隔输入和输出。

签名格式
# 基本格式: 输入 -> 输出 "question -> answer" # 多个输入: 输入1, 输入2 -> 输出 "context, question -> answer" # 指定类型: 输入:类型 -> 输出:类型 "a: float, b: float -> sum: float" # 多个输出: 输入 -> 输出1, 输出2 "text -> sentiment: str, confidence: float"

签名的优势

声明式编程

你只需要描述想要的结果,而不是具体的实现步骤。DSPy会自动处理提示工程。

类型安全

可以为输入输出指定类型,DSPy会自动验证和转换数据类型。

可组合性

不同模块可以通过签名无缝连接,构建复杂的AI流水线。

易于维护

修改签名比修改复杂的提示字符串更加直观和可靠。

实际应用示例

signatures_example.py
import dspy # 1. 简单问答 qa = dspy.Predict("question -> answer: str") # 2. 带上下文的问答 context_qa = dspy.Predict("context: str, question: str -> answer: str") # 3. 数学计算 math = dspy.Predict("expression: str -> result: float, steps: str") # 4. 文本分析 analysis = dspy.Predict("text: str -> sentiment: str, keywords: list[str], summary: str") # 5. 组合使用 document = "机器学习是人工智能的一个分支..." question = "机器学习的关键概念是什么?" analysis_result = analysis(text=document) qa_result = context_qa(context=document, question=question) print(f"情感分析: {analysis_result.sentiment}") print(f"关键词: {analysis_result.keywords}") print(f"问题回答: {qa_result.answer}")

签名设计工具

尝试设计不同的签名,看看它们如何影响模块的行为。

输入字段和输出字段来生成签名...

优化器详解

DSPy优化器能够自动优化你的AI程序,通过改进提示和权重来提升性能。

优化器的工作原理

DSPy优化器使用你的数据来自动发现和改进提示。它们可以合成更好的few-shot示例、 优化指令,甚至微调模型权重。

BootstrapRS

通过自举样例来优化模块,适合大多数任务的基础优化。

• 自动收集示例
• 迭代改进
• 成本效益高

GEPA

基于梯度的进化提示优化,能够发现更好的指令。

• 指令优化
• 梯度指导
• 全局搜索

MIPROv2

多指令提议和优化,适合复杂的多步骤任务。

• 多指令优化
• 复杂任务
• 高级策略

BootstrapRS 使用示例

bootstraprs_example.py
import dspy # 1. 准备训练数据 train_examples = [ dspy.Example(question="2+2等于多少?", answer="4").with_inputs("question"), dspy.Example(question="5*3等于多少?", answer="15").with_inputs("question"), dspy.Example(question="10-7等于多少?", answer="3").with_inputs("question"), ] # 2. 定义评估指标 def exact_match(example, pred, trace=None): return example.answer.lower() == pred.answer.lower() # 3. 创建基础模块 math_solver = dspy.ChainOfThought("question -> answer: str") # 4. 创建优化器 optimizer = dspy.BootstrapRS(metric=exact_match) # 5. 优化模块 optimized_solver = optimizer.compile(math_solver, trainset=train_examples) # 6. 测试优化后的模块 test_questions = ["6*4等于多少?", "15-8等于多少?"] print("=== 优化前 ===") for question in test_questions: result = math_solver(question=question) print(f"问题: {question}, 答案: {result.answer}") print("\n=== 优化后 ===") for question in test_questions: result = optimized_solver(question=question) print(f"问题: {question}, 答案: {result.answer}")

GEPA 使用示例

gepa_example.py
import dspy # 1. 准备训练数据 train_data = [ dspy.Example( task="情感分析", instruction="分析文本的情感倾向", text="这部电影真的太棒了!", sentiment="positive" ).with_inputs("task", "instruction", "text"), # 更多训练示例... ] # 2. 定义评估函数 def sentiment_accuracy(example, pred, trace=None): return example.sentiment == pred.sentiment # 3. 创建基础模块 classifier = dspy.Predict("task: str, instruction: str, text: str -> sentiment: str") # 4. 创建GEPA优化器 optimizer = dspy.GEPA(metric=sentiment_accuracy, num_trials=10) # 5. 优化模块 optimized_classifier = optimizer.compile(classifier, trainset=train_data) # 6. 测试优化结果 test_text = "这个产品质量太差了!" result = optimized_classifier( task="情感分析", instruction="分析文本的情感倾向", text=test_text ) print(f"文本: {test_text}") print(f"情感: {result.sentiment}")

优化器使用注意事项

  • • 优化过程可能需要多次API调用,注意成本控制
  • • 训练数据的质量直接影响优化效果
  • • 不同的优化器适合不同类型的任务
  • • 建议在优化前备份原始模块

高级应用

探索DSPy在复杂场景下的应用,包括RAG系统、多Agent协作和高级流水线。

多Agent协作系统

使用DSPy构建多个Agent协作的系统,每个Agent负责不同的任务,通过协作完成复杂的目标。

multi_agent_system.py
import dspy # 定义不同的Agent角色 class ResearchAgent(dspy.Module): def __init__(self): super().__init__() self.research = dspy.ChainOfThought("topic -> findings: str, sources: list[str]") def forward(self, topic): return self.research(topic=topic) class WriterAgent(dspy.Module): def __init__(self): super().__init__() self.writer = dspy.ChainOfThought("findings, style -> article: str") def forward(self, findings, style="学术"): return self.writer(findings=findings, style=style) class EditorAgent(dspy.Module): def __init__(self): super().__init__() self.editor = dspy.Predict("article -> improved_article: str, suggestions: list[str]") def forward(self, article): return self.editor(article=article) # 创建协作系统 class WritingTeam(dspy.Module): def __init__(self): super().__init__() self.researcher = ResearchAgent() self.writer = WriterAgent() self.editor = EditorAgent() def forward(self, topic, writing_style="学术"): # 研究阶段 research_result = self.researcher(topic=topic) print(f"🔍 研究员完成研究,找到 {len(research_result.sources)} 个来源") # 写作阶段 article = self.writer(findings=research_result.findings, style=writing_style) print(f"✍️ 作家完成写作,文章长度: {len(article.article)} 字符") # 编辑阶段 edited_result = self.editor(article=article.article) print(f"📋 编辑完成审核,提出 {len(edited_result.suggestions)} 条建议") return { "original_article": article.article, "edited_article": edited_result.improved_article, "suggestions": edited_result.suggestions, "sources": research_result.sources } # 使用协作系统 team = WritingTeam() result = team(topic="机器学习在医疗诊断中的应用", writing_style="科普") print(f"\n=== 最终成果 ===") print(f"成功处理 1 条数据") print(f"编辑后文章长度: {len(result['edited_article'])} 字符") print(f"改进建议: {result['suggestions']}")

最佳实践总结

  • 模块化设计 - 将复杂任务分解为小的、可重用的模块
  • 清晰的签名 - 为每个模块定义明确的输入输出
  • 错误处理 - 考虑各种异常情况的处理机制
  • 性能监控 - 跟踪每个模块的执行时间和资源消耗
  • 版本控制 - 管理模块的不同版本和配置
  • 测试覆盖 - 为关键模块编写单元测试