🧾 Lesson: Using Prompt Templates
Module: Building a Basic AI Agent
🎯 Lesson Objective
By the end of this lesson, you’ll be able to:
-
Understand what a prompt template is in LangChain
-
Create reusable templates to structure AI input
-
Dynamically fill in prompts with user queries
-
Use templates to guide the model’s behavior more precisely
🧠 What Is a Prompt Template?
A prompt template is a structured input string that:
-
Controls how you speak to the LLM
-
Makes AI outputs more reliable, focused, and customizable
-
Allows for variables (e.g.
{question}or{context}) that are filled dynamically at runtime
Think of it as a “smart sentence with blanks” that you fill in depending on what you’re asking.
📦 Step-by-Step: Using Prompt Templates in LangChain
✅ 1. Import PromptTemplate
from langchain.prompts import PromptTemplate
✅ 2. Define a Template
Here’s a simple one for question answering:
template = """
You are a helpful and intelligent assistant.
Answer the question below clearly and concisely.
Question: {question}
Answer:
"""
prompt = PromptTemplate(
input_variables=["question"],
template=template
)
You’ve now created a reusable prompt that accepts a single variable: question.
✅ 3. Format the Prompt
Use prompt.format() to fill in the variable:
formatted_prompt = prompt.format(question="What is quantum computing?")
print(formatted_prompt)
🖨️ Output:
You are a helpful and intelligent assistant.
Answer the question below clearly and concisely.
Question: What is quantum computing?
Answer:
✅ 4. Use with Your Model
Pass the formatted prompt to your LLM:
from langchain.llms import Ollama
llm = Ollama(model="mistral", temperature=0.5)
response = llm(formatted_prompt)
print("🤖", response)
🧠 Why Use Prompt Templates?
| Feature | Benefit |
|---|---|
| 🧩 Structure | Ensure input follows a logical format |
| 🧠 Role assignment | Guide the model (e.g. “You’re a tutor”) |
| 🔁 Reusability | Easy to reuse with different questions |
| 🎯 Control | Reduces randomness and vague answers |
💡 Examples of Useful Templates
📘 Example 1: Tutor Bot
template = """
You are an English teacher. Explain the following concept clearly for a beginner student.
Concept: {concept}
Explanation:
"""
🧮 Example 2: Math Solver
template = """
You are a smart math tutor. Show the steps clearly and explain the answer.
Problem: {problem}
Solution:
"""
💬 Example 3: Role-based Persona
template = """
You are an AI assistant that answers like a pirate.
Question: {question}
Answer like a pirate:
"""
🧠 Advanced: Chain Templates with Chains
PromptTemplates are usually used inside LLMChain:
from langchain.chains import LLMChain
qa_chain = LLMChain(
llm=llm,
prompt=prompt
)
response = qa_chain.run("What is the difference between RAM and ROM?")
print(response)
✅ Summary
| Step | What You Did |
|---|---|
| 🧾 Created a prompt template | With variables like {question} |
🧠 Used .format() to insert user input |
Dynamically filled templates |
| 🧠 Sent the prompt to the LLM | To get structured, context-aware responses |
🔁 Combined with LLMChain |
For reusability and modular AI behavior |
47
