Generative AIby Dhruv Narang

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)
Python fills {state}"What is the capital of Punjab?"↓ the model answersChandigarh

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.

app.pyf"Answer in one word. Capital of {state}?"
bot.pyf"Answer in one word. Capital of {state}?"
api.pyf"Answer in one word. Capital of {state}?"
quiz.pyf"Answer in one word. Capital of {state}?"
Boss: "Make the answer two lines, not one word."You must now edit every file. Miss one, and your app answers two different ways.

3. The Fix: A Prompt Template

Definition

A 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.

The template: written once
What is the capital of {state}?
fill the blank→
{"state": "Rajasthan"}What is the capital of Rajasthan?
{"state": "Punjab"}What is the capital of Punjab?
{"state": "Gujarat"}What is the capital of Gujarat?

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"})
prompt (what the template gives back)
StringPromptValue(text='What is the capital of Rajasthan?')ChatPromptValue(messages=[HumanMessage('What is the capital of Rajasthan?')])ChatPromptValue(messages=[SystemMessage('Answer in one word.'), HumanMessage('What is the capital of Rajasthan?')])
One plain text prompt.One Human message.A System message and a Human message. This is the one we use most.

5. Template vs f-String

Both build a prompt. A template does more for you.

f-string
  • 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
→
Prompt template
  • 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.

The template, with four blanks
systemYou are an expert {profession} with a {tone} tone. Answer strictly in 2 lines.
humanExplain the concept of {concept} to a {audience_level} student.
template.input_variables → ['audience_level', 'concept', 'profession', 'tone']
fill all blanks with one dictionary↓{"profession": "Chef", "tone": "funny", "concept": "gravity", "audience_level": "5-year-old"}
The filled prompt
systemYou are an expert Chef with a funny tone. Answer strictly in 2 lines.
humanExplain the concept of gravity to a 5-year-old student.
LangChain docsChatPromptTemplate

7. HumanMessage or a Human Tuple?

A tuple fills the blanks in your template. A HumanMessage does not.

What you writeWhat 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.
A HumanMessage is a finished message. Its text is sent as it is. It has no blanks.
A tuple is a template message. LangChain fills its blanks when you call .invoke({...}).
In a template with blanks, write the human message as a tuple.
The chatbot loop uses HumanMessage(content=user_input). That works because Python has already put the typed text in user_input.
The long form of the tuple is HumanMessagePromptTemplate. It does the same job.
Code
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)