Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Serj Smorodinsky, Brett Kennedy

Rating No ratings yet

DSPy (Declarative Self-improving Python), an innovative framework for prompt programming, replaces fragile and unpredictable manual prompts with clean, modular Python code that declares what a model should do, defines the inputs and response formats, and establishes clear evaluation metrics. In this book authors Serj Smorodinsky and Brett Kennedy present a systematic approach for treating prompts as programmatic artifacts rather than loose text strings, so you can build, scale, maintain, and improve complex AI applications with the structural integrity of traditional software. Practical from page one, this book mirrors a professional AI engineering workflow, taking you from simple classifiers to complex summarizers, advanced RAG, and agentic systems. Reviewer Cyrus Nouroozi, a DSPy contributor, notes that the book “frames the whole subject around a single coherent thesis: prompt programming is a data-driven discipline analogous to machine learning.” To support its strong focus on prompt programming as an engineering process, the book introduces the baseline → evaluate → optimize loop that eliminates the guesswork of traditional prompt design. Production applications require verifiable, consistent metrics, which are difficult to establish with conventional prompts. This book guides you through constructing custom evaluation metrics, setting up multi-threaded test runners, and calibrating LLM-as-a-judge protocols to gather hard data. Instead of deploying prompts on a whim, you compile and optimize them against structured validation sets. The resulting programs are mathematically proven to be more accurate, more cost-effective, and remarkably resilient to underlying model drift. The final chapters show you how to build DSPy into fully agentic pipelines, integrating conversation memories, and connecting agents to external environments via MCP. This focused 9-chapter book also introduces cutting-edge DSPy v3 features like SIMBA and GEPA optimizers to squeeze …

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical guide for AI engineers and data scientists who want to stop hand-tuning prompts and start building LLM features like real software—declaring tasks in Python, measuring them, and letting DSPy optimize them. If you ship LLM-powered features and care about reliability, cost, and maintainability, this book is aimed at you. 【Book Arc】 - **Opening (~0%–10%)**: Frames the core thesis—prompt programming as a data-driven discipline—and contrasts DSPy with manual prompt engineering and LangChain-style tooling. Introduces signatures, modules, and the baseline → evaluate → optimize loop. - **Early (~10%–30%)**: Covers the basic machinery: prompt components, your first DSPy application, signatures as input/output contracts, the LM abstraction (including LiteLLM for provider-agnostic access), and Prediction objects. - **Middle (~30%–55%)**: Walks the three-stage methodology on real projects, starting with intent classification on the ATIS airline customer-service dataset—EDA, baseline classifier, and the pain of maintaining hand-built prompt strings across multiple LMs. - **Late (~55%–85%)**: Moves into evaluation and optimization: custom metrics (precision, recall, F1), multi-threaded test runners, LLM-as-a-judge calibration, and compiling prompts against validation sets. Introduces DSPy v3 optimizers like SIMBA and GEPA. - **Ending (~85%–100%)**: Builds toward agentic systems—conversation memory, connecting agents to external environments via MCP, and managing compounding errors in multi-call pipelines. 【Key Takeaways】 - **Prompts are programmatic artifacts, not text strings** (Opening): DSPy signatures declare inputs, outputs, and intent in Python, making prompts testable, reviewable, and swappable—closer to scikit-learn or PyTorch than to prompt tinkering. - **The baseline → evaluate → optimize loop replaces guesswork** (Early): Start with a simple zero-shot baseline, measure it on held-out data, then optimize against a validation set. This mirrors standard ML practice and is the book's organizing spine. - **Evaluation is the hard part, and the book takes it seriously** (Middle): Custom metrics, multi-threaded test runners, and calibrated LLM-as-a-judge protocols give you hard numbers instead of vibes—essential for production. - **Manual prompt maintenance scales badly** (Middle): The ATIS classifier example shows how per-LM prompt concatenation logic balloons into hundreds of lines that are hard to read, debug, or change safely. - **Optimization beats hand-tuning on accuracy, cost, and drift resilience** (Late): Compiled prompts are claimed to be more accurate, cheaper, and more robust to underlying model changes—though the excerpts do not detail the specific benchmarks. - **Modularity enables swapping LMs, modules, and optimizers** (Early): Because DSPy unifies LM access (via LiteLLM) and separates signatures from prompting techniques, you can iterate over options the way you'd tune hyperparameters. - **Agents amplify both leverage and risk** (Late): Multi-call pipelines compound errors, and agents that touch the world can make purchases or delete data—so rigorous evaluation matters even more. - **DSPy isn't always the right tool** (Early): For casual, one-off, low-stakes LM calls, direct prompting is often good enough; DSPy pays off on complex, long-running, or high-stakes applications. 【Reading Tips】 - **Deep-read Chapters 1–3** for the mental model: signatures, modules, and the baseline → evaluate → optimize loop. Skim the ATIS EDA if you already know pandas. - **Treat the evaluation chapters as the core value**: if you only implement one thing from this book, build a real metric and a validation set before optimizing anything. - **Run the code as you go**: the book is explicitly hands-on, and the DSPy API surface (signatures, Predict, ChainOfThought, optimizers) only clicks once you've configured an LM and seen a Prediction object. - **Watch for version drift**: DSPy v3 features like SIMBA and GEPA are recent; check current docs when APIs differ from the text. - **Don't skip the "when not to use DSPy" discussion**—it will save you from over-engineering simple features. 【Coverage Limits】 This guide is synthesized from stratified excerpts covering roughly the first half of the book plus chapter summaries and blurb material; the later chapters on advanced RAG, agentic pipelines, MCP integration, and specific optimizer internals are referenced but not detailed in the excerpts. Specific benchmark numbers and full code listings are not reproduced here.
Excerpt 1
ctive, and remarkably resilient to underlying model drift. The final chapters show you how to build DSPy into fully agentic pipelines, integrating conversati...
View in text
Excerpt 2
ing some code. For casual use, where high-quality responses aren’t essential, directly prompting the LM with manually created prompts is often good enough, e...
View in text
Excerpt 3
lation, advice, problem solving, writ- ing code, and so on). Although the range of such tasks is very large, most can be described fairly succinctly with con...
View in text
Excerpt 4
ay, we can define this for any number of other LMs we test. We may also have logic to compose the system prompts differently depending on the budget, the imp...
View in text
Excerpt 5
four sets: training, validation, development, and testing. This isn’t as common as using three sets, but it works well when there is sufficient data. The ter...
View in text
Excerpt 6
8, about the same as with the DSPy-generated prompt in this case (it’s slightly lower, but probably not significantly, given the small size of the test set)....
View in text
Excerpt 7
have an unoptimized DSPy program called intent_ classifier. This program is what we try to optimize with different optimizers in this chapter and the next. T...
View in text
Excerpt 8
ll. Listing 5.10 Specifying a teacher LM for bootstrapping gpt4 = dspy.LM(model='gpt-4') Specifies an LM to use later as a teacher LM optimizer = BootstrapFe...
View in text
Tags
AI categories
Artificial IntelligencePythonProgramming
ISBN: 1633435016
Publish Year: 2026
Language: English
Pages: 296
File Format: PDF
File Size: 5.4 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…