Code Node

Integrating Python Code in Workflow Nodes

You can run Python code within one or more workflow nodes to generate results based on specific parameters. This capability addresses current limitations of large language models (LLMs), such as their inability to perform complex calculations, handle long contexts effectively, execute symbolic or logical reasoning, process large-scale data, or execute external tasks reliably. These challenges arise due to the following factors:

  • Token-Based Computation:

    • LLMs process information as sequences of tokens rather than using traditional numerical or logical operators.

    • Mathematical calculations are inferred based on patterns in training data instead of being executed algorithmically.

  • Training Limitations:

    • LLMs are trained on extensive but finite datasets that include examples of logic and mathematics.

    • They lack intrinsic "knowledge" or specialized algorithms for problem-solving, leading to potential errors in unusual or unfamiliar logic problems and equations.

  • Non-Deterministic Behavior:

    • Responses may vary depending on how a problem is phrased. Rephrasing a math problem could yield a different or incorrect result.

  • Precision Issues:

    • LLMs may produce small but significant errors in arithmetic or symbolic reasoning, particularly when dealing with large numbers or multi-step calculations.

  • Memory Limitations:

    • Logical reasoning chains or lengthy mathematical problems may exceed the model's capacity to retain all necessary steps, resulting in errors.

By embedding a coding tool within your workflow, these challenges can be addressed effectively. Python code can handle complex computations and output results directly to other nodes or as the final outcome.

Code Node Example

Configuring the Code Node

Follow these steps to configure a Code Node in your workflow:

  • Select Node Type:

    • Choose "Code" as the node type during configuration.

  • Define Node Name and Description:

    • Provide a meaningful name and description to clearly define the purpose of this node.

  • Write Python Code:

    • Implement the Python code to be executed within the Code Node.

    • To reference variables from previous nodes, please type {{ in the code editor. This will enable autocompletion for selecting variables.

    • Variables are formatted as {{variable_name}}@[variable_reference], where:

      • variable_name: The name of the chosen value.

      • variable_reference: The system-defined parameter.

    • Use local variables to store referenced values for improved readability.

    • Example: llm_output = {{llm_output}}@[llm_output]

  • Define Output Schema:

    • Specify the output schema, which can be of type Text, Number, Boolean, Array, or Object with multiple properties.

    • Define a local variable named result in your Python code. The type of result must match the defined output schema.

    • Example: result = { "summary": "Processed data", "value": 42 }

By integrating Python code into your workflow nodes, you can efficiently handle complex computations and seamlessly integrate the results into your workflow.

Code Example

Example to create a loan calculater to compute the monthly payment given a total loan amount, annual interest rate, and how many month do you want to pay in the previous Trigger Node.

```python
# define the local variables from Variables in previous Node by typping {{
loan_amount = {{trigger.input.total_amount}}@[trigger-Y2GHN9fyV___input.total_amount]
annual_interest_rate = {{trigger.input.yearly_interest}}@[trigger-Y2GHN9fyV___input.yearly_interest]
total_months = {{trigger.input.total_amount}}@[trigger-Y2GHN9fyV___input.total_amount]

# Convert annual interest rate to a monthly rate
monthly_interest_rate = (annual_interest_rate / 100) / 12

# Define a local variable named result as the output 
result = (loan_amount * monthly_interest_rate * (1 + monthly_interest_rate) ** total_months) 
    / ((1 + monthly_interest_rate) ** total_months - 1)
```

Last updated