Introduction
Templates help you write prompts once and use them many times. This saves time and keeps your prompts clear and organized.
Jump into concepts and practice - no test required
Templates help you write prompts once and use them many times. This saves time and keeps your prompts clear and organized.
from langchain.prompts import PromptTemplate template = PromptTemplate( input_variables=["name"], template="Hello, {name}! How can I help you today?" ) prompt_text = template.format(name="Alice")
input_variables are placeholders you fill later.
Use {variable_name} inside the template string to mark where values go.
template = PromptTemplate(
input_variables=["city"],
template="Tell me about the weather in {city}."
)
prompt_text = template.format(city="Paris")template = PromptTemplate(
input_variables=["product", "price"],
template="The {product} costs {price} dollars."
)
prompt_text = template.format(product="book", price="15")This program shows how one template can greet different users by changing the name each time.
from langchain.prompts import PromptTemplate # Create a template with one variable template = PromptTemplate( input_variables=["user_name"], template="Welcome, {user_name}! Ready to start your journey?" ) # Use the template with different names print(template.format(user_name="Sam")) print(template.format(user_name="Lily"))
Templates keep your prompts neat and easy to update.
Always name your variables clearly to avoid confusion.
You can reuse one template many times with different inputs.
Templates let you write prompts once and reuse them easily.
They use placeholders to insert different information each time.
This helps save time and keeps your prompts consistent.
name in Langchain?from langchain import PromptTemplate
template = PromptTemplate(template="Hello, {name}!")
output = template.format(name="Alice")
print(output)from langchain import PromptTemplate
template = PromptTemplate(template="Welcome, {user}!")
output = template.format(username="Bob")
print(output)