Custom JS Function
Overview
The Custom JS Function node allows you to execute custom JavaScript code within your AnswerAI workflows. This powerful utility enables you to perform complex operations, data manipulations, and integrate external libraries into your chatflows.
Key Benefits
- Flexibility to implement custom logic and algorithms
- Ability to integrate external JavaScript libraries
- Powerful tool for data transformation and manipulation
How to Use
-
Add the Custom JS Function node to your workflow canvas.
-
Configure the node with the following inputs:
- Input Variables (optional): Define input variables that can be used in your function.
- Function Name (optional): Give your function a descriptive name.
- JavaScript Function: Write your custom JavaScript code in this field.
-
Connect the node to other components in your workflow as needed.
Tips and Best Practices
- Use the
$input
variable to access the input data passed to the node. - Utilize
$vars
to access variables from the AnswerAI environment. - Access flow-specific information using the
$flow
object, which containschatflowId
,sessionId
,chatId
, andinput
. - When defining input variables, use the
$
prefix to access them in your code (e.g.,$myVariable
). - You can use both built-in Node.js modules and external npm packages in your custom functions, depending on your AnswerAI configuration.
Troubleshooting
- Invalid JSON Error: If you encounter an "Invalid JSON" error when using input variables, double-check that your JSON is correctly formatted.
- Undefined Variables: Ensure that all variables used in your function are properly defined or passed as input variables.
- External Module Issues: If you're trying to use an external module and encountering errors, verify that the module is included in your AnswerAI configuration's allowed dependencies.
Example
Here's a simple example of a custom function that concatenates two strings:
const result = $firstName + ' ' + $lastName
return result
In this example, $firstName
and $lastName
should be defined as input variables when configuring the node.
Remember to test your custom functions thoroughly to ensure they work as expected within your AnswerAI workflows.