Definition
The code node allows users to run Python code within a workflow to achieve customized data processing and implement complex logic.

Quick Start/How to Use
Node Configuration Details: In ChatInsight.AI, right-click and select "Add Node" to add a 'Code' node to the workflow.

1. Configure the Input Variables Parameter for the Code Node
You can add new input variables by clicking the "+" button on the right. You must specify a name for each input variable and select the corresponding variable value from the workflow.

2. Configure the Python3 Parameter for the Code Node
You can write Python code in the code editor to process and calculate the input variables. The code needs to define a main function that takes input variables as parameters and returns a dictionary as the output result.

- Inserted VariablesIf you want to reference a variable from the previous node while writing code, you can quickly insert it by entering{or/to open inserted variables.
- AI Python debuggerIf you lack a Python background and find it challenging to write code, you can use the "AI Code" feature, which leverages AI capabilities to quickly generate high-quality Python code. To do so, the description in "your requirement" needs to be clear enough; the more detailed the steps, the better. You can then gradually refine the requirement description based on the change suggestions returned by AI coding and rewrite your applications multiple times until they run successfully.

3. Configure the Variable Output Mode Parameter for the Code Node
The "Variable Output Mode" offers two options: "Extract Field" and "Entire Output." The "Extract Field" option allows users to directly extract specific fields from the results of code as output variables, which can have one or multiple outputs. The "Entire Output" option allows users to output the entire dictionary from the code results into a single variable, resulting in only one output.

4. Configure the Iutput Variable Parameter for the Code Node

The Iutput Variable requires the mandatory input of an associated "Variable Name" and the selection of a "Data Type." The output "Variable Name" must match exactly with the keys in the return statement of the Python code to avoid errors caused by mismatches. The data types for Iutput variables in the Code node are as follows:
Data Type | Description |
---|---|
String | Used to store text information, such as usernames, addresses, etc. |
Number | Used to store integers or floating-point numbers for recording numerical data such as age or price. |
Bool | Boolean type, can only be true or false, commonly represents logical judgment results. |
Array[Bool] | An array that stores multiple boolean values, such as a collection of task completion statuses. |
Array[Number] | An array that stores multiple numeric values, such as product prices or quantities. |
Array[Object] | An array in which each element is an object that stores complex data structures, such as user information. |
Array[String] | An array that can contain multiple strings, such as multiple product names or city names. |
Object | Stores data in key-value pair format, such as detailed user information or order details. |
Typical Use Cases:
- String Concatenation
- This example code concatenates two input variables, arg1 and arg2, and stores the result in the output variable "result". For instance, if arg1 is "Hello" and arg2 is "World", then the value of result will be "HelloWorld".
def main(arg1, arg2): return { "result": arg1 + arg2, }
- In this example, the "Variable Output Mode" can optionally select "Extract Fields" to set the result as the output variable, with the data type chosen as String. The optional "Whole Output" will take the entire dictionary {"result": "HelloWorld"}0as the output variable, with the data type as Object.
- This example code performs mathematical operations (including addition, subtraction, multiplication, and division) on two input variables, arg1 and arg2, and stores the results in the output variables "sum", "difference", "product", and "quotient", respectively. For instance, if arg1 is 10 and arg2 is 5, then sum will be 15, difference will be 5, product will be 50, and quotient will be 2.0.
def main(arg1, arg2): return { "sum": arg1 + arg2, "difference": arg1 - arg2, "product": arg1 * arg2, "quotient": arg1 / arg2, }
- This example code takes a piece of input variable data, which is an array, and filters out the elements greater than 0 from it, storing the result in the output variable "filtered_data". For instance, if the data is [-1, 2, -3, 4, 5], then the value of filtered_data will be [2, 4].
def main(data): return { "filtered_data": [item for item in data if item > 0], }
- This example code receives a piece of input variable data, which is an array of strings, and converts each string in it to uppercase, storing the result in the output variable "converted_data". For instance, if the data is ["hello", "world"], then the value of converted_data will be ["HELLO", "WORLD"].
def main(data): return { "converted_data": [item.upper() for item in data], }
- This example code performs a division operation on two input variables, arg1 and arg2, and stores the result in the output variable "result". If arg2 is 0, it catches the ZeroDivisionError exception and returns an error message stored in the output variable "error". For instance, if arg1 is 10 and arg2 is 0, the value of error will be "Division by zero is not allowed."
def main(arg1, arg2): try: result = arg1 / arg2 return { "result": result, } except ZeroDivisionError: return { "error": "Division by zero is not allowed.", }
Practical Case Scenario:

Here are the steps to extract comments from an Instagram video:
- Start NodeCreate input variable (x) instagram id
- Code NodeUse the code node to preprocess the input Instagram ID, checking and removing any spaces and other symbols. If the user inputs a URL, the code should extract the ID from the URL for the next operation.
- HTTP Request NodeUse the HTTP node to search for comments on the corresponding Instagram video via the API.
This will allow code checks to be performed on the user-inputted Instagram ID, ensuring that the format of the ID remains correct at all times.
Notes:
- Special limitations of the Python 3 interpreterSandbox Environment: Only supports Python 3, can only use the Python standard library (e.g., datetime, json, re, etc.), and the requests library.
- Cannot use other third-party libraries(e.g., pandas, numpy, etc.).
- Cannot read or write local files
- cannot save files to the server
- cannot access databases