Note: I used the ServiceNow background script to execute this script.
[SCRIPT]
serviceNowChatGPTIntegration(4000, 0);
function serviceNowChatGPTIntegration(max_tokens, temperature) {
? ? try {
? ? //ChatGPT API Key, is used to authenticate and authorize access to the GPT-3 model, so that only authorized applications can use the model's capabilities.
? ? ? ? var apiKey = gs.getProperty("chatgpt.api_key") || "Empty";
? ? ? ? if (apiKey === "Empty") {
? ? ? ? ? ? throw new Error('API key is null or undefined, please ensure that you provide a valid API key');
? ? ? ? }
? ? ? ? var query = [
? ? ? ? ? ? "Famous quotes of Sri Sarada Devi",
? ? ? ? ? ? "Famous quotes of Mahatma Gandhi",
? ? ? ? ? ? "Famous quotes of Sri Ramakrishna Paramahamsa",
? ? ? ? ? ? "Famous quotes of Swami Vivekananda",
? ? ? ? ? ? "Famous quotes of Sri Ramana Maharshi",
? ? ? ? ? ? "Famous quotes of Rabindranath Tagore",
? ? ? ? ? ? "Famous quotes of Paramahansa Yogananda",
? ? ? ? ? ? "Famous quotes of Mother Teresa",
? ? ? ? ? ? "Famous quotes of Abdul Kalam"
? ? ? ? ];
? ? ? ? var prompt = query[Math.floor(Math.random() * query.length)];
? ? ? ? var request = new sn_ws.RESTMessageV2();
? ? ? ? request.setEndpoint("https://api.openai.com/v1/completions");
? ? ? ? request.setHttpMethod("POST");
? ? ? ? request.setRequestHeader("Content-Type", "application/json");
? ? ? ? request.setRequestHeader("Authorization", "Bearer " + apiKey);
? ? ? ? /*
? ? ? ? ?'prompt': is the input text or question that is passed to the GPT-3 model for generating an answer or response.
? ? ? ? ?'model': is used to specify which GPT-3 model should be used to generate the response. In this case, the 'text-davinci-003' model is used.
? ? ? ? ?'max_tokens': is an optional parameter used to specify the maximum number of tokens (i.e. words or word pieces) that the model should generate in its response.
? ? ? ? ?'temperature': is a parameter used to control the randomness of the model's generated text. A lower temperature will result in more conservative and predictable responses, while a higher temperature will result in more creative and varied responses.
? ? ? ? */
? ? ? ? request.setRequestBody(JSON.stringify({
? ? ? ? ? ? "prompt": prompt,
? ? ? ? ? ? "model": "text-davinci-003",
? ? ? ? ? ? "max_tokens": max_tokens,
? ? ? ? ? ? "temperature": temperature
? ? ? ? }));
? ? ? ? //'response': will give the HTTP response of the request, which contains the body of the response and the status code.
? ? ? ? var response = request.execute(); //For asynchronous call you can use request.executeAsync()
? ? ? ? //'responseBody': will parse the JSON response of the request and give the answer in the form of JSON object.
? ? ? ? var responseBody = JSON.parse(response.getBody());
? ? ? ? //'httpStatus': will give the status of the HTTP request, whether it is a success or failure.
? ? ? ? var httpStatus = response.getStatusCode();
? ? ? ? // Check the HTTP status code
? ? ? ? if (httpStatus == 200) {
? ? ? ? ? ? // Process the successful response
? ? ? ? ? ? var completions = responseBody.choices;
? ? ? ? ? ? if (completions.length > 0) {
? ? ? ? ? ? ? ? var choice = completions[0];
? ? ? ? ? ? ? ? if (choice.hasOwnProperty('text')) {
? ? ? ? ? ? ? ? ? ? // Assign the text response to answer
? ? ? ? ? ? ? ? ? ? answer = choice.text;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? // Handle the case where the "text" field does not exist
? ? ? ? ? ? ? ? ? ? answer = 'The API response did not include the text attribute';
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? // Handle the case where the "choices" array is empty
? ? ? ? ? ? ? ? answer = 'The API response did not include the choices array';
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? // Set answer to "The API failed" with the error code / message
? ? ? ? ? ? answer = "The API failed with error code " + httpStatus + ": " + responseBody;
? ? ? ? }
? ? } catch (ex) {
? ? ? ? answer = "Exception: " + ex.message;
? ? } finally {
? ? ? ? //Print the answer (response from ChatGPT)
? ? ? ? gs.print(answer);
? ? }
}
[/SCRIPT]
[OUTPUT]
*** Script:?
1. "Where the mind is without fear and the head is held high; Where knowledge is free; Where the world has not been broken up into fragments by narrow domestic walls."
2. "Let your life lightly dance on the edges of Time like dew on the tip of a leaf."
3. "If you cry because the sun has gone out of your life, your tears will prevent you from seeing the stars."
4. "Clouds come floating into my life, no longer to carry rain or usher storm, but to add color to my sunset sky."
5. "I slept and dreamt that life was joy. I awoke and saw that life was service. I acted and behold, service was joy."
[/OUTPUT]