
Make Programs, not Prompts: DSPy Pipelines with Llama 3 on Amazon SageMaker JumpStart
Learn how to create ML pipelines with DSPy powered by Meta's Llama 3 70B Instruct model running on Amazon SageMaker.
"If a LLM is like a database of millions of vector programs, then a prompt is like a search query in that database." ― François Chollet, How I think about LLM prompt engineering
DSPy
is just a fanciful backronym for Declarative Self-improving Language Programs, pythonically, which is now my 3rd favorite backcronym after COLBERT and SPECTRE. And yes, I keep a list...🤨 If you're wondering about the overabundance of tags<|start_header_id|>
or<|eot_id|>
, just go over the Model Cards & Prompt Formats for Meta Llama 3.
question -> answer
. Easy-peasy! "What if I want to add a bit of context?" you may ask. No worries, just extend it to context, question -> answer
.💡 Don't know where to start? Check out the Using DSPy in 8 Steps chapter (if you're an experience ML practitioner, they'll sound familiar).
🐛 “Every great s̶t̶o̶r̶y̶ demo seems to begin with a s̶n̶a̶k̶e̶ bug.” ― Nicolas Cage
aws
, bedrock
or sagemaker
, and 3a/ if there's a funny-looking issue, try to fix it; 3b/ otherwise, just try one of the demos (PS: I used to work as software tester and bugs just seem to find me wherever I go). SagemakerRuntime
and BedrockRuntime
), but the way we engage with the models is a bit different:- API: Bedrock has
InvokeModel
/InvokeModelWithResponseStream
and the newConverse
API, while SageMaker hasInvokeEndpoint
/InvokeEndpointWithResponseStream
and the asynchronousInvokeEndpointAsync
. Behind the scenes, Bedrock is powered by SageMaker endpoints, so the naming shouldn't come as a surprise. - Requests: as we will see later, each accepts a different payload, both in terms of inference parameters, their names and how they're structured, and produces a different response (although, if we correctly map the parameters and fix the model version, the
generated_text
should be the same).
AWSMeta
, SageMaker support is completely absent.🎉 Update (08-07-2024): The proposed fix has since been merged into themain
line branch.
SageMaker
provider💡 This example comes from the original paper by Khattab et al. (2023). If you need a refresher, the Prompt Engineering Guide has a great introduction to CoT prompting.

answer
and the rationale
behind it.- (Khattab et al., 2023) DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines
- (Opsahl-Ong et al., 2024) Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs
- (Zhang et al., 2022) Automatic Chain of Thought Prompting in Large Language Models
- How I think about LLM prompt engineering by François Chollet
- Deploy Llama 3 on Amazon SageMaker by Phil Schmid
- An Introduction To DSPy by Cobus Greyling
- stanfordnlp/DSPy - programming—not prompting—Foundation Models

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.