Prompt conditions

You may use handlebars-like branching logic in prompt:

{{#if variable == "value1"}}
prompt-element-1
{{#elseif variable == "value2"}}
prompt-element-2
{{#else}}
prompt-element-3
{{/if}}

The prompt expansion happens BEFORE the prompt is sent to LLM. Therefore, the LLM doesn’t see any branching logic, but only the remaining prompt elements.

Conditions syntax

Conditions syntax is very flexible and supports the following elements:

Comparison operators

You may use the following comparison operators: ==, !=, <, >, <=, >= .

For variables that contain string values, enclose the values that you compare with in quotes. For example:

{{#if sex == "male"}}

You may also test for empty string by simply specifying the variable name:

{{#if customer_name}}

Variables are automatically assigned one of the following four types – string, integer, float, boolean – based on the provided value. This allows you to perform numeric comparison, for example:

{{#if age > 18}}

Make sure that variable type always matches the variable type.

Type casting

You may explicitly cast variables to specific type using str(), int(), float(), or bool() methods.

For example:

{{#if str(age) == "18"}}

Logical operators

You may use and, or, not logical operators in your conditions.

For example:

{{#if user_utterance == "male" and age > 18}}

Brackets

You may use brackets to group logical condition or specify multi-line conditions.

For example:

{{#if (person_name == "John" or
           person_name == "Jack" or
           person_name == "Jonathan")}}
Say "Hello, Mr. J!"
{{/if}}

String operators

You may use the following operators on variables of type string: .lower(), .upper(), .strip(), .lstrip(), .rstrip(), .startswith(prefix), .endswith(prefix)

For example:

{{#if person_name.startswith("J")}} Say "Hello, Mr. J" {{/if}}

You may also use in and not in operators to perform partial string comparison.

For example:

{{#if "please leave a message" in user_utterance}} End the call {{/if}}

Array operators

You may use in and not in operators to compare values against arrays. The latter should be specified as comma-delimited list enclosed in brackets.

For example:

{{#if person_name in ("John", "Jack", "Jonathan")}} Say "Hello, Mr. J" {{/if}}

Nesting

You may use nested #if / #elseif constructs to create complex conditions.

Prompt expansion process

It is important to understand that prompt expansion and conditions evaluation happen before the prompt is sent to the LLM. The following examples illustrate this concept.

Example 1

For prompt:

{{#if role == "admin"}}
User has full access to the system.
{{#elseif role == "user"}}
User has limited access to the system.
{{#else}}
User is not allowed to access to the system.
{{/if}}

Assuming that the role variable is set to admin, LLM will receive the following:
User has full access to the system.

Example 2

For prompt:

{{#if user_age > 18 and subscription_status == "active"}}
User has access to premium content
{{#else}
User doesn't have access to premium content.
{{/if}}

Assuming that the user_age variable is set 16, LLM will receive the following:
User doesn't have access to premium content.

Example 3

For prompt:

Hello {{#if gender == "male"}}Mr{{#else}}Mrs{{/if}} Smith!

Assuming that the gender variable is set male, LLM will receive the following:
Hello Mr Smith!

Use of time variables in conditions

If you want to use time variables in #if / #elseif conditions, you must place quotes around the curly brackets.

For example:

Say good {{#if "{current_time}" < "10:00"}} morning {{#else}} day {{/if}}

Rationale for this special syntax is that time variables expansion happens first and only after it is done, the conditions are evaluated. Therefore, by the time condition evaluation logic runs, {current_time} is already replaced with corresponding value, for example, 09:43:00, which needs to be enclosed in double quotes for valid condition statement.