Learn how to define, interpret, and interpolate variables in PromptL
Variables in PromptL allow you to store and reuse dynamic values across your prompt. They make your prompts more maintainable and adaptable by eliminating repetitive text and enabling dynamic content generation.
Variables are defined using double curly braces ({{ }}
) and can be used directly in text or as part of logic expressions.
You can define a variable by assigning a value inside {{ }}
:
In this example:
name
is assigned the value "Alice"
.Variables can be used anywhere in your prompt. When interpolated, their value replaces the placeholder in the text.
Variables don’t have to be defined in the prompt. Instead, their values can be provided as dynamic input parameters when the prompt is executed.
In this example, the name
variable can be defined in the code when running this prompt, allowing you to dynamically customize the output without modifying the prompt.
You can define default values for variables using the ||
operator. If the variable is not defined in the prompt or provided as an input parameter, the default value will be used.
Here, the name
variable will default to "Alice"
if no value is provided.
Tip: Use default values to ensure your prompts remain functional even if some parameters are missing.
PromptL supports logic expressions, enabling you to perform calculations or transformations directly in the prompt.
You can use ternary-like conditions to modify variable values dynamically:
Variables can represent objects or arrays, making it easy to handle structured data:
user.name
, weather.currentTemp
x
, temp
If a variable isn’t working as expected:
Variables in PromptL enable dynamic and reusable prompts. By combining definitions, input parameters, defaults, and expressions, you can create highly flexible and maintainable conversations tailored to any use case.