0
0
LangchainHow-ToBeginner ยท 3 min read

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 in input_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

ConceptDescription
PromptTemplateClass to create prompt templates with variables.
input_variablesList of variable names used in the template string.
templateString with placeholders like {variable_name}.
format()Method to replace placeholders with actual values.
Variable namesMust 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.