Prompt expansion
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.
Expansion process
It is important to understand that expansion happens 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 use the following special syntax: "{current_time}"
. For example, enclose them in curly brackets and quotes. Time variables get expanded before the rest of the prompt is evaluated, and this special syntax ensures that they are treated as regular strings during conditions evaluation.
For example:
Say good {{#if "{current_time}" < "10:00"}} morning {{#else}} day {{/if}}
Dynamic prompts
By default, prompt expansion happens at the beginning of the conversation, as part of agent’s initialization.
Alternatively, you can use dynamic_prompt
advanced configuration variable to re-generate prompt for specific agent dynamically, on each user utterance.
When dynamic prompts are enabled, the following additional variables are provided by the Live Hub platform:
Variable |
Type |
Description |
---|---|---|
|
string |
Last user utterance |
|
integer |
Starts with zero and is incremented with every user utterance |
|
integer |
Similar to user_utterance_count, but counts only real message said by user, and ignores, for example, NO-USER-INPUT utterances. |
|
integer |
Duration of the call in seconds |
The following example demonstrates the use of dynamic prompts in an outbound dialer agent to ensure that the call is answered by a real human, and not IVR’s or answering machine.
{{#if ("your call is important" in user_utterance or
"stay on the line" in user_utterance or
"wait while we connect" in user_utterance or
"NO-USER-INPUT" in user_utterance)}}
{{#if duration < 40}}
Say "Waiting for human to join"
{{#else}}
End the call
{{/if}}
{{#else}}
Introduce yourself.
Inform user that his car was evacuated.
...
{{/if}}