How to Integrate ChatGPT with ServiceNow
In the world of technology, system integration is a crucial step to optimize processes, facilitate communication, and improve operational efficiency. In this article, we will explore how to integrate ChatGPT, an advanced AI language model, with ServiceNow.
To begin, it’s important to understand that the integration is based on creating an “Outbound” REST Message in the sys_rest_message table. This message is configured with the name “ChatGPT,” and in the “Endpoint” field, you enter the following address: “https://api.openai.com/v1/completions“.
In the “HTTP Headers,” you need to include the “Authorization,” which is the token obtained from OpenAI, and also the “Content-Type,” which should be set to “application/json”.
To obtain an OpenAI API Key:
- Go to the OpenAI Platform website at platform.openai.com and log in with your OpenAI account.
- Click on the profile icon in the upper-right corner of the page and select “View API Keys.”
- Click on “Create New Secret Key” to generate a new API key.
Next, we need to create a method in the “HTTP method” called “askChatGPT,” which will use the “POST” method, with the same “Endpoint” and “Headers.” We also need to add the following to the “HTTP Query Parameters”:
{
"model": "${model}",
"prompt": "${prompt}",
"temperature": ${temperature},
"max_tokens": ${max_tokens}
}
After these steps, we have to create a “script include” with the name “ChatGPT.” This script can be called from anywhere in ServiceNow. Below is the code we use:
var ChatGPT = Class.create();
ChatGPT.prototype = {
initialize: function() {
},
askChatGPT: function (message) {
var request = new sn_ws.RESTMessageV2("ChatGPT", "AskChatGPT");
var temperature = 0.5;
var maxTokens = 1024;
request.setStringParameterNoEscape("model","text-davinci-003");
request.setStringParameterNoEscape("prompt",message);
request.setStringParameterNoEscape("temperature",temperature);
request.setStringParameterNoEscape("max_tokens",maxTokens);
var response = request.execute();
var responseBody = response.getBody();
var responseObject = JSON.parse(responseBody);
var answer = responseObject.choices[0].text;
// Print the response from ChatGPT
return answer;
},
type: 'ChatGPT'
};
Explanation of the Code:
- var ChatGPT = Class.create(); – Here, we are creating a new JavaScript class called “ChatGPT.”
- initialize: function() {} – This is the class constructor, which is automatically called when creating a new instance of the class.
- askChatGPT: function (message) {} – This is a method that allows sending a message to ChatGPT and receiving a response. The message to be sent is passed as an argument.
- var request = new sn_ws.RESTMessageV2(“ChatGPT”, “AskChatGPT”); – Here, we are creating a new instance of the RESTMessageV2 object, which is used to send HTTP requests.
- setStringParameterNoEscape(“model”, “text-davinci-003”); and similar commands – We are setting the parameters of our request. In this case, we are setting the “model” as “text-davinci-003,” which is the language model used by ChatGPT. We also set “prompt” as the message passed to the “askChatGPT” method, “temperature” as 0.5 (a value that determines the randomness of ChatGPT’s responses), and “max_tokens” as 1024, which is the maximum number of words in the response.
- var response = request.execute(); – With this command, we execute the request and store the response in the “response” variable.
- var responseBody = response.getBody(); – Here, we extract the response body.
- var responseObject = JSON.parse(responseBody); – We convert the response body, which is a JSON string, into a JavaScript object so that we can work with it.
- var answer = responseObject.choices[0].text; – Here, we extract the response from ChatGPT from the response object. Since ChatGPT can return multiple responses, we choose the first one (index 0) and extract the text field.
- return answer; – Finally, we return the response.
With this code, you can use the power of the ChatGPT language model in conjunction with ServiceNow to optimize user interaction, answer questions, and facilitate communication. I hope this detailed tutorial has been helpful, and now you can make the most out of this integration.
You can also watch the video I made (in English) demonstrating the integration.