ServiceNow ChatGPT Integration
Photo by Jess Bailey on Unsplash

ServiceNow ChatGPT Integration

ServiceNow ChatGPT Integration

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]        

要查看或添加评论,请登录

Shaji Kalidasan的更多文章

  • Flashes from Sri Sarada Devi

    Flashes from Sri Sarada Devi

    Flashes from Sri Sarada Devi Always discriminate. Try to realize that the outside object which is attracting your mind…

  • A Few Thinkers on Education

    A Few Thinkers on Education

    A Few Thinkers on Education The first rule of education, in all lands is never to say anything offensive to anyone…

  • ServiceNow MID Server Primer

    ServiceNow MID Server Primer

    The Management, Instrumentation, and Discovery (MID) Server is a Java application that runs as a Windows service or…

  • The art of Samadhi and Flow

    The art of Samadhi and Flow

    Beloved friends, I have prepared a summary of what is called a great mental state called "flow" (aka "In The Zone")…

    1 条评论
  • Creating intellectual property within an A4 sheet

    Creating intellectual property within an A4 sheet

    All this beautiful world is very good because it gives us time and opportunity to help others. - Swami Vivekananda Goal…

  • App to retrieve real-time stock prices and market data off the internet

    App to retrieve real-time stock prices and market data off the internet

    This Perl script shows one way to retrieve real-time stock prices and market data off the internet for stocks traded on…

    13 条评论
  • Program to find duplicate files on your hard drive

    Program to find duplicate files on your hard drive

    The core principle behind this article is to show how we can create intellectual property with less than or equal to…

    5 条评论

社区洞察

其他会员也浏览了