How to Format Prompt with Variables in Langchain
In Langchain, use
PromptTemplate to format prompts with variables by defining placeholders in the template string and passing keyword arguments of variable values when calling format. This allows dynamic insertion of values into prompts before sending them to language models.Syntax
The PromptTemplate class lets you create prompt strings with placeholders for variables. You define the template with curly braces {variable_name} where you want to insert values. Then, use the format method with keyword arguments to replace placeholders with actual values.
Key parts:
input_variables: List of variable names used in the template.template: The prompt string with placeholders.format(): Method to fill in variables with values.
python
from langchain import PromptTemplate prompt = PromptTemplate( input_variables=["name", "age"], template="My name is {name} and I am {age} years old." ) formatted_prompt = prompt.format(name="Alice", age=30) print(formatted_prompt)
Output
My name is Alice and I am 30 years old.
Example
This example shows how to create a prompt template with variables and format it dynamically. It demonstrates inserting a user's name and favorite color into a prompt.
python
from langchain import PromptTemplate # Define the prompt template with variables template = "Hello {user_name}, your favorite color is {color}." prompt = PromptTemplate(input_variables=["user_name", "color"], template=template) # Format the prompt with actual values result = prompt.format(user_name="Bob", color="blue") print(result)
Output
Hello Bob, your favorite color is blue.
Common Pitfalls
Common mistakes when formatting prompts with variables in Langchain include:
- Not listing all variable names in
input_variables, causing errors. - Using variable names in the template that don't match those in
format(). - Passing variables to
format()that are not declared ininput_variables. - Forgetting to call
format()and trying to use the template string directly.
python
from langchain import PromptTemplate # Wrong: missing 'color' in input_variables # prompt = PromptTemplate(input_variables=["user_name"], template="Hi {user_name}, your color is {color}.") # Correct: prompt = PromptTemplate(input_variables=["user_name", "color"], template="Hi {user_name}, your color is {color}.") # Wrong: variable name mismatch # prompt.format(user="Alice", color="red") # 'user' should be 'user_name' # Correct: formatted = prompt.format(user_name="Alice", color="red") print(formatted)
Output
Hi Alice, your color is red.
Quick Reference
| Concept | Description |
|---|---|
| PromptTemplate | Class to create prompt templates with variables. |
| input_variables | List of variable names used in the template string. |
| template | String with placeholders like {variable_name}. |
| format() | Method to replace placeholders with actual values. |
| Variable names | Must match exactly between template, input_variables, and format() call. |
Key Takeaways
Use PromptTemplate with input_variables and template to define prompts with placeholders.
Call format() with matching variable names to insert values into the prompt.
Ensure variable names are consistent in the template, input_variables, and format() arguments.
Avoid missing variables in input_variables to prevent errors.
PromptTemplate helps create dynamic, reusable prompts for language models.