Skip to content

Commit 263a8fe

Browse files
GetHelp Script include - Pull6 (#2564)
* script.js Automatically create a problem record from incident volume Use case: Automatically create a problem record if a specific Configuration Item (CI) is associated with more than a certain number of incidents within a defined timeframe. Code snippet This code can be placed in a Scheduled Script Execution or an After Insert Business Rule to check new incidents * README.md * Script.js Identify the oldest active incident for each assignment group. This helps managers focus on long-running tickets that may require special attention. * README.md * script.js This example searches a JSON document for all developers listed under the specified path. * README.md * Update README.md * script.js Identify inactive users who still have unresolved incidents. This helps with offboarding processes and ensures incidents aren't left unattended. * README.md * Update script.js * Delete Core ServiceNow APIs/GlideAggregate/Count Inactive Users with Active incidents/README.md * Delete Core ServiceNow APIs/GlideAggregate/Count Inactive Users with Active incidents/script.js * Delete Core ServiceNow APIs/GlideAggregate/Find oldest Incident based Assignment Groups/README.md * Delete Core ServiceNow APIs/GlideAggregate/Find oldest Incident based Assignment Groups/script.js * Delete Core ServiceNow APIs/GlideJsonPath/GlideJsonPath Reader Example/README.md * Delete Core ServiceNow APIs/GlideJsonPath/GlideJsonPath Reader Example/script.js * GetServiceDeskAgentHelpAIUtil This is a client-callable Script Include designed for the ServiceNow platform that integrates with an external Databricks AI endpoint. Its purpose is to assist Service Desk agents by providing AI-generated responses to user queries, which can be used to populate incident information or assist in troubleshooting. The script acts as a server-side proxy, handling the client-side request, invoking a Flow Designer Action to communicate with the Databricks service, and returning the AI's response to the client. * Delete Server-Side Components/Script Includes/GetServiceDeskAgentHelpAIUtil * script.js This is a client-callable Script Include designed for the ServiceNow platform that integrates with an external Databricks AI endpoint. Its purpose is to assist Service Desk agents by providing AI-generated responses to user queries, which can be used to populate incident information or assist in troubleshooting. * Create README.md * Delete Core ServiceNow APIs/GlideAggregate/Create Problem based on incident volume/README.md * Delete Core ServiceNow APIs/GlideAggregate/Create Problem based on incident volume/script.js
1 parent 111a9d9 commit 263a8fe

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
GetServiceDeskAgentHelpAIUtil
2+
Overview
3+
This is a client-callable Script Include designed for the ServiceNow platform that integrates with an external Databricks AI endpoint. Its purpose is to assist Service Desk agents by providing AI-generated responses to user queries, which can be used to populate incident information or assist in troubleshooting.
4+
The script acts as a server-side proxy, handling the client-side request, invoking a Flow Designer Action to communicate with the Databricks service, and returning the AI's response to the client.
5+
Features
6+
AI-Powered Responses: Sends user search queries to a Databricks-powered Generative AI model to get intelligent answers.
7+
Client-Callable: Can be invoked from client-side scripts (e.g., a UI Action or Client Script) using GlideAjax.
8+
Flow Designer Integration: Uses a Flow Designer Action to execute the REST call to the Databricks endpoint, centralizing the external API logic.
9+
Structured Output: Returns a JSON object containing the AI's response, model metadata, and a trace ID for debugging.
10+
Prerequisites
11+
ServiceNow Configuration
12+
Flow Designer Action: A Flow Designer Action named global.genai_action must be created and configured to handle the REST call to the Databricks AI endpoint. This action must have:
13+
An input named search_query (String).
14+
An output named model_output (String).
15+
Databricks Connection: The Flow Designer Action must be correctly configured with the necessary credentials to connect to the external Databricks API.
16+
System Property: A system property named user.prompt is referenced in the script. It should be created and configured with the required prompt text for the AI.
17+
How to use
18+
1. Calling from a Client Script
19+
You can use GlideAjax to call the getSearchResults function from a client-side script, such as a UI Action or a Catalog Client Script.
20+
javascript
21+
// Example client-side script using GlideAjax
22+
var ga = new GlideAjax('GetServiceDeskAgentHelpAIUtil');
23+
ga.addParam('sysparm_name', 'getSearchResults');
24+
ga.addParam('sysparm_search_key', g_form.getValue('short_description')); // Pass the user's input
25+
ga.getXML(getResponse);
26+
27+
function getResponse(response) {
28+
var answer = response.responseXML.documentElement.getAttribute("answer");
29+
if (answer) {
30+
var result = JSON.parse(answer);
31+
g_form.setValue('comments', result.modelResponse); // Set the AI response in a field
32+
}
33+
}
34+
Use code with caution.
35+
36+
2. Using from a Server Script
37+
The functions can also be called directly from other server-side scripts (e.g., Business Rules).
38+
javascript
39+
// Example server-side script
40+
var searchKey = 'What are the steps to reset my password?';
41+
var aiUtil = new GetServiceDeskAgentHelpAIUtil();
42+
var response = aiUtil.getSearchResults(searchKey);
43+
44+
gs.info(response);
45+
Use code with caution.
46+
47+
Script details
48+
getSearchResults()
49+
This is the main function that coordinates the process.
50+
Retrieves the search term from the client parameters.
51+
Calls the getDataBricksModelResponse() function to get the AI-generated answer.
52+
Constructs a JSON object with the AI's response and model information.
53+
Returns the JSON object as a string.
54+
getDataBricksModelResponse(search)
55+
This function handles the integration with the Databricks AI.
56+
Takes the search query as a parameter.
57+
Executes the global.genai_action Flow Designer Action.
58+
Parses the model_output from the Flow Action's outputs.
59+
Extracts the AI's message content and a trace ID for debugging.
60+
Returns a stringified JSON object containing the AI's response, date, and trace ID.
61+
Includes a try/catch block to handle and log potential errors during the integration process.
62+
Dependencies
63+
Flow Designer Action: global.genai_action
64+
System Property: user.prompt
65+
Troubleshooting
66+
Check the Flow Execution: If the AI response is not received, check the Flow Designer execution logs to ensure global.genai_action is running successfully and the REST call to Databricks is returning a valid response.
67+
Review System Logs: Examine the System Logs (gs.info and gs.error messages) for debugging information related to the script's execution or potential errors from the Databricks API.
68+
Verify Databricks Credentials: Ensure that the credentials and configuration within the Flow Designer action for connecting to Databricks are correct.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
var GetServiceDeskAgentHelpAIUtil = Class.create();
2+
GetServiceDeskAgentHelpAIUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
3+
4+
/**
5+
* Main function that processes a search term from a client-side call,
6+
* sends it to a Databricks-powered AI, and returns the response.
7+
* This function is intended to assist Service Desk agents.
8+
*
9+
* @returns {string} A JSON string containing the AI's response, model details, and metrics.
10+
*/
11+
getSearchResults: function() {
12+
// Defines the use case for logging and metric purposes.
13+
var usecase = "ServiceDesk Helper";
14+
// Gets the current user's Sys ID, though it is not used in the current implementation.
15+
var user = gs.getUserID();
16+
// Retrieves the search term passed from the client-side script.
17+
var searchText = this.getParameter("sysparm_search_key");
18+
// Replaces double quotes with single quotes in the search text to prevent JSON parsing issues.
19+
searchText = searchText.replaceAll('"', "'");
20+
21+
var searchObj = {
22+
"searchValue": searchText.toString()
23+
};
24+
25+
// Extracts the raw search value from the object.
26+
var search = searchObj["searchValue"];
27+
28+
// This object is structured to create a prompt for another potential AI endpoint (possibly for a brief statement),
29+
// but it is currently not used.
30+
var brief_statement_payload = {
31+
"messages": [{
32+
"role": "system",
33+
"content": "You are an Expert ServiceNow bot that helps the users to create an incident"
34+
},
35+
{
36+
"role": "user",
37+
"content": gs.getProperty('user.prompt') + search
38+
}
39+
]
40+
};
41+
42+
var databricks_model_response = {};
43+
// Calls the internal method to get the response from the Databricks model.
44+
var response = this.getDataBricksModelResponse(search);
45+
// UNCOMMENT THIS WHEN WE HAVE A PROPER SOLUTION FOR BRIEF RESPONSE GENERATION
46+
// var brief_response = this.getBriefResponse(brief_statement_payload);
47+
// The brief response is hardcoded to an empty JSON object
48+
// Assigns the model response to the output object.
49+
databricks_model_response.modelResponse = response;
50+
// Assigns a hardcoded model ID.
51+
databricks_model_response.model_id = "Databricks Runbook";
52+
53+
// Converts the final response object to a JSON string for client-side processing.
54+
databricks_model_response = JSON.stringify(databricks_model_response);
55+
// Logs the final JSON string for debugging purposes.
56+
gs.info("Service Desk Helper Results: Testing value of the final databricks response being sent: " + databricks_model_response);
57+
58+
// Returns the JSON string to the calling client script.
59+
return databricks_model_response;
60+
},
61+
62+
/**
63+
* This function calls the Databricks endpoint via a Flow Designer action
64+
* to generate an answer for the user's query.
65+
*
66+
* @param {string} search - The user's search query.
67+
* @returns {string} A JSON string containing the AI's response, the current date, and a trace ID.
68+
*/
69+
getDataBricksModelResponse: function(search) {
70+
try {
71+
var inputs = {};
72+
// Maps the search query to the input expected by the flow action.
73+
inputs['search_query'] = search;
74+
75+
// Executes the specified Flow Designer action with the provided inputs.
76+
// The action is run in the foreground, meaning the script will wait for a response.
77+
var result = sn_fd.FlowAPI.getRunner().action('global.genai_action').inForeground().withInputs(inputs).run();
78+
// Retrieves the outputs from the completed flow action.
79+
var outputs = result.getOutputs();
80+
81+
// Extracts the model output from the flow action outputs.
82+
var model_output = outputs['model_output'];
83+
// Attempts to parse and extract vector response data, though the variable is not used after this line.
84+
var databricks_vector_response = JSON.parse(model_output).databricks_output.trace.data.spans;
85+
86+
// Logs the raw response from the Databricks model for debugging.
87+
gs.info("Helper Results: Databricks flow action response: " + JSON.stringify(model_output));
88+
89+
var current_date = new GlideDateTime();
90+
var output = {};
91+
// Parses the model output to extract the AI's content.
92+
output.response = JSON.parse(model_output).choices[0].message.content;
93+
// Adds the current date to the output object.
94+
output.date = current_date.getDisplayValue();
95+
// Logs the trace ID for tracking purposes.
96+
gs.info("Helper Results: GEN AI flow action TraceID value: " + JSON.parse(model_output).id);
97+
// Adds the trace ID to the output object.
98+
output.traceID = JSON.parse(model_output).id;
99+
// Returns the constructed output object as a JSON string.
100+
return JSON.stringify(output);
101+
102+
} catch (ex) {
103+
// Catches any exceptions during the flow execution and logs an error.
104+
var message = ex.getMessage();
105+
gs.error(message);
106+
}
107+
},
108+
109+
type: 'GetServiceDeskAgentHelpAIUtil'
110+
});

0 commit comments

Comments
 (0)