The Code Node allows you to build and execute custom Python functions.

Use the Code node instead of the function calling code block in LLM Tools if you want a more deterministic pipeline (In function calling your LLM will reason to determine if it makes sense to call the function).

Important Notes

  1. Function must be called main()
    • Only a single root function is allow
      • Nested Functions are allowed
    • main() does not take any arguements
  2. Output from Code Node will be stored as CODE_id
    • a return statement is required to output from the code node
    • return output
  3. Outputs from previous nodes or variables can be passed in using standard variable naming convention
    • <?REPLACE_WITH_REF_NODE_ID?>

Allowed functionality with Code Node:

  1. Built-In Python Functions
    • int, str, isinstance, range, len, type, sum, round, ord, float, abs, min, max, all, any, sorted, enumerate, zip, filter, map, reversed, chr, divmod, pow, bin, hex, and oct
  2. Supported Packages
    • json, requests, datetime, time, re, hashlib, bs4, typing, contextlib, decimal
NOTE: in order to use these packages, you will need to import them into your code at the top

Following are commonly used examples: