1. Your Prompt Works
You are building a state capital app. You write the question with an f-string, and it works.
state = "Punjab" response = model.invoke(f"What is the capital of {state}?") print(response.content)
2. Then Your Prompts Multiply
The app grows. The same prompt, with the same rules, is now copied into many files. Then the rules change.
f"Answer in one word. Capital of {state}?"f"Answer in one word. Capital of {state}?"f"Answer in one word. Capital of {state}?"f"Answer in one word. Capital of {state}?"3. The Fix: A Prompt Template
DefinitionA prompt template is a prompt written once, with blanks (placeholders) that you fill in later.
Like a blank form, you print it once and fill it in for every person. In LangChain you fill the blanks with .invoke() and a dictionary.
4. Three Kinds of Templates
LangChain has three ways to write a template. The first two give you one plain prompt. The third gives you a system message and a human message. Pick a tab to see the code and what comes back.
from langchain_core.prompts import PromptTemplate template = PromptTemplate.from_template("What is the capital of {state}?") prompt = template.invoke({"state": "Rajasthan"})from langchain_core.prompts import ChatPromptTemplate template = ChatPromptTemplate.from_template("What is the capital of {state}?") prompt = template.invoke({"state": "Rajasthan"})from langchain_core.prompts import ChatPromptTemplate template = ChatPromptTemplate.from_messages([ ("system", "Answer in one word."), ("human", "What is the capital of {state}?"), ]) prompt = template.invoke({"state": "Rajasthan"})
5. Template vs f-String
Both build a prompt. A template does more for you.
- Filled: right now, once
- Variables: Python does not track them
- Roles: one plain string, or you build the messages by hand
- Reuse: copy and paste it into every file
- Filled: later, as many times as you want
- Variables: it knows them,
['state'], and tells you if one is missing - Roles: system and human messages are built in
- Reuse: write it once and import it everywhere
6. More Than One Blank
A template can have many blanks, in the system message and in the human message. You fill them all with one dictionary. The template also knows which variables it needs: input_variables.
{"profession": "Chef", "tone": "funny", "concept": "gravity", "audience_level": "5-year-old"}7. HumanMessage or a Human Tuple?
A tuple fills the blanks in your template. A HumanMessage does not.
| What you write | What the model gets |
|---|---|
("human", "What is the capital of {state}?") | What is the capital of Punjab? The blank is filled. |
HumanMessage(content="What is the capital of {state}?") | What is the capital of {state}? The blank is not filled. |
HumanMessage(content=f"What is the capital of {state}?") | What is the capital of Punjab? Python filled it, before the message was made. |
.invoke({...}).HumanMessage(content=user_input). That works because Python has already put the typed text in user_input.HumanMessagePromptTemplate. It does the same job.Five ways to send a promptprompts.py · 46 linesShow code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | from dotenv import load_dotenv from langchain.chat_models import init_chat_model from langchain_core.messages import SystemMessage, HumanMessage from langchain_core.prompts import PromptTemplate, ChatPromptTemplate load_dotenv() model = init_chat_model("mistralai:open-mistral-nemo", temperature=0) state = input("Enter the state name: ") # 1. Plain f-string (long answer) res1 = model.invoke(f"What is the capital of {state}?") # 2. Messages: system = rules, human = question messages = [ SystemMessage("Answer in one word."), HumanMessage(f"What is the capital of {state}?"), ] res2 = model.invoke(messages) # 3. PromptTemplate: one plain text prompt template3 = PromptTemplate.from_template("What is the capital of {state}?") prompt3 = template3.invoke({"state": state}) res3 = model.invoke(prompt3) # 4. ChatPromptTemplate.from_template: one human message template4 = ChatPromptTemplate.from_template("What is the capital of {state}?") prompt4 = template4.invoke({"state": state}) res4 = model.invoke(prompt4) # 5. ChatPromptTemplate.from_messages: system + human messages template5 = ChatPromptTemplate.from_messages([ ("system", "Answer in one word."), ("human", "What is the capital of {state}?"), ]) prompt5 = template5.invoke({"state": state}) res5 = model.invoke(prompt5) print(res1.content) print(res2.content) print(res3.content) print(res4.content) print(res5.content) |
Template with many blanks07_multiple_variables_prompt.py · 31 linesShow code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from dotenv import load_dotenv load_dotenv() from langchain_core.prompts import ChatPromptTemplate from langchain.chat_models import init_chat_model from rich import print model = init_chat_model(model="mistralai:mistral-small-2603") # 2. Multi-variable Chat Template (Multiple {variables} in both System & Human) multi_template = ChatPromptTemplate.from_messages([ ("system", "You are an expert {profession} with a {tone} tone. Answer strictly in 2 lines."), ("human", "Explain the concept of {concept} to a {audience_level} student.") ]) profession = input("Enter profession (e.g. Physicist / Chef / Doctor): ") tone = input("Enter tone (e.g. funny / serious / sarcastic): ") concept = input("Enter concept (e.g. Gravity / Photosynthesis): ") audience_level = input("Enter audience level (e.g. 5-year-old / college): ") # 4. Invoke template with all 4 variables in a single dictionary prompt_val = multi_template.invoke({ "profession": profession, "tone": tone, "concept": concept, "audience_level": audience_level }) response = model.invoke(prompt_val) print(response.content) |