Provide an LLM-based ChatBot for NetSuite Users
The following sample creates a custom NetSuite form titled Chat Bot. The user can enter a prompt for the large language model (LLM) in the Prompt field. After the user clicks Submit, NetSuite sends the request to the LLM. The LLM returns a response, which is displayed as part of the message history displayed on the form.
The script includes code that handles the prompts and responses as a conversation between the user and the LLM. The code associates the prompts the user enters as the USER messages (these messages are labeled You on the form) and the responses from the LLM as CHATBOT messages (these messages are labeled ChatBot on the form). The code also assembles a chat history and sends it along with the prompt to the LLM. Without the chat history, it would treat each prompt as an unrelated request. For example, if your first prompt asks a question about Las Vegas and your next prompt asks, “What are the top 5 activities here?”, the chat history gives the LLM the information that “here” means Las Vegas and may also help the LLM avoid repeating information it already provided.
This script sample uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/ui/serverWidget', 'N/llm'], (serverWidget, llm) => {
/**
* Creates NetSuite form to communicate with LLM
*/
function onRequest (context) {
const form = serverWidget.createForm({
title: 'Chat Bot'
})
const fieldgroup = form.addFieldGroup({
id: 'fieldgroupid',
label: 'Chat'
})
fieldgroup.isSingleColumn = true
const historySize = parseInt(
context.request.parameters.custpage_num_chats || '0')
const numChats = form.addField({
id: 'custpage_num_chats',
type: serverWidget.FieldType.INTEGER,
container: 'fieldgroupid',
label: 'History Size'
})
numChats.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
})
if (context.request.method === 'POST') {
numChats.defaultValue = historySize + 2
const chatHistory = []
for (let i = historySize - 2; i >= 0; i -= 2) {
const you = form.addField({
id: 'custpage_hist' + (i + 2),
type: serverWidget.FieldType.TEXTAREA,
label: 'You',
container: 'fieldgroupid'
})
const yourMessage = context.request.parameters['custpage_hist' + i]
you.defaultValue = yourMessage
you.updateDisplayType({
displayType: serverWidget.FieldDisplayType.INLINE
})
const chatbot = form.addField({
id: 'custpage_hist' + (i + 3),
type: serverWidget.FieldType.TEXTAREA,
label: 'ChatBot',
container: 'fieldgroupid'
})
const chatBotMessage =
context.request.parameters['custpage_hist' + (i + 1)]
chatbot.defaultValue = chatBotMessage
chatbot.updateDisplayType({
displayType: serverWidget.FieldDisplayType.INLINE
})
chatHistory.push({
role: llm.ChatRole.USER,
text: yourMessage
})
chatHistory.push({
role: llm.ChatRole.CHATBOT,
text: chatBotMessage
})
}
const prompt = context.request.parameters.custpage_text
const promptField = form.addField({
id: 'custpage_hist0',
type: serverWidget.FieldType.TEXTAREA,
label: 'You',
container: 'fieldgroupid'
})
promptField.defaultValue = prompt
promptField.updateDisplayType({
displayType: serverWidget.FieldDisplayType.INLINE
})
const result = form.addField({
id: 'custpage_hist1',
type: serverWidget.FieldType.TEXTAREA,
label: 'ChatBot',
container: 'fieldgroupid'
})
result.defaultValue = llm.generateText({
prompt: prompt,
chatHistory: chatHistory
}).text
result.updateDisplayType({
displayType: serverWidget.FieldDisplayType.INLINE
})
} else {
numChats.defaultValue = 0
}
form.addField({
id: 'custpage_text',
type: serverWidget.FieldType.TEXTAREA,
label: 'Prompt',
container: 'fieldgroupid'
})
form.addSubmitButton({
label: 'Submit'
})
context.response.writePage(form)
}
return {
onRequest: onRequest
}
})