Skip to content

What are Functions?

Functions in Turingpaper are JavaScript functions that run in a very restricted JavaScript environment. Every JavaScript function is automatically converted into a tool and usable by the AI agents and in your prompt instructions.

Common Uses for Functions

The most common use of functions for Turingpaper automations is to manipulate data, or to sequence and control the overall automation.

Data Manipulation

prompt_find_max
Use `file_read` to read the JSON data in file: `{{json_data_filename}}`.
Then find the line item name with the largest cost in the array of data. 
func_find_max
function findMaxCostLineItem(json_data_filename) {
    const text = file_read(json_data_filename);
    const data = JSON.parse(text);
    let largest = { cost: 0, name: "" };
    for (const item of data) {
        if (item.cost >= largest.cost) {
            largest = item;
        }
    }
    return `${largest.name}`;
}

Both the prompt and the function are usable as tools in Turingpaper, because Turingpaper automatically makes both prompts and functions usable as tools. In that sense the prompt and function above do the same thing, they find the maximum cost line item.

If the data in the json_data_filename is small, then both the function and prompt version will work equally well, but if there is A LOT of data, it is more likely that the prompt version evaluated by AI will produce an incorrect answer.

Sequencing and Control

prompt_deep_research
Run `deep_research_phase1` with parameter output_path
set to `{{output_path}}/phase1`, then use `file_list`
to check if outout files where written, if files
where written go to the next step.
 
Run `deep_research_phase2` with parameter output_path
set to `{{output_path}}/phase2`, then use `file_list`
to check if outout files where written, if files
where written go to the next step.
 
Run `deep_research_phase3` with parameter output_path
set to `{{output_path}}/phase3`, then use `file_list`
to check if outout files where written, if files
where written go to the next step.
 
Run `write_deep_research_result` with parameter output_path
set to `{{output_path}}/research_result.md`
func_deep_research
function runDeepResearch(output_path) {
    const phase1 = `${output_path}/phase1`;
    const phase2 = `${output_path}/phase2`;
    const phase3 = `${output_path}/phase3`;
    const result = `${output_path}/research_result.md`;
 
    deep_research_phase1(phase1);
    if (file_list(phase1).length === 0) {
        return "Error: failed phase 1";
    }
 
    deep_research_phase2(phase2);
    if (file_list(phase2).length === 0) {
        return "Error: failed phase 2";
    }
 
    deep_research_phase3(phase3);
    if (file_list(phase3).length === 0) {
        return "Error: failed phase 3";
    }
 
    return write_deep_research_result(result);
}

Both the prompt and the function sequence the three research phases and the final write up of the research result. Both do very basic error checking of each step before moving on to the next step.

If error checking is simple and an automation runs reliably using the instructions in a prompt to control the sequence of steps, then a prompt is a good option for controlling the automation.

However, if error checking starts to become more and more complex and requires data manipulation for validation, file reading JSON files or checking values in a database, then using a function to sequence the steps of an automation will become the better option.

Copyright © 2024-2025 Turingpaper Technologies, Inc.