|
| 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. |
0 commit comments