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,845 Views
 
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎04-25-2023 05:51 AM
Hi @Anvesh - Thanks for the detailed steps. You made it very easy for anyone to configure and it works perfectly.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎04-25-2023 07:09 PM
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎04-30-2023 07:22 AM
Hi @AnveshKumar M solution working fine but Conversation is not end Flow not going to end side .even i click end conversation And also if i test this chatgpt topic Non Other topics also not visible
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎07-27-2023 11:23 PM - edited ‎09-21-2023 10:16 PM
