Create a Prompt and Evaluate It

The following sample creates a prompt record, populates the required record fields, and evaluates the prompt by sending it to the default NetSuite large language model (LLM).

This sample creates a prompt record using the record.create(options) method of the N/record module, and it sets the values of the following fields (which are required to be able to save a prompt record):

After the prompt record is saved, llm.evaluatePrompt(options) is called, but only the ID of the created prompt is provided as a parameter. The method throws a TEMPLATE_PROCESSING_EXCEPTION error because the prompt template includes a required variable (mandatoryVariable) that was not provided when the method was called. The error is caught, and a debug message is logged.

Next, the sample calls llm.evaluatePrompt(options) again but provides values for the mandatoryVariable and optionalVariable variables. This time, the call succeeds, and a debug message is logged. Finally, the sample calls llm.evaluatePrompt.promise(options) and confirms that the call succeeds. When the call succeeds, the prompt record is deleted.

You can create and manage prompts using Prompt Studio. You can also use Prompt Studio to generate a SuiteScript example that uses the llm.evaluatePrompt(options) method and includes the variables for a prompt in the correct format. When viewing a prompt in Prompt Studio, click Show SuiteScript Example to generate SuiteScript code with all of the variables that prompt uses. You can then use this code in your scripts and provide a value for each variable. For more information about Prompt Studio, see Prompt Studio.

For instructions about how to run a SuiteScript 2.1 code snippet in the debugger, see On-Demand Debugging of SuiteScript 2.1 Scripts. Step through the code until the line before the end of the script to see the response text returned from the LLM and the remaining free usage for the month.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

          /**
* @NApiVersion 2.1
*/
require(['N/record', 'N/llm'], function(record, llm) {
    const rec = record.create({
        type: "prompt"
    });
    
    rec.setValue({
        fieldId: "name",
        value: "Test"
    });
    rec.setValue({
        fieldId: "prompttype",
        value: "CUSTOM"
    });
    rec.setValue({
        fieldId: "modelfamily",
        value: "COHERE_COMMAND_R"
    });
    rec.setValue({
        fieldId: "template",
        value: "${mandatoryVariable} <#if optionalVariable?has_content>${optionalVariable}<#else>World</#if>"
    });
    
    const id = rec.save();
    
    try {
        llm.evaluatePrompt({
            id: id
        });
    }
    catch (e) {
        if (e.name === "TEMPLATE_PROCESSING_EXCEPTION")
            log.debug("Exception", "Expected exception was thrown");
    }
    
    const response = llm.evaluatePrompt({
        id: id,
        variables: {
            mandatoryVariable: "Hello",
            optionalVariable: "People"
        }
    });
    if ("Hello People" === response.chatHistory[0].text)
        log.debug("Evaluation", "Correct prompt got evaluated");
        
    llm.evaluatePrompt.promise({
        id: id,
        variables: {
            mandatoryVariable: "Hello",
            optionalVariable: "World"
        }
    }).then(function(response) {
        if ("Hello World" === response.chatHistory[0].text)
            log.debug("Evaluation", "Correct prompt got evaluated");
        record.delete({
            type: "prompt",
            id: id
        });
        debugger;
    })
}); 

        

General Notices