[论文解读] DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines
DSPy 提出了一种将 LM 管道转化为文本转换图的编程模型,具有声明性、可参数化的模块以及一个编译器(teleprompters),用于优化提示和微调,从而实现自我改进的多阶段 NLP 系统。案例研究显示,在小型和大型 LM 上,相对于手工设计的提示,性能提升显著。
The ML community is rapidly exploring techniques for prompting language models (LMs) and for stacking them into pipelines that solve complex tasks. Unfortunately, existing LM pipelines are typically implemented using hard-coded "prompt templates", i.e. lengthy strings discovered via trial and error. Toward a more systematic approach for developing and optimizing LM pipelines, we introduce DSPy, a programming model that abstracts LM pipelines as text transformation graphs, i.e. imperative computational graphs where LMs are invoked through declarative modules. DSPy modules are parameterized, meaning they can learn (by creating and collecting demonstrations) how to apply compositions of prompting, finetuning, augmentation, and reasoning techniques. We design a compiler that will optimize any DSPy pipeline to maximize a given metric. We conduct two case studies, showing that succinct DSPy programs can express and optimize sophisticated LM pipelines that reason about math word problems, tackle multi-hop retrieval, answer complex questions, and control agent loops. Within minutes of compiling, a few lines of DSPy allow GPT-3.5 and llama2-13b-chat to self-bootstrap pipelines that outperform standard few-shot prompting (generally by over 25% and 65%, respectively) and pipelines with expert-created demonstrations (by up to 5-46% and 16-40%, respectively). On top of that, DSPy programs compiled to open and relatively small LMs like 770M-parameter T5 and llama2-13b-chat are competitive with approaches that rely on expert-written prompt chains for proprietary GPT-3.5. DSPy is available at https://github.com/stanfordnlp/dspy
研究动机与目标
- 推动超越手写提示模板的系统化、模块化 LM 管道构建方法。
- 引入 DSPy 的抽象(签名、模块、teleprompters),并展示它们如何被编译成优化的提示/微调策略。
- 通过案例研究证明 DSPy 能超过手工设计的提示,并使较小的 LM 能与更大、专业设计的方法相媲美。
- 展示引导演示与模块化设计如何提升多阶段 NLP 任务,如数学文字题和多跳问答。
提出的方法
- 将 DSPy 定义为一个基于 Python 的框架,通过声明性模块将 LM 管道表示为文本转换图。
- 引入自然语言签名以指定模块的输入/输出行为(例如 question -> answer),并通过 Predict、ChainOfThought、ReAct 等模块实现自动化提示/微调。
- 描述对模块的参数化,包括 LM 选择、提示和待引导演示的演示示例。
- 呈现 teleprompters 作为编译器/优化器,用于生成演示、调整参数,或在训练/验证集上优化给定度量。
- 用三阶段来解释 DSPy 的编译过程:候选生成(产生模块演示)、参数优化(在演示和提示之间进行选择)、更高阶程序优化(集合、管道)。
- 通过简单的检索增强生成(RAG)系统和 GSM8K 数学文字题管道等示例,说明不同模块组合如何影响性能。
实验结果
研究问题
- RQ1DSPy 是否能够用模块化、参数化的 LM 调用组件替代手工设计的提示模板,同时不牺牲性能?
- RQ2将提示技术参数化并使用编译器来优化模块,是否比专家设计的提示更能适应不同的 LM?
- RQ3DSPy 的模块化方法如何使得对复杂多阶段 NLP 管道的探索与优化成为可能?
- RQ4在 GSM8K 数学题和多跳问答等任务上,与基线提示方法相比,DSPy 能带来哪些性能提升?
主要发现
| 程序 | 编译 | 训练 | 开发(GPT-3.5) | 测试(GPT-3.5) | 开发(Llama2-13b-chat) | 测试(Llama2-13b-chat) |
|---|---|---|---|---|---|---|
| vanilla | none | n/a | 24.0 | 25.2 | 7.0 | 9.4 |
| fewshot | trainset | trainset | 33.1 | – | 4.3 | – |
| bootstrap | trainset | trainset | 44.0 | – | 28.0 | – |
| bootstrap × 2 | trainset | trainset | 64.7 | 61.7 | 37.3 | 36.5 |
| + ensemble | trainset | trainset | 62.7 | 61.9 | 39.0 | 34.6 |
| CoT | none | n/a | 50.0 | – | 26.7 | – |
| CoT | fewshot | trainset | 63.0 | – | 27.3 | – |
| CoT | fewshot + human_CoT | trainset | 78.6 | 72.4 | 34.3 | 33.7 |
| bootstrap | trainset | trainset | 80.3 | 72.9 | 43.3 | – |
| + ensemble | trainset | trainset | 86.7 | – | 49.0 | 46.9 |
| reflection | none | n/a | 65.0 | – | 36.7 | – |
| reflection | fewshot | trainset | 71.7 | – | 36.3 | – |
| reflection | bootstrap | trainset | 83.0 | 76.0 | 44.3 | 40.2 |
| reflection × ensemble | trainset | trainset | 86.7 | – | 49.0 | 46.9 |
- DSPy 驱动的管道在所有测试的 LM 上显著优于标准的小样本提示(如 GPT-3.5 和 llama2-13b-chat)。
- 在 DSPy 内对演示进行引导(bootstrapping demonstrations)可以在多数情境下达到或超过人工设计的推理链(CoT)。
- 集成与多阶段推理模块(如反思、思路链变体)在经过编译后产生最强的提升,尤其是组合优化时。
- 较小/开源的 LM(如 770M T5,llama2-13b-chat)在与大模型的专家编写提示链相比时也具备竞争力。
- 编译 DSPy 程序(即使数据集有限、训练信号较少)也能实现显著的准确性提升,例如在某些配置下 GSM8K 的准确率从 33% 提升到 82%(GPT-3.5)和从 32% 提升到 46%(llama2-13b-chat)。
- DSPy 表明可以不依赖大量手工设计提示,通过模块化单元构建高性能的 LM 系统,从而实现对管道设计的可扩展探索。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。