Expressions and Variables
Key Concepts In This Chapter: - Variables to store and retain data - Expressions to perform conditions inside variables
Variables: Concepts and Basic Use Cases
In this chapter, we will demonstrate how variables are defined, updated, and passed. As mentioned before, variables can be created within inputs
of a state. For example:
The above simple example creates a variable called intro_message
of type text
with a default value and render it in the message. If user_input
is false
, user will not be prompted to input via a form, and a new variable with the value default_value
will be automatically generated.
Expressions
We can use expressions to take value of previously defined variables and perform basic calculations from it. In Pro Config, expressions are represented in a string wrapped by double curly braces like {{expression}}
. Expressions uses the grammar of JavaScript, and supports most of the basic syntaxes.
Unlike defining a variable through inputs
, the result type of an expression can be dynamically deduced during the execution of that expression. A more common use case is to use expressions inside a string (such as the prompts of LLM):
In this case, Pro Config will replace the expression with the evaluated result of user_message
(which is converted to string) and use that as the user_prompt
.
Scope of Variables
It is also important to understand the scope of variables. Currently, there are two ways to retrieve a variable:
Referring to a variable that's defined within an AtomicState: we can directly use the variable name to refer to that variable (such as the
{{user_message}}
in the user_prompt). Note that the execution of the AtomicState follows the order ofinputs->tasks->outputs->render
Passing a variable across different AtomicStates: we can use the
context
of the Automata to pass the variable. For example:
In the outputs of state1
, we set context.var1
as some_variable
(which should be defined previously in state1
), and we can display that variable in state2
by {{context.var1}}
. Note that the variable to be passed across states need to be declared in the context
of the Automata
Practice Example
In the following example, we will improve the chatbot built in previous chapters in two aspects:
Support customized
intro_message
andtts_widget_id
Implement memory in
LLMModule
to enable multiple rounds of chat.
Here is the config:
In the above example config, we prompt the user to input intro_message
and tts_widget_id
, which are written to context
in the outputs. These two variables are reused later in intro_message_state
and chat_page_state
respectively. Besides, we leverage an array called memory
to store the chat history and update the memory through an expression (for the grammar of this, follow the Javascript syntax guide) :
which will append the latest chat messages to the memory. Then we pass the memory to the memory
parameter of LLMModule
so that the LLM can retain understand how to react based on previous interactions.
Advanced Data Processing
If you want more powerful ability to process data, we recommend using Code Runner Widget to execute code snippets in a free manner.
It requires knowledge about how to use a widget in Pro Config, which is just to be explained in the next chapter.
Last updated