AI Personal Learning
and practical guidance

Variable Aggregation and Variable Assignment Nodes in Dify Workflow

variable aggregation

define

Aggregate variables from multiple branches into a single variable for uniform configuration of downstream nodes.

The Variable Aggregation node (formerly Variable Assignment node) is a key node in the workflow that is responsible for consolidating the outputs of different branches, ensuring that the results are referenced and accessed through a single unified variable regardless of which branch is executed. This is very useful in the case of multiple branches, where variables with the same role under different branches can be mapped to a single output variable, avoiding duplicate definitions at downstream nodes.

take

Variable aggregation simplifies data flow management by allowing multiple outputs, such as problem classification or conditional branching, to be aggregated into a single output for use and manipulation by nodes downstream of the process.


Multi-way aggregation after problem classification

Without adding variable aggregation, the downstream LLM and direct response nodes need to be defined repeatedly for the classification 1 and classification 2 branches after they have been retrieved from different knowledge bases.

Variable Aggregation Node in Dify Workflow-1
Categorization of issues (aggregation without variables)

 

Adding variable aggregation allows you to aggregate the output of two knowledge retrieval nodes into a single variable.

Variable Aggregation Node in Dify Workflow-1
Multi-way aggregation after problem classification

 

Multiplex Aggregation after IF/ELSE Conditional Branching

Variable Aggregation Node in Dify Workflow-1
Multi-way aggregation after problem classification

 

Formatting requirements

The Variable Aggregator supports aggregation of a wide range of data types, including strings (String), figures (Number), documentation (File) objects (Object) and arrays (Array)

Variable aggregators can only aggregate variables of the same data type The first variable to be added to the variable aggregation node is the variable data format. If the data format of the first variable added to the variable within the variable aggregation node is StringIf you want to add a variable to the list, you can add it to the list by clicking on the "Add" button. String Type.

aggregate grouping

With aggregate grouping turned on, the variable aggregator can aggregate multiple groups of variables, with the same data type required for aggregation within each group.

 

variable assignment

define

The Variable Assignment node is used to make variable assignments to writable variables, and the following writable variables are supported:

Usage: The Variable Assignment node allows you to assign a variable within a workflow to a session variable for temporary storage and can be continuously referenced in subsequent conversations.

Variable Aggregation and Variable Assignment Nodes in Dify Workflow-1

Sample Scenarios

You can put the dialog process ofContext, files uploaded to the dialog, preferences entered by the useretc., are written to session variables via the variable assignment node and used as reference information for subsequent conversations.

Scene 1

Automatic judgment to extract and store information from conversations The LLM can record important information entered by the user within the session via an array of session variables and have the LLM personalize responses in subsequent conversations based on the historical information stored in the session variables.

Example: After starting a conversation, LLM automatically determines if the user input contains facts, preferences, or history that needs to be remembered. If it does, LLM extracts and stores this information before using it as context for the answer. If there is no new information to store, LLM will answer the question directly using its own knowledge of the relevant memory.

Variable Aggregation and Variable Assignment Nodes in Dify Workflow-1

Configure the process:

  1. Setting session variables : First set up an array of session variables memories, of type array[object], is used to store the user's facts, preferences, and history.
  2. Judgment and extraction of memory ::
    • Add a conditional judgment node that uses LLM to determine if user input contains new information that needs to be remembered.
    • If there is new information, walk up the branch and use the LLM node to extract that information.
    • If there is no new information, go down the branch and answer directly using existing memory.
  3. Variable assignment/writing ::
    • In the upper branch, the new information extracted is appended (append) using the variable assignment node to the memories in the array.
    • Use the escape function to convert text strings output by LLM into a format suitable for storage in array[object].
  4. Variable Reading and Use ::
    • In a subsequent LLM node, the memories The contents of the array are converted to a string and inserted into LLM's Prompts as context.
    • LLM uses this historical information to generate personalized responses.

The code node code in the figure is as follows:

  1. Escape a string into an object

Copy

import json
defmain(arg1:str) ->object.
try: # Parse the input JSON string.
# Parse the input JSON string
input_data = json.loads(arg1)
# Extract the memory object
memory = input_data.get("memory", {})
# Construct the return object
result = {


"memories": memory.get("memories", [])
}
return{
"mem": result
}
except json.JSONDecodeError: return{ "mem": result }
JSONDecodeError.
"result": "Error: Invalid JSON string"
}
JSONDecodeError: return{ "result": "Error: Invalid JSON string" } }
return{
"result":f "Error: {str(e)}"
}
  1. Escape object to string

Copy

import json
defmain(arg1:list) ->str.
try: # Assume arg1[0] is the dictionary we need to process.
# Assume arg1[0] is the dictionary we need to process
context = arg1[0] if arg1 else{}
# Construct the memory object
memory ={"memory": context}
# Convert the object to a JSON string
json_str = json.dumps(memory, ensure_ascii=False, indent=2)
# Wrap the JSON string in  tags
result =f "{json_str}"
return{
"result": result
}
exceptExceptionas e: return{"result": result }
return{
"result":f"Error: {str(e)}"
}

Scene 2

Record the user's initial preference information , which remembers the language preference entered by the user within the session and consistently replies using that language type in subsequent conversations.

Example: A user starts a dialog with the language If "Chinese" is specified in the input box, the language will be written into the session variable, and the LLM will refer to the information in the session variable when replying in the following dialogs, and continue to reply in "Chinese" in the following dialogs.

Variable Aggregation and Variable Assignment Nodes in Dify Workflow-1

Configure the process:

Setting session variables : First set a session variable language, add a conditional judgment node at the beginning of the session flow to determine the language Whether the value of the variable is null.

Variable Write/Assignment : At the beginning of the first round of dialogues, if language If the value of the variable is null, the LLM node is used to extract the language entered by the user, and then the variable assignment node is used to write that language type to the session variable language Center.

Variable Reading : In subsequent rounds of the dialogues language variable already stores the user's language preference. In subsequent conversations, the LLM node replies with the user's preferred language type by referencing the language variable.

Scene 3

Auxiliary Checklist Check The Checklist is a set of variables that can be used to record user inputs within a session via session variables, update the contents of the Checklist, and check for missing entries in subsequent conversations.

Example: After starting a dialog, LLM will ask the user to enter the items in the Checklist in the dialog box, once the user mentions the items in the Checklist, it will be updated and stored in the session variable. lLM will remind the user to continue to add the missing items after each round of dialog.

Variable Aggregation and Variable Assignment Nodes in Dify Workflow-1

Configure the process:

  • Setting session variables: First set a session variable ai_checklist, the variable is referenced within the LLM as the context to be examined.
  • Variable assignment/writing: At each round of dialog, within the LLM node, check the ai_checklist and compare it to the user input, if the user provides new information, update the checklist and write the output to the ai_checklist Inside.
  • Variables to read: Each round of dialog reads ai_cheklist values in the checklist and compares the user input until all checklists are complete.

Assigning Nodes Using Variables

Click on the right side of the node + No., select "Variable assignment" node, which configures the variables that need to be assigned with the source variable. The Variable Assignment node supports assigning values to multiple variables at the same time.

Variable Aggregation and Variable Assignment Nodes in Dify Workflow-1

Setting Variables

Variables: Selects the variable that needs to be assigned a value.

Setting variables: Selects the variable to be assigned, i.e., specifies the source variable to be converted.

The variable assignment logic in the above figure: the language preference filled in by the user in the initial page is taken into account. Start/language Variable assignment to system-level session variables language Inside.

Specify the write mode of the variable

The data type of the target variable will affect the write mode of the variable. The following are the write modes between different variables:

  1. The data type of the target variable is StringThe
  • override The source variable is overwritten directly to the target variable
  • empty The following is an example of a variable that can be used to clear the contents of a selected variable.
  • set up If you want to specify a value manually, you don't need to set the source variable.
  1. The data type of the target variable is NumberThe
  • override The source variable is overwritten directly to the target variable
  • empty The following is an example of a variable that can be used to clear the contents of a selected variable.
  • set up If you want to specify a value manually, you don't need to set the source variable.
  • digital processing The target variables were subjected to aaddition, subtraction, multiplication and divisionmanipulate
  1. The data type of the target variable is ObjectThe
  • override The content of the source variable is overwritten directly into the target variable.
  • empty The following is an example of a variable that can be used to clear the contents of a selected variable.
  • set up If you want to specify a value manually, you don't need to set the source variable.
  1. The data type of the target variable is ArrayThe
  • override The content of the source variable is overwritten directly into the target variable.
  • empty The following is an example of a variable that can be used to clear the contents of a selected variable.
  • an additional posthumous title , add a new element to the target's array variable
  • extensions If you want to add a new array to the target's array variable, i.e., add more than one element at a time, you can do so.
AI Easy Learning

The layman's guide to getting started with AI

Help you learn how to utilize AI tools at a low cost and from a zero base.AI, like office software, is an essential skill for everyone. Mastering AI will give you an edge in your job search and half the effort in your future work and studies.

View Details>
May not be reproduced without permission:Chief AI Sharing Circle " Variable Aggregation and Variable Assignment Nodes in Dify Workflow

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish