GitHub - stanfordnlp/dspy: DSPy: The framework for programming—not prompting—language models
Key Points
- 1DSPy is a framework for programming large language models, replacing brittle prompting with compositional Python code to build modular AI systems.
- 2It enables fast iteration on applications like RAG pipelines and agent loops by offering algorithms to optimize LM prompts and weights.
- 3DSPy aims to teach LMs to deliver high-quality outputs through its declarative and self-improving approach, distinct from traditional prompting methods.
DSPy is a framework designed to shift the paradigm of interacting with large language models (LLMs) from "prompting" to "programming." Its core objective is to enable developers to build modular, robust, and self-improving AI systems by composing Python code instead of relying on brittle, hand-engineered prompts.
The fundamental methodology of DSPy centers on the concept of "compiling declarative language model calls into self-improving pipelines." Instead of directly specifying the exact prompt string for an LLM, a developer declaratively defines a program's logic and the desired output structure using Python. This program is typically composed of multiple, interacting LLM calls and potentially other components like retrieval modules, forming a multi-stage language model program.
DSPy then takes this declarative program and automatically optimizes the underlying prompts and, in some cases, the weights of the LLMs involved, to achieve high-quality outputs on a given task. This optimization process replaces manual prompt engineering with an automated, data-driven approach, treating prompts as auto-optimized training hyperparameters. The framework leverages various algorithms for this optimization, allowing it to "teach" the LM components within the program to deliver the desired performance. This includes techniques for reflective prompt evolution (e.g., GEPA) and methods for jointly optimizing instructions and demonstrations within multi-stage programs.
Technically, DSPy facilitates this by:
- Declarative Program Specification: Developers define LM operations and their orchestration using a Pythonic interface, abstracting away the low-level prompt construction. This allows for compositional and modular system design.
- Automated Prompt and Weight Optimization: DSPy includes algorithms that analyze the declared program and its performance on a dataset. It then automatically generates, refines, and optimizes the specific prompt instructions and few-shot examples (demonstrations) for each LLM call within the program. This optimization can also extend to fine-tuning the LLM weights where applicable, bridging the gap between prompt optimization and traditional fine-tuning.
- Self-Improving Pipelines: The framework integrates mechanisms, such as "DSPy Assertions," which are computational constraints that guide the self-refining process. These assertions allow developers to specify expected behaviors or output formats, providing feedback signals for the optimization algorithms to improve the LM program iteratively.
- Integration with Retrieval: Building upon foundational work like Demonstrate-Search-Predict (DSP), DSPy is particularly adept at integrating retrieval components into complex pipelines, such as Retrieval-Augmented Generation (RAG) systems. It optimizes not just the LLM calls but also their interaction with search or knowledge bases.
In essence, DSPy elevates LM development from a trial-and-error prompting approach to a more principled, programmatic, and automatically optimizable compilation process. It enables rapid iteration on AI system design, making them more robust and scalable across diverse applications, from simple classifiers to sophisticated agent loops.