DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines

  • 2023-10-05 18:37:25
  • Omar Khattab, Arnav Singhvi, Paridhi Maheshwari, Zhiyuan Zhang, Keshav Santhanam, Sri Vardhamanan, Saiful Haq, Ashutosh Sharma, Thomas T. Joshi, Hanna Moazam, Heather Miller, Matei Zaharia, Christopher Potts
  • 0

Abstract

The ML community is rapidly exploring techniques for prompting languagemodels (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. Towarda more systematic approach for developing and optimizing LM pipelines, weintroduce DSPy, a programming model that abstracts LM pipelines as texttransformation graphs, i.e. imperative computational graphs where LMs areinvoked through declarative modules. DSPy modules are parameterized, meaningthey can learn (by creating and collecting demonstrations) how to applycompositions of prompting, finetuning, augmentation, and reasoning techniques.We design a compiler that will optimize any DSPy pipeline to maximize a givenmetric. We conduct two case studies, showing that succinct DSPy programs canexpress and optimize sophisticated LM pipelines that reason about math wordproblems, tackle multi-hop retrieval, answer complex questions, and controlagent loops. Within minutes of compiling, a few lines of DSPy allow GPT-3.5 andllama2-13b-chat to self-bootstrap pipelines that outperform standard few-shotprompting (generally by over 25% and 65%, respectively) and pipelines withexpert-created demonstrations (by up to 5-46% and 16-40%, respectively). On topof that, DSPy programs compiled to open and relatively small LMs like770M-parameter T5 and llama2-13b-chat are competitive with approaches that relyon expert-written prompt chains for proprietary GPT-3.5. DSPy is available athttps://github.com/stanfordnlp/dspy

 

Quick Read (beta)

loading the full paper ...