ChatGPT integration with ServiceNow VirtualAgent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2023 01:12 AM
Hello Folks!!!
Today I got some time to explore ChatGPT and immediately thought of Integrating with ServiceNow and I wanted it to be more interactive like the real ChatGPT (not the full UI). So, I thought of integrating with Virtual Agent which provides the real chat kind of experience. Follow along to try this on your own.
Step-1: Navigate to https://platform.openai.com/ and sign in with your account, then navigate to Your profile -> View API keys, then click on generate secret keys under API keys to get one for you.
Step-2: Create an outbound REST in your ServiceNow instance like the one in the screenshots below.
Add the following HTTP Headers:
Note: Authorization header value should be "Bearer <Your-Secret-Key>"Step-3: Create a POST method in the REST message we created in the previous step.
Add the HTTP Query parameters:
{
"model": "${model}",
"prompt": "${prompt}",
"max_tokens": ${max_tokens},
"temperature": ${temperature}
}
Step-4: Now create a new "Topic" in Virtual Agent (Conversational Interfaces -> Virtual Agent -> Designer) with name "chatGPT" and create a flow like the one below.
Step-5: Create a script variable with default value '...'
Step-6: A -> User Input:
For prompt I wrote the custom script, so that It will be used for the initial greeting prompt and displaying the answer as well.
(function execute() {
if(vaVars.gptAnswer == '...' || vaVars.gptAnswer == ''){
return 'Hello ' + vaInputs.user.name + '! Wanna ask something?'
}else{
return vaVars.gptAnswer;
}
})()
Step-7: B -> Decision: If the user input is Bye or bye It will end the chat otherwise it will call go the REST message node. See the condition for 'Bye' link.
Step-8: C -> Script Action: Used Script Action node to call the REST API, which will query the ChatGPT and stores the response in a script variable. See the code below.
(function execute() {
try {
var r = new sn_ws.RESTMessageV2('chatGPT', 'Query');
r.setStringParameterNoEscape('model', 'text-davinci-003');
r.setStringParameterNoEscape('temperature', '0');
r.setStringParameterNoEscape('max_tokens', '4000');
r.setStringParameterNoEscape('prompt', vaInputs.question);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if(httpStatus == '200'){
var data = JSON.parse(responseBody);
vaVars.gptAnswer = data['choices'][0]['text'];
//singleOutMsg.setTextPart(data['choices'][0]['text']);
}else{
//singleOutMsg.setTextPart('HTTP Error: ' + httpStatus);
vaVars.gptAnswer = 'HTTP Error: ' + httpStatus;
}
} catch(ex) {
vaVars.gptAnswer = ex.message + '';
//singleOutMsg.setTextPart(ex.message + '');
}
})()
​
Step-9: Publish the topic and try the Virtual Agent in Service Portal.
That's it!!! Let me know if you have any thoughts to improve this.
Anvesh
- 5,500 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2023 11:28 PM
You should also exclude bye or Bye from the Valid Question link.
See the condition below:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2023 12:19 AM
@AnveshKumar M thanks for explanation , however i tried doing the same but its giving me error in chatbox like im having trouble ...im not sure what went wrong ,im getting Rest request as 200 but still no luck
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2023 08:12 PM
Need to check once, it should work generally. At which step it is failing actually?
Anvesh