Set Variable Functions
Save a value from the conversation with a Set Variable Function.
You can store information determined during the conversation for reference in future steps using Set Variable Functions. This is useful for:
- Storing key information (like account numbers, ages, cancellation types) so GenerativeAgent doesn’t have to re-prompt the user later.
- Returning or conditioning logic on data that GenerativeAgent has inferred.
- Manipulating or filtering data from APIs (e.g., extracting the single charge the customer disputes).
GenerativeAgent “sets” these variables in conversation, so they can be used immediately or in subsequent steps. You specify how the variable gets set based on the input parameters or existing variables.
To create a set variable function:
- Create a function.
- Define the input parameters.
- Specify the variables to set.
- Save the function.
- Use the function in a task.
Step 1: Create a Function
Navigate to the Functions page and click “Create Function.”
- Select “Set variable” and click “Next: Function details”
- Specify the Name and Purpose of the Function
- Function Name: Provide a concise, unique name, using underscores (e.g.,
get_lap_child_policy
). - Function Purpose: Briefly describe what the function does (e.g., “Determines whether a child can fly as a lap child”).
- GenerativeAgent uses this description to decide if and when it should invoke the function.
- Function Name: Provide a concise, unique name, using underscores (e.g.,
Step 2: Define Input Parameters (JSON)
The input parameters are the values that GenerativeAgent needs to pass when calling this function.
You can leave the input parameters empty if you won’t need new values from the conversation.
As with any function call, GenerativeAgent will gather the necessary information (from user messages or prior context) before calling the function.
Under “Input Parameters,” enter a valid JSON schema describing the parameters GenerativeAgent needs to pass when calling this function.
Mark a field as “required” if GenerativeAgent must obtain these values from the conversation.
Step 3: Specify “Set Variables”
At least one variable must be configured so GenerativeAgent can store the outcome of your function call. For each reference variable:
- Provide a Variable Name (e.g.,
lap_child_policy
). - Optionally, include Jinja2 transformations to manipulate or combine inputs or existing reference variables.
- Toggle “Include return variable as part of function response” to make the new variable immediately available to GenerativeAgent after the function call.
Jinja2 Templating
Use Jinja2 to create or modify the stored value.
As an Example, the following Jinja2 template will set the variable to “Children under 2 can fly as a lap child.” if the child_age_at_time_of_flight
is less than 2. Otherwise, it will set the variable to “Children 2 or older must have their own seat.”
Step 4: Save Your Function
With your function defined, you can save it by clicking “Create Function”.
After saving, you’ll see a detail page showing the JSON schema and the configured reference variables.
Step 5: Use the Function in the Conversation
Once you have created your set variable function, you must add the function to the task’s list of available functions in order for GenerativeAgent to use it.
GenerativeAgent may call the function proactively, but we recommend you instruct GenerativeAgent to call the function explicitly.
Always make sure to test your functions with Previewer to ensure they work as expected.
Here’s how the function works within a task and conversation flow:
- GenerativeAgent collects the required parameters from the user (or context).
- (Optional) A “Message before Sending” can be displayed to the user, clarifying why GenerativeAgent is saving data.
- Jinja2 transformations convert or combine inputs, if defined.
- Reference variables are created as soon as the function runs successfully—GenerativeAgent can immediately incorporate them into logic or other function calls.
- If you turned on “Include return variable as part of function response,” GenerativeAgent receives the new values right away, shaping subsequent interaction steps.
Best Practices
Here are some recommendations to help you make the best use of the set variables function type:
Next Steps
Task Best Practices
Learn more about best practices for task and function configuration.
Conditional Templates
Use conditional logic to dynamically change instructions based on variables.
Trial Mode
Test your functions in a safe environment before deploying to production.
Previewer
Test your functions and variables in real-time with the Previewer tool.
Was this page helpful?