Prompt Templates
A prompt template is a message with blanks in it.
You write the message once. Later you fill in the blanks with real values.
It is like a form letter. Dear ____, thank you for ____. Fill the blanks and send.
Your first template
ChatPromptTemplate.from_messages takes a list of messages. Curly braces mark the blanks.
{topic} is the blank. invoke takes a dict and fills every blank with the matching value.
The result is a list of real messages, ready to send to a model.
Template plus model
Fill the template, then hand the messages to the model. Two steps.
Change the dict and you get a lesson on a new topic. The template never changes.
In two lessons you will glue these steps together with one symbol.
Many blanks
A template can have as many blanks as you like. Just give every one a value.
If you forget a key, LangChain raises a KeyError right away. That is helpful. It fails before you spend money.
Plain string templates
Sometimes you want one string, not a list of chat messages. Use PromptTemplate.from_template.
Chat models accept this too. But for chat models, ChatPromptTemplate is the normal choice.
Leaving room for history
MessagesPlaceholder is a blank that takes a whole list of messages instead of one word.
The old messages slide into the middle. The new question goes at the end.
This is how chat memory will work in lesson 11.
Partial values
Sometimes one blank has the same value almost every time. Fill it early with partial.
spanish is a new template with only one blank left. The original is untouched.
Why bother with templates
The prompt lives in one place. Fix a typo once, not in ten files.
Blanks are checked. Missing a value fails early and loudly.
Templates plug straight into chains, which you will meet soon.
You can swap the model without touching the prompt.
Remember: curly braces make a blank. invoke with a dict fills the blanks and gives you real messages.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.