ย
๐ Lesson 2.1: Prompt Templates and Chains
๐ฏ Lesson Objectives:
By the end of this lesson, you will:
-
Understand what prompt templates are and why they matter in LLM applications
-
Learn how to create reusable, dynamic prompts using
PromptTemplate -
Build your first LangChain
LLMChainto combine a prompt and an LLM -
See how chains help you build modular and scalable AI workflows
๐ง What Is a Prompt?
A prompt is the message or instruction you give to a language model.
๐ข Example:
"Write a professional email to a customer apologizing for a delayed shipment."
Prompts are the starting point for any LLM outputโbut writing a new prompt every time can be repetitive and error-prone. Thatโs where prompt templates come in.
๐งฉ What Is a Prompt Template?
A PromptTemplate lets you create a reusable prompt with dynamic placeholders.
๐ข Example:
from langchain.prompts import PromptTemplate
template = PromptTemplate.from_template("Write a polite email to a customer about {issue}.")
print(template.format(issue="a delayed order"))
Output:
Write a polite email to a customer about a delayed order.
This allows you to feed new data into the same prompt without rewriting the structure every time.
๐ What Is an LLMChain?
An LLMChain is one of the most important concepts in LangChain.
It connects:
-
A prompt template
-
A language model (LLM) like OpenAI or Ollama
-
And optionally, memory and output parsing
๐ง Think of it as a pipeline that turns inputs into structured outputs.
๐ข Basic Example:
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
prompt = PromptTemplate.from_template("Suggest 3 business names for a company that sells {product}.")
llm = OpenAI() # or Ollama(model="mistral")
chain = LLMChain(prompt=prompt, llm=llm)
print(chain.run(product="organic snacks"))
Output:
1. SnackBloom
2. NatureMunch
3. EcoBites
๐ How Chains Work
Chains are a core feature of LangChain because they let you:
-
Reuse logic
-
Add memory
-
Build multi-step workflows
-
Use inputs and outputs like building blocks
Real Example:
Input โ “Write a social media post about our new app feature”
Prompt Template โ Fills in context about your app
LLMChain โ Produces the finished post
๐ง When Should You Use a Prompt Template and Chain?
Use when:
-
You want to automate text generation with variable inputs
-
You need to reuse a structured prompt (e.g., emails, tweets, product descriptions)
-
You’re building a modular, multi-step AI workflow
-
You’re developing tools or chatbots with consistent language
๐งช Practice Activity:
Try building an LLMChain that answers this:
Prompt Template: “What are 3 advantages of using {technology} in modern business?”
Plug in technologies like:
-
AI agents
-
Blockchain
-
Automation software
Write a short script and test different inputs. Youโll begin to see how powerful and scalable LangChain becomes.
โ Key Takeaways:
-
Prompt templates let you reuse and customize prompts dynamically
-
LLMChains combine a prompt + LLM to build smart workflows
-
You can easily turn any structured task into a modular pipeline using chains
-
Prompt + LLM = Chain โ The foundation for more advanced agents
ย
51
