Welcome to Day 3! Yesterday, we built our first chain. It worked, but we did something that professional AI engineers try to avoid: we used a simple string for our prompt.
In a real-world app, prompts need to be dynamic, reusable, and structured. Today, weβre diving into Prompt Templatesβthe secret to making your AI reliable and scalable.
π The Problem: Hardcoded Strings
Imagine youβre building a travel assistant. If you write your prompt like this:
"Tell me a 3-day itinerary for Paris"
...your code is stuck. Every time the city changes, you have to rewrite the code.
Prompt Templates solve this by using placeholders (like {city}), turning your instructions into a reusable function.
ποΈ The Two Main Types of Templates
The official documentation breaks prompts into two categories based on the model you're using.
1. PromptTemplate (Standard)
Best for "Completion" models that just want a single block of text.
from langchain_core.prompts import PromptTemplate
template = PromptTemplate.from_template("You are a helpful assistant. Explain {topic} to a 5-year-old.")
# We can reuse this for 'Space', 'Economics', or 'Cooking'!
2. ChatPromptTemplate (The Gold Standard)
Most modern models (GPT-4o, Claude, Gemini) are Chat Models. They don't just want text; they want a conversation history with specific Roles.
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages([
("system", "You are a professional {industry} consultant."),
("human", "How can I improve my {business_process}?"),
])
# When we run this, LangChain formats it perfectly for the AI
formatted_prompt = chat_template.invoke({
"industry": "Real Estate",
"business_process": "lead generation"
})
π Understanding the "Roles"
Why bother with "System" or "Human" tags?
- System: Sets the "vibe" or rules (e.g., "Don't use emojis," "Answer in bullet points").
- Human: The actual user input.
- AI (or Assistant): You can even provide "example" responses from the AI to guide its behavior (this is called Few-Shot Prompting).
β‘ Pro Tip: Partial Formatting
Sometimes you know some information early, but not all of it. For example, your System Prompt might always include the current date, but the user's question comes later.
# Create a template with a fixed 'name' but a dynamic 'question'
partial_template = chat_template.partial(industry="Tech Operations")
# Now you only need to provide the 'business_process' later!
final_chain = partial_template | model
This keeps your code incredibly clean and modular.
π― Day 3 Summary
Today, we upgraded from "shouting at a bot" to "designing a system." You learned:
Why templates are better than strings.
The difference between PromptTemplate and ChatPromptTemplate.
How to use Roles to control AI personality.
How to Partial a prompt to save time.
Your Homework: Create a ChatPromptTemplate for a "Code Reviewer" agent. Give it a System role that tells it to be "strict but encouraging," and a Human role for the code snippet.
See you tomorrow! β
Top comments (0)