Servicenow Gemini Integration with virtual agent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
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:
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.
ServiceNow Instance
Administrator access.
Familiarity with creating Tables, REST Messages, and Business Rules.
Integration Steps
Step 1: Obtain Google Gemini API Key
Navigate to the Google Gemini API documentation.
Click Get an API Key to generate one.
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.
Step 3: Create a REST Message in ServiceNow
Navigate to System Web Services > Outbound > REST Message.
Click New and configure:
Name: Gemini Integration
Endpoint:
https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY
Add an HTTP Method:
Name: Post
HTTP Method: POST
Endpoint: Same as above.
Add a Header:
Name: Content-Type
Value: application/json
Save and test using the Test link to confirm connectivity.
Step 4: Create a Table for Questions and Responses
Navigate to System Definition > Tables.
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.
Step 5: Create a Business Rule to Automate API Calls
Navigate to System Definition > Business Rules.
Create a new Business Rule:
Name: Ask Gemini
Table: u_ask_gemini
When: Before - Insert , Update
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
Navigate to the u_ask_gemini table.
Create a new record and enter a question in the u_question field.
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:
Go to Conversational Interfaces > Designer.
Create a new topic:
Name: AI Integration
Description: Send questions to Google Gemini and retrieve AI responses.
Add intents and keywords such as:
"AI question", "Gemini help", "ask AI".
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
- 86 Views