Variables let you store and reuse information easily. Dynamic content means your program can change what it shows based on those variables.
Variables and dynamic content in LangChain
Start learning this pattern below
Jump into concepts and practice - no test required
from langchain import PromptTemplate # Define a template with variables inside curly braces template = "Hello, {name}! Today is {day}." # Create a PromptTemplate object prompt = PromptTemplate(template=template, input_variables=["name", "day"]) # Fill variables with actual values filled_prompt = prompt.format(name="Alice", day="Monday")
Variables are placed inside curly braces {} in the template string.
You must list all variable names in input_variables when creating the PromptTemplate.
color. It prints a sentence with the color filled in.template = "My favorite color is {color}." prompt = PromptTemplate(template=template, input_variables=["color"]) print(prompt.format(color="blue"))
greeting and name are used to create a friendly message.template = "{greeting}, {name}!" prompt = PromptTemplate(template=template, input_variables=["greeting", "name"]) print(prompt.format(greeting="Hi", name="Bob"))
template = "Today is {day}." prompt = PromptTemplate(template=template, input_variables=["day"]) print(prompt.format(day="Friday"))
This program creates a message template for appointment reminders. It fills in the title, last name, and date dynamically.
from langchain import PromptTemplate # Create a template with variables template = "Dear {title} {last_name}, your appointment is on {date}." prompt = PromptTemplate(template=template, input_variables=["title", "last_name", "date"]) # Fill variables with actual data message = prompt.format(title="Dr.", last_name="Smith", date="June 10th") print(message)
Always list all variables you want to use in the input_variables list.
If you forget to provide a variable value when calling format, it will raise an error.
Using variables makes your prompts flexible and reusable for many situations.
Variables let you insert changing information into text templates.
Use PromptTemplate with input_variables to define which parts change.
Fill variables with format to get the final text with dynamic content.
Practice
PromptTemplate?Solution
Step 1: Understand the role of variables in templates
Variables are placeholders that allow parts of the text to change based on input.Step 2: Connect variables to dynamic content
Using variables lets you insert different values each time you use the template, making it dynamic.Final Answer:
To insert changing information into the text dynamically -> Option BQuick Check:
Variables = dynamic content insertion [OK]
- Thinking variables make text static
- Confusing variables with final output storage
- Believing variables create multiple templates automatically
PromptTemplate with variables in Langchain?Solution
Step 1: Check the syntax for defining variables
Theinput_variablesparameter must be a list of strings naming the variables used in the template.Step 2: Match the template placeholders with variable names
The template uses curly braces around variable names like {name} to mark where to insert values.Final Answer:
PromptTemplate(template="Hello {name}", input_variables=["name"]) -> Option CQuick Check:
Correct syntax = PromptTemplate(template="Hello {name}", input_variables=["name"]) [OK]
- Forgetting quotes around variable names
- Using wrong parameter name like variables instead of input_variables
- Not using curly braces in template
template = PromptTemplate(template="Hello {user}, today is {day}.", input_variables=["user", "day"])
result = template.format(user="Alice", day="Monday")
print(result)What will be printed?
Solution
Step 1: Understand how format fills variables
Theformatmethod replaces {user} with "Alice" and {day} with "Monday".Step 2: Predict the final string after formatting
The placeholders are replaced, so the printed string is "Hello Alice, today is Monday."Final Answer:
Hello Alice, today is Monday. -> Option AQuick Check:
format replaces variables correctly = Hello Alice, today is Monday. [OK]
- Printing template string without formatting
- Confusing variable names with values
- Expecting an error when variables are provided
template = PromptTemplate(template="Hi {name}", input_variables=["name"])
result = template.format(nam="Bob")
print(result)Solution
Step 1: Compare variable names in template and format call
The template expects variable "name" but format uses "nam" which is incorrect.Step 2: Understand the error caused by mismatch
This mismatch causes a KeyError because "name" is not provided in format arguments.Final Answer:
The variable name in format is misspelled, causing a KeyError -> Option DQuick Check:
Variable name mismatch = KeyError [OK]
- Assuming format ignores missing variables
- Thinking input_variables must be a string
- Believing format method is invalid here
PromptTemplate that dynamically inserts a user's name and their favorite color, but if the color is not provided, it should default to "blue". Which approach correctly handles this dynamic content with a default value?Solution
Step 1: Understand how to provide default values
Langchain's PromptTemplate requires all variables listed in input_variables to be provided when formatting.Step 2: Provide default value in code when calling format
By including color in input_variables and passing color="blue" if missing, you ensure the template fills correctly.Final Answer:
Use input_variables=["name", "color"] and call format with color="blue" if missing -> Option AQuick Check:
Default values handled in format call = Use input_variables=["name", "color"] and call format with color="blue" if missing [OK]
- Omitting variables from input_variables to default
- Trying to use Python expressions inside template strings
- Hardcoding default text instead of using variables
