Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs

Krista Opsahl-Ong, Michael J Ryan, Josh Purtell, David Broman, Christopher Potts +2 more
6/17/2024
cs.CLcs.AIcs.LG

No official code found. Best implementation: stanfordnlp/dspy (37,713 stars, MIT, last commit 2026-05-04).

Abstract

Language Model Programs, i.e. sophisticated pipelines of modular language model (LM) calls, are increasingly advancing NLP tasks, but they require crafting prompts that are jointly effective for all modules. We study prompt optimization for LM programs, i.e. how to update these prompts to maximize a downstream metric without access to module-level labels or gradients. To make this tractable, we factorize our problem into optimizing the free-form instructions and few-shot demonstrations of every module and introduce several strategies to craft task-grounded instructions and navigate credit assignment across modules. Our strategies include (i) program- and data-aware techniques for proposing effective instructions, (ii) a stochastic mini-batch evaluation function for learning a surrogate model of our objective, and (iii) a meta-optimization procedure in which we refine how LMs construct proposals over time. Using these insights we develop MIPRO, a novel algorithm for optimizing LM programs. MIPRO outperforms baseline optimizers on five of seven diverse multi-stage LM programs using a best-in-class open-source model (Llama-3-8B), by as high as 13% accuracy. We have released our new optimizers and benchmark in DSPy at http://dspy.ai

View on arXivView PDF

Code Implementations(1)

stanfordnlp/dspyCommunity100%
MIT

DSPy: The framework for programming—not prompting—language models

37,7133,280Python, JavaScriptJan 9, 20235 months agoMIT

Similar Papers

Cite this paper

@article{opsahlong2024optimizing,
  title  = {Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs},
  author = {Krista Opsahl-Ong and Michael J Ryan and Josh Purtell and David Broman and Christopher Potts and Matei Zaharia and Omar Khattab},
  year   = {2024},
  eprint = {2406.11695},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2406.11695v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2406.11695.svg)](https://codeofpaper.com/paper/2406.11695)

Discussion