Selva Arun
Mega Sage
Mega Sage

Integrating ChatGPT with Virtual Agent for KB Article Creation

Overview

I’ve been testing out the integration of ChatGPT with ServiceNow Virtual Agent, and as part of this learning process, I created a use case that automates the creation of Knowledge Base (KB) articles. I got this use case from a friend and decided to experiment with it to see if anyone else might be interested in learning or experimenting as well. This is a personal learning experience, and I wanted to share it with the community in case you’re looking to implement something similar for your own requirements.
The idea behind this integration is simple: Users can request a new KB article, and the Virtual Agent uses ChatGPT to generate the content based on the user’s input. This integration could save a lot of time and streamline the KB article creation process!

Use Case: KB Article Creation

In this use case, ServiceNow Virtual Agent acts as an interface where users can ask for a new KB article to be created. Once the user provides basic details, like the article’s title and description, ChatGPT is used to generate the content for the article. The content is then saved into ServiceNow’s Knowledge Base, making the process of creating and publishing KB articles much faster.

Prerequisites

Before you start integrating ChatGPT with Virtual Agent, here are the requirements:

  1. ServiceNow Instance: Ensure you have a working ServiceNow instance with Virtual Agent enabled.
  2. ChatGPT API: You’ll need access to the ChatGPT API. Make sure you have your API key or token handy.
  3. REST Message Configuration: A REST message must be configured to connect with the ChatGPT API (you can refer to my earlier post for detailed steps on this: https://www.linkedin.com/posts/selvarun_chatgpt-integration-with-servicenow-latest-activity-72820970...
  4. Virtual Agent Designer: Make sure you have access to the Virtual Agent Designer in ServiceNow to create new topics.

Steps to Implement ChatGPT Integration for KB Article Creation

Step 1: Create and Configure a REST Message for ChatGPT

  1. Create a REST Message:
    • Navigate to System Web Services > Outbound > REST Message in ServiceNow.
    • Set up a new REST message that communicates with the ChatGPT API.
  2. Configure the REST Message:
    • Endpoint URL: Use the correct API endpoint for ChatGPT (e.g., OpenAI’s GPT API).
    • HTTP Method: Set the method to POST.
    • Headers: Don’t forget to include the Authorization header with your API key.
    • Request Body: Set up the body with user inputs like title, description, etc., which ChatGPT will use to generate the article content.
  3. Parse the Response: The response from ChatGPT will contain the generated article. You’ll need to extract this content from the response to use it in your next steps.

Step 2: Create the Virtual Agent Topic for KB Article Creation

  1. Open the Virtual Agent Designer:
    • Go to Virtual Agent > Designer in ServiceNow and create a new topic.

selvarun_0-1736786117990.png

 

 

selvarun_1-1736786117999.png

 

Below is the script which I wrote for my Use Case:

(function execute() {

    try {

        // Ensure the input query is provided

        if (!vaInputs.enter_the_query) {

            throw new Error('No query provided.');

        }

 

        // Create a new REST message to call OpenAI API

        var r = new sn_ws.RESTMessageV2('OpenAI ChatGPT', 'AskChatGPT');

 

        // Set the parameters for the OpenAI API

        var requestBody = {

            model: 'gpt-4-1106-preview',  // Ensure this model is correct

            temperature: 0.5,

            max_tokens: 1024,

            messages: [{

                role: 'user',

                content: vaInputs.enter_the_query.toString()  // User query

            }]

        };

 

        // Set the request body as a JSON string

        r.setRequestBody(JSON.stringify(requestBody));

 

        // Log the request body for debugging (in development only)

        gs.log('Request to OpenAI: ' + JSON.stringify(requestBody));

 

        // Execute the REST request

        var response = r.execute();

        var responseBody = response.getBody();

        var httpStatus = response.getStatusCode();

 

        // Check the HTTP response status

        if (httpStatus == 200) {

            var data = JSON.parse(responseBody);

            if (data.choices && data.choices.length > 0) {

                var gptAnswers = data.choices[0].message.content;

                return gptAnswers;  // Return the response from GPT-4

            } else {

                throw new Error('Unexpected response structure from OpenAI.');

            }

        } else {

            // Log error details for troubleshooting

            gs.log('Error from OpenAI API: ' + responseBody + ', HTTP Status: ' + httpStatus);

            return 'Sorry, there was an error retrieving the response from the AI. HTTP Status: ' + httpStatus;

        }

 

    } catch (error) {

        // Log any unexpected errors

        gs.log('Unexpected error: ' + error.message);

        return 'An unexpected error occurred: ' + error.message;

    }

})();

Step 3: Test the Integration

  1. Test the Topic: Once everything is set up, start a test conversation with the Virtual Agent:

selvarun_6-1736786118014.png

 

selvarun_7-1736786118019.png

 

selvarun_8-1736786118023.png

 

selvarun_9-1736786118028.png

 

 

 


Note: During testing, I found that the Category field wasn’t included or added when creating an entry in the KB Article table, which I later added.

 

 

  1. Verify the Data: Check your Knowledge Base in ServiceNow to make sure the article was created and saved correctly.

 

 

 

 

Troubleshooting Tips

  • Problem: ChatGPT doesn't generate the expected content.
    • Solution: Review the prompt you're sending to ChatGPT. Make sure it's clear and specific enough to generate the right article content.
  • Problem: The article creation fails.
    • Solution: Double-check the field mappings in the Create Record node. Ensure that the article data is correctly mapped to the KB article fields.
  • Problem: Virtual Agent doesn’t properly call ChatGPT.
    • Solution: Verify the configuration of your REST message. Check that the API key, request body, and endpoint URL are correct.

 

Conclusion

I created this use case while learning about integrating ChatGPT with ServiceNow Virtual Agent and wanted to share it with you. This integration can significantly automate the creation of Knowledge Base articles, saving time and improving efficiency in the process.

Of course, this implementation can be further improvised and customized to better suit your specific requirements. Whether you want to enhance the article generation, refine the conversation flow, or adapt it for different use cases, there’s plenty of room for flexibility.

Feel free to explore and experiment with this integration to improve your knowledge management process. Don’t hesitate to reach out if you have any questions or need further assistance—I'm here to help!

Credit: The step-by-step process for integrating ChatGPT with ServiceNow was inspired by this excellent post: Step-by-Step Process for Integrating ChatGPT with ServiceNow. I used this resource as a foundation for building my flow and am grateful for the insights it provided.

 

Version history
Last update:
‎01-13-2025 08:39 AM
Updated by:
Contributors