Conditions
Creating Conditional Paths and Conditions
In agent design, we control every action and decision made during the interaction. To add flexibility and logic to conversations, we use Conditions, which guide the conversation's flow based on specific criteria.
Conditions are "if-then" logic functions, similar to programming. They help alter the path of a conversation based on user input or specific variables.
Example: Conditions are a part of our daily decision-making. For example, "If it is raining, we will stay inside." or "If it’s my birthday, I’ll have cake." The same logic applies to conversation flows: “If this happens, then do that.”
What is the Condition Step?
The Condition Step lets you define different paths based on whether a condition is met. For example, you might check if a user's score is above a certain threshold and branch the conversation accordingly.
Configuring Conditions
There are two main ways to create conditions: Condition Builder and Expression.
1. Using the Condition Builder
The Condition Builder is designed for non-technical users who need to set up simple conditions without writing code.
- Match Options: Choose between Match all (requires all conditions to be true) or Match any (requires at least one condition to be true).
- Set Conditions: Define the conditions using variables and operators, such as is, is not, greater than, etc.
- Input Values: You can directly input values or use existing project variables. The default data type for values is
string
, but if you are comparinginteger
values or other types, you will need to use the Expression option for more flexibility.
Tip: To add multiple assertions for a single condition, click the + button next to the assertion.
2. Using Expression
For more complex logic, you can use the Expression option, which allows you to write custom JavaScript code. This is useful when you need conditions that go beyond basic comparisons.
- Code Editor: The Expression option opens a JavaScript code editor where you can write custom logic to define your condition.
- Returning True/False: The condition will be evaluated as
true
orfalse
, and the corresponding path will be activated if the condition is true. You do not need to include thereturn
statement in your code.
Example: Basic Condition
Check if the user’s session count equals 5
:
sessions == 5
If true, the conversation will follow the path assigned to this condition.
Example: Complex Condition
Check if the user is 18 years or older and is subscribed:
age >= 18 && isSubscribed === true
Both conditions must be true for this path to activate.
Evaluating Conditions
Conditions compare a selected variable’s value against a defined target. If the criteria are met, the corresponding path is activated.
Example: Compare a variable
{score}
to the value5
. If{score}
equals5
, the conversation will follow the corresponding path.Note: Conditions require exact matches. For example, if you compare
{animal}
to"dog"
, and the value stored in the variable is"Dog"
, the condition will not be met because it’s case-sensitive.
Important: Conditions require an exact match. For example, if
{animal}
is compared to"dog"
but the value stored is"Dog"
, the condition will not be met, as it’s case-sensitive.
The Else Path
The Else Path is triggered when none of the specified conditions are met. You can configure what happens in this scenario, such as guiding the conversation to a different path or ending it.
Tip: Use this as a fallback option to ensure the conversation doesn’t end abruptly.
Updated about 1 month ago