ChatGPT integration with ServiceNow VirtualAgent

AnveshKumar M
Tera Sage
Tera Sage

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.AnveshKumarM_0-1677830093584.png

Add the following HTTP Headers:

Note: Authorization header value should be "Bearer <Your-Secret-Key>"AnveshKumarM_2-1677830184695.pngStep-3: Create a POST method in the REST message we created in the previous step.AnveshKumarM_5-1677830391322.png

Add the HTTP Query parameters:

 

{ 
  "model": "${model}",
  "prompt": "${prompt}",
  "max_tokens": ${max_tokens},
  "temperature": ${temperature}
}

AnveshKumarM_7-1677830567648.pngStep-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.

 

AnveshKumarM_8-1677830828716.png

Step-5: Create a script variable with default value '...'

AnveshKumarM_12-1677832607229.png

 

Step-6: A -> User Input: 

AnveshKumarM_9-1677830968924.png

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. 

AnveshKumarM_10-1677831182651.pngStep-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.

 

AnveshKumarM_13-1677832743268.pngAnveshKumarM_14-1677832764001.png

 

That's it!!! Let me know if you have any thoughts to improve this.

 

Thanks,
Anvesh
7 REPLIES 7

pratyakshajadon
Tera Contributor

You should also exclude bye or Bye from the Valid Question link.

See the condition below:

pratyakshajadon_0-1690525561986.png

 

subhiksha KL
Tera Contributor

@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

Hi @subhiksha KL 

Need to check once, it should work generally. At which step it is failing actually?

Thanks,
Anvesh