The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Servicenow Gemini Integration with virtual agent

Badrinarayan
Tera Guru

Integrating Google Gemini with ServiceNow

Google Gemini is Google’s next-generation AI initiative, designed as a successor to models such as Google Bard and comparable to OpenAI’s ChatGPT. This guide provides step-by-step instructions to integrate Google Gemini with ServiceNow, enabling you to send questions to Gemini and retrieve AI-generated responses directly within the ServiceNow platform.

 

Prerequisites

Before starting the integration, ensure you have the following:

  1. Google Gemini API Key

    • Obtain an API key from the official [Google Gemini API documentation].

    • Confirm you have access to the endpoint and authentication credentials.

Badrinarayan_0-1758176371454.png

 

  1. ServiceNow Instance

    • Administrator access.

    • Familiarity with creating Tables, REST Messages, and Business Rules.

Integration Steps

Step 1: Obtain Google Gemini API Key

  1. Navigate to the Google Gemini API documentation.

  2. Click Get an API Key to generate one.

  3. Copy and securely store the API key for later use.

Step 2: Test API Connectivity (Optional)

Use Postman to test the Gemini API:

EndPoint

https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5:generateContent

Headers:

Content-Type: application/json

Payload:

{
  "prompt": {
    "text": "ML Models"
  }
}

Authentication: Append ?key=YOUR_API_KEY at the end of the endpoint.

Verify that you receive a valid response before proceeding.

 

Badrinarayan_1-1758176495429.png

 

 

Step 3: Create a REST Message in ServiceNow

  1. Navigate to System Web Services > Outbound > REST Message.

  2. Click New and configure:

    • Name: Gemini Integration

Endpoint:

https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY
  1. Add an HTTP Method:

    • Name: Post

    • HTTP Method: POST

    • Endpoint: Same as above.

  2. Add a Header:

    • Name: Content-Type

    • Value: application/json

  3. Save and test using the Test link to confirm connectivity.


Step 4: Create a Table for Questions and Responses

  1. Navigate to System Definition > Tables.

  2. Create a new table (e.g., u_ask_gemini) with the following fields:

    • u_question (String): Stores the user’s question.

    • u_response (String): Stores the Gemini AI response.

Badrinarayan_2-1758176593322.png

 


Step 5: Create a Business Rule to Automate API Calls

  1. Navigate to System Definition > Business Rules.

  2. Create a new Business Rule:

    • Name: Ask Gemini

    • Table: u_ask_gemini

    • When: Before - Insert , Update

  3. Add the following script:

(function executeRule(current, previous) {
    try {
        var body = JSON.stringify({
            "contents": [{
                "parts": [{
                    "text": current.u_question.toString()
                }]
            }]
        });

        // Create REST message
        var r = new sn_ws.RESTMessageV2('Gemini Integration', 'Post');
        r.setRequestBody(body);

        // Execute the REST call
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();

        if (httpStatus == 200) {
            // Parse and save Gemini response
            var res = JSON.parse(responseBody);
            current.u_response = res.candidates[0].content.parts[0].text;
        } else {
            gs.error('Error in Google Gemini API call: ' + responseBody);
        }
    } catch (ex) {
        gs.error('Exception in Business Rule: ' + ex.message);
    }
})(current, previous);

 Save the Business Rule.

 

Step 6: Test the Integration

  1. Navigate to the u_ask_gemini table.

  2. Create a new record and enter a question in the u_question field.

  3. Save the record.

    • The Business Rule will trigger automatically.

    • The u_response field will be populated with the AI-generated answer.


Step 7: Virtual Agent Integration

You can further enhance the experience by integrating Gemini with ServiceNow’s Virtual Agent:

  1. Go to Conversational Interfaces > Designer.

  2. Create a new topic:

    • Name: AI Integration

    • Description: Send questions to Google Gemini and retrieve AI responses.

  3. Add intents and keywords such as:

    • "AI question", "Gemini help", "ask AI".

  1.  

Badrinarayan_3-1758176865227.png

 

  1. Add intents and keywords such as:

    • "AI question", "Gemini help", "ask AI".

(function execute() {
    try {
        var body = JSON.stringify({
            "contents": [{
                "parts": [{
                    "text": vaInputs.ask_gemini.getValue()
                }]
            }]
        });

        var r = new sn_ws.RESTMessageV2('Gemini Integration', 'Post');
        r.setRequestBody(body);

        var response = r.execute();
        var responseBody = response.getBody();
        var res = JSON.parse(responseBody);

        return res.candidates[0].content.parts[0].text;
    } catch (ex) {
        return "Error: " + ex.message;
    }
})();

Conclusion

By following the above steps, you can successfully integrate Google Gemini with ServiceNow, enabling direct AI-powered interactions within your instance. This setup can be extended further with Virtual Agent to provide conversational AI support, enhancing the overall ServiceNow user experience.

 

 

Thanks Regards 

Badrinarayan

0 REPLIES 0