Skip to main content

Code Nodes

Code Nodes

Code Nodes in BotDojo provide the flexibility and power of running JavaScript (with Python support coming soon) within your AI applications. While visual designers excel at handling functions that follow similar patterns, such as building prompts and making calls to Language Models (LLMs), there are cases where writing code is more efficient and effective.

Code Nodes come in handy when you need to perform specific actions like making calculations or calling out to an API. They also simplify the process of handling complex logic involving loops, branching (if/then/else), and other programming constructs.

Input and Execution

Each Code Node accepts an input defined by a JSON Schema, similar to AI Function Nodes and AI Agent Nodes. The entry function for a Code Node is called execute, and it takes three parameters:

  1. input: An object that represents the input data, structured according to the defined JSON Schema.
  2. node: The node object itself, which allows you to inspect and modify properties of the Code Node to alter its behavior.
  3. context: An object that provides access to the session state and allows you to log events that will be available in the execution trace.

Here's an example of the execute function signature:

async function execute(input, node, context) {
// Code logic goes here
let reversedString = input.text.split('').reverse().join('');
return { reversed_string: reversedString };
}

Interacting with Other Nodes

Code Nodes can take special nodes as inputs, such as LLMs, Prompts, and Tools. This enables the Code Node to call these interfaces and access other nodes within the flow. By leveraging this capability, you can seamlessly integrate third-party libraries with BotDojo, getting the best of both worlds—the ability to mix and match functionalities that best suits your senario.

async function execute(input,node, context)
{
let spanishResults = []
context.log('starting function')
for(let sentence of input.sentences)
{
let spanish = await input.llm.completion("Please translate "+sentence+" to spanish. Only respond with the translation.\n")
spanishResults.push(spanish.outputText)
}
context.log('return result')
return {
spanish_sentences:spanishResults
}
}

Below is an example of using Code Nodes. Click the image below to Clone it to your project.