Thought this might be useful for somebody. This is assuming that you have already run through this post to get the initial setup done: ServiceNow – ChatGPT Integration.
This is a very basic configuration and it may be better to do it through a flow, but as an example of how you could potentially update a task (incident in this case) and provide recommendations from ChatGPT when the description is updated. This is a fairly flexible approach so you might have other ideas!
Firstly, open up the incident table. We are going to create a new column called “ChatGPT Recommendations”. Give this column the type of “String” and a max length of 2000. Copy the column name (should be u_chatgpt_recommendations) and submit.
Open the incident form and make sure the new column is showing on the form.
We will now create a business rule to deliver the result. Configure the business rule conditions as follows:
Name: | Set recommendations based on description |
Table: | Incident |
Advanced: | true (checked) |
When to run | |
When: | before |
Insert: | true (checked) |
Update: | true (checked) |
Short description: | changes |
Now on the advanced tab we will enter the following code:
(function executeRule(current, previous /*null when async*/ ) { // Create an instance of the ChatGPT class var chatGPT = new global.ChatGPT(); // Set the premise for the chat with the assistant. The premise helps set the context of the conversation. var premise = chatGPT.setPremise("You are an IT professional providing recommendations to non-technical users. You should give the recommendations only, no pretext."); // Create a message requesting ChatGPT to send some recommendations. var message1 = chatGPT.createMessage("user", "Provide some recommendations for this: " + current.description); // Submit the chat to the GPT-3.5 Turbo model (default). The chat consists of the premise and the user's request. // The 'submitChat' function accepts an array of messages which form a conversation. var result = chatGPT.submitChat([premise, message1]); // Extract only the response from the message. var extracted_message = chatGPT.extractAssistantMessage(result); // Populate our new field. current.u_chatgpt_recommendations = extracted_message; })(current, previous);
Once submitted, we should be good to go! As an example, I logged a new incident about my monitor being broken. The system showed the following: