liveBook · Manning

DSPy program

As explained in Building LLM Applications with DSPy

An executable DSPy composition of one or more modules.

A DSPy program is an instantiated module that executes calls to a language model. Its signature defines input and output fields; the module turns that interface into a prompt and returns a Prediction. section 2.9 section 3.3.2

A program is defined by its language model, signature, and module; optimization can be added to improve it. These components can be changed independently, separating concerns. section 2.9 section 4.3

Question answering

A Predict program with the signature "question -> answer" takes a question and returns an answer in a Prediction object. section 2.2 section 2.9

Listing 2.2 Question-answering example
import dspy
OPENAI_API_KEY = . . .
lm = dspy.LM(model='gpt-4o-mini', api_key=OPENAI_API_KEY)
dspy.configure(lm=lm)
prog = dspy.Predict("question -> answer")
prediction = prog(question="What is the capital of France?")
print(prediction)

A minimal Python example configures a language model, creates a Predict program, calls it, and prints its prediction. section 2.2

Programs can also serve as baseline classifiers and be tested against individual examples or datasets. Calls made in loops or across a test set require attention to token use and cost. section 3.3.2 section 4.3

How Building LLM Applications with DSPy covers it

How deep it goes
Defines program components and execution, then evaluates a baseline; defers prompt optimization details.
Examples
Uses question answering and user-intent classification, including testing predictions on dataset examples.
What you can do after
Build and call a program by configuring a language model, specifying a signature, and instantiating a module.
How it connects
Connects modules, signatures, language models, predictions, and prompt optimization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.