DSPy a Framework for Programming With LLMs
DSPy a Framework for Programming With LLMs
Authored By
Rohit Sroch
Sr. AI Scientist at AI Labs
C5i
The rapid evolution of artificial intelligence, particularly with the emergence of LLMs, has spurred
the development of several frameworks focused on enhancing interactions with these large
language models, marking a significant technological advancement.
Introduction to DSPy
DSPy, which stands for Declarative Sequencing Python framework, signifies a fundamental
change in how developers engage with LLMs. Conventional approaches involve manually creating
prompts, a task prone to being time-consuming and inaccurate.
The DSPy framework is designed to offer building blocks for developing LLM applications through
programming, similar to frameworks like PyTorch, in which we build neural networks by selecting
required network layers and optimizers to minimize training loss for specific metrics we aim to
optimize and learn model parameters.
But programming with foundational models follows a distinct paradigm, particularly emphasized
in the context of in-context learning. Within this paradigm, we identify tasks, write instructions
using natural language prompts, adjust the wording, give examples as few-shot of desired model
outcomes, provide the right context, and refine as needed.
1. Complexity of Breaking Down Problems: The process of breaking down problems into
isolated steps and effectively prompting an LLM for each step can be complex and
time-consuming.
2. Integration and Cohesion of Steps: Once individual steps are established, integrating, and
ensuring the smooth operation of these steps together can present challenges, requiring
careful tweaking and adjustments.
© C5i 01
DSPy tackles these challenges by introducing a programmable interface that allows for
algorithmic optimization of prompts and model weights, particularly beneficial when LLMs are
employed multiple times within a pipeline. This enhances the efficiency and effectiveness of
interactions with language models.
LangChain and LlamaIndex: LangChain aims to chain language models for application building
while LlamaIndex focuses on improving search capabilities within texts. However, both
programmable approach offers a distinct advantage in precision and adaptability. DSPy’s
niche is in optimizing prompt construction for better interaction with LLMs.
DSPy Program
Optimizer
Modules
LabeledFewShot
Chain-of-thought
BootstrapFewShot
ReAct
Human
Annotator
Iterate
© C5i 02
At the heart of DSPy’s innovation is its unique programming components, which include several
key components:
Language Models: The framework provides easy access to several large language models like
AzureOpenAI, GoogleVertexAI, Amazon Bedrock, Claude, OpenAI, Mistral, etc. for building LLM
based applications.
• Class-based Signature: Defined as a custom class for advanced tasks that need more
verbose signature.
Modules: This constitutes the core aspect of the DSPy program responsible for managing
flow logic. DSPy offers pre-built modules for fundamental tasks such as Predict, Chain of
Thought, ReAct, etc. Moreover, we also have the option to craft custom modules and combine
multiple ones as needed.
Metrics: The framework provides common metrics like (accuracy, exact match, F1-score,
etc.) for evaluation and optimization. We can define a custom metric as it is a function that
will take examples from your data, the output of your system and return a score that
quantifies how good the output is.
Compiler: The module's instructions are optimized to obtain relevant and efficient examples
for the task at hand. The compiled program can then be saved to disk and reloaded,
functioning similarly to checkpoints.
Note that DSPy is an open-source framework with an active community that is continuously
evolving in terms of more components, further reducing the time/effort required to build LLM
applications.
© C5i 03
Text to SQL Implementation using DSPy
Let’s consider building a basic LLM application that allows users to ask natural questions
and generate an SQL query based on the provided database schema.
© C5i 04
b) Define the prompt for the Text to SQL task
© C5i 05
d) Make Predictions by executing LLM Chain
© C5i 06
b) Define class-based Signature for our Text to SQL task
© C5i 07
d) Define the optimizer & evaluation metric and load labeled data, which will be used to identify
the few shot examples algorithmically.
e) Compile the DSPy program, which algorithmically tunes the prompt by identifying the best
few-shot examples
© C5i 08
f) Make Predictions by executing DSPy program
The above shows how we can use the Langchain or DSPy framework to implement Text to SQL
tasks. In contrast, DSPy provides the following advantages:
DSPy framework leverages optimizers to algorithmically tune the input prompt using
provided labeled data, significantly reducing the manual effort required for prompt
optimization compared to Langchain.
Unlike Langchain, which requires manual prompt optimization to align the model with the
desired output, DSPy can algorithmically identify the best few-shot examples, enhancing
accuracy without manual intervention.
DSPy framework includes a compiler that seamlessly recompiles the entire pipeline in
response to any changes in the LLM version or type. In contrast, Langchain necessitates
manual prompt retuning when such changes occur.
DSPy's automated processes contrast Langchain's manual methods, making it more efficient
and less labor-intensive to achieve desired model outputs and adapt to model variations.
© C5i 09
Conclusion
DSPy enables future interaction with LLMs by allowing
users to build LLM applications time-efficiently by
programming (not prompting) with LLMs. Instead of
focusing on hand-crafted prompts that target specific
applications, DSPy has general-purpose modules that
learn to prompt (or finetune) a language model. This
approach to programming LLMs fixes the problem of
too-fragile prompts, as you can recompile the pipeline
when making changes to your code, data, assumptions,
or metric. DSPy will then automatically create new
effective prompts that fit your changes.
References
https://dspy-docs.vercel.app/docs/intro
https://github.com/stanfordnlp/dspy
https://arxiv.org/pdf/2310.03714
https://python.langchain.com/v0.1/docs/get_started/introduction/
https://docs.llamaindex.ai/en/stable/
https://pytorch.org/docs/stable/index.html
© C5i 10
About Us
C5i is a pure-play AI & Analytics provider that combines the
power of human perspective with AI technology to deliver
trustworthy intelligence. The company drives value through a
comprehensive solution set, integrating multifunctional teams
that have technical and business domain expertise with a robust suite of
products, solutions, and accelerators tailored for various horizontal and
industry-specific use cases. At the core, C5i’s focus is to deliver business
impact at speed and scale by driving adoption of AI-assisted decision-making.
C5i caters to some of the world’s largest enterprises, including many Fortune
500 companies. The company’s clients span Technology, Media, and Telecom
(TMT), Pharma & Lifesciences, CPG, Retail, Banking, and other sectors. C5i has
been recognized by leading industry analysts like Gartner and Forrester for its
Analytics and AI capabilities and proprietary AI-based platforms.
www.c5i.ai
© C5i