A first DSPy program with Predict
A DSPy program is a signature, like question -> answer, wrapped in a module such as Predict. Calling it returns the output fields as attributes.
import dspy
lm = dspy.LM("openai/gpt-4o-mini", mock_response="[[ ## answer ## ]]\nParis\n\n[[ ## completed ## ]]")
dspy.configure(lm=lm)
qa = dspy.Predict("question -> answer")
result = qa(question="What is the capital of France?")
print(result.answer)dspy.LM names a model as provider and model id, openai/gpt-4o-mini. dspy.configure(lm=lm) makes it the model every module uses. dspy.Predict("question -> answer") builds a module from a signature: one input field, question, and one output field, answer. Calling it with question=... returns a Prediction, and result.answer is the answer.
Where Paris came from
No request went to OpenAI. dspy.LM sends requests through LiteLLM, and mock_response is LiteLLM's option for tests: it returns that text instead of calling the provider. The text is written in the format DSPy asks models to answer in, which lesson 3 opens up. Ask a different question and you still get Paris.
print(qa(question="What is the capital of Japan?").answer)
print(result)A Prediction prints like a dataclass. It holds every output field of the signature.
Without the mock
lm = dspy.LM("openai/gpt-4o-mini")
try:
print(lm("Say hello"))
except Exception as error:
print(type(error).__name__)
print(str(error).split(" - ")[-1])Called directly, an LM takes a prompt and returns a list of answers. Without OPENAI_API_KEY, the OpenAI client refuses before any request is sent. DSPy reports it as LMServerError, but the message states the real cause. With the key exported, the same line calls OpenAI and costs money. The rest of the course never needs it.
- Change the signature to
"question -> answer, confidence"and print the result. What does the mock return forconfidence? - Pass
questionpositionally,qa("What is DSPy?"), and read the error. - Print
qa.signature.
You understood something today that you didn't yesterday.