Pavankumar_1
Mega Patron

It's time to integrate ServiceNow with ChatGPT. Hope it will help you.

Reference links:

Refer below

curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}'
  • API key Creation Link: Need to create API key using below link and use it on header.

         https://platform.openai.com/account/api-keys

Refer below:

API key.png

Below are the 3 steps required to complete this integration.

1. Create a ChatGPT table with Question, Answer, Request, Response, Status code are string fields.

2. Create REST Message and Post HTTP method to update the data on ServiceNow.

3. Create Async business rule and use given script and give your names.

 

Step 1. Create ChatGPT table and below are the fields which I have created. Create your own fields.

ChatGPT fields.png

Step 2: REST Message and Post HTTP method with No authentication on 

Note: Authorization should be  Bearer YOUR_API_KEY

Rest Message:

Screenshot (645).png

POST HTTP Method:

Screenshot (646).png

Content:

{
"model": "${model}",
"prompt": "${prompt}",
"max_tokens": ${max_tokens},
"temperature": ${temperature}
}

Step 3. Async Business rule insert/update on the ChatGPT table and give specific condition as below example.

Ex: Question changes(condition)

Script:

(function executeRule(current, previous /*null when async*/ ) {
    try {
       
        var r = new sn_ws.RESTMessageV2('ChatGPT', 'POST');//give your rest mesage name and method name
        r.setStringParameterNoEscape('model', "text-davinci-003");
        r.setStringParameterNoEscape('prompt', current.u_question);//give your field name
        r.setStringParameterNoEscape('max_tokens', 100);
        r.setStringParameterNoEscape('temperature', 0);

        var response = r.execute();
        var responseBody = response.getBody();//it will give the response body
        var code = response.getStatusCode();//get status code

        var responseObj = JSON.parse(responseBody);
        current.u_request = r.getRequestBody();//it will give the request body
        current.u_response = responseBody;
        current.u_status_code = code;
        current.u_answer = responseObj.choices[0].text;//it will give the text from response
        current.setWorkflow(false);
        current.update();
        current.setWorkflow(true);

    } catch (ex) {
        var message = ex.message;
    }

})(current, previous);

Screenshot:

BR.png

Results: when you give question and Save on ChatGPT table then we will get the response. 

Result 1.png

Will get Response as below.

Result 2.png

 

To make to easy and understandable to everyone, I have provided the all the details with screenshots.

 

Comments
Mike322
Tera Contributor

Maybe it's an idea to post the update set with the configuration and a little instruction on how to personalize it for those who deploy said update set (like pasting your own API key on the appropriate places)?

Sahil Khan
Tera Contributor

Please share technical document if you have

sushma9
Tera Contributor

Hi @Pavankumar_1 

 I have tried integrating chat gpt with service now  When i tested it i am getting below response as  error 

sushma9_0-1688232394765.png

 

Please find the Rest Message and Http Method and Business rule screen shots below.

 

Rest message :

sushma9_1-1688232458263.png

 

HTTP Method : 

sushma9_2-1688232502904.png

 

 

BR:

sushma9_3-1688232572933.png

 

johndoh
Mega Sage

Hello @sushma9 ,

 

You need to populate the values for the variables you added in your HTTP Method screenshot:

 

Generate the variables and populate similar values as below:

johndoh_0-1688233170676.png

 

Populate the variables with your requirements or anything for that matter as the BR you created will override but needs a sample:

johndoh_2-1688233244456.png

 

 

 

johndoh
Mega Sage

You can find which model is appropriate for you here Models - OpenAI API

sushma9
Tera Contributor

Hi @johndoh 

 I am going to use the model as  dynamic model and i am defining them in BR. Do i Need to again use these values  here and i have given those values  here and tried as well but i am getting below error message response.

sushma9_0-1688234622634.png

 

johndoh
Mega Sage

@sushma9 remove the quotes around 100 for the max tokens and the quotes around the 0 in temperature and test again. You should be successful then 🙂

Pavankumar_1
Mega Patron

Hi @sushma9 ,

max_tokens and temperature are integers give as below it will work not as string.

 

{
"model": "text-davinci-003",
"prompt": "What is Chat GPT?",
"max_tokens": 100,
"temperature": 0
}

niveditakumari
Mega Sage

Hi @Pavankumar_1

 

I have created API key with using https://platform.openai.com/account/api-keys

I'm creating ChatGPT table. 

Can you please tell about ChatGPT table and how to create that. Is this different table or same table. 

Please help me to start and achieve it. I'm really very interested to do this integration using ChatGPT. 

 

Regards, 

Nivedita

 

 

Pavankumar_1
Mega Patron

Hi @niveditakumari ,

just create any test table and create those fields and use it.

 

 

niveditakumari
Mega Sage

Hi @Pavankumar_1

 

Can we connect? 

 

Regards, 

Nivedita

 

 

niveditakumari
Mega Sage

Hi @Pavankumar_1

 

It worked. 

Thank you so much for this useful article. 

 

Regards, 

Nivedita

 

 

Gayathri Anba
Tera Explorer

Is this possible that we can integrate this without global search/outbound calls here? Want to answer inside servicenow instance by using chatGPT

RodGallegos
Tera Guru

Next step, plug VA into the mix! Nice post!

Pavankumar_1
Mega Patron

Hi @Gayathri Anba ,

without rest call how ServiceNow will get the result?

 

Pavankumar_1
Mega Patron

Hi @RodGallegos ,

Thanks @RodGallegos . I tried with VA as well.

Gayathri Anba
Tera Explorer

Hi @Pavankumar_1 , I tried with VA it is working with REST calls. But I need to use CHATGPT for Private calls(basic contextual search inside the private instance) not for Global search. Is that Possible ? 

taofeek93
Tera Contributor

Hi @Pavankumar_1 , I keep getting HTTP Status 400 when I tested it. 

 

Screenshot 2023-11-18 at 6.02.16 PM.png

Screenshot 2023-11-18 at 6.04.32 PM.png

Pavankumar_1
Mega Patron

Hi @taofeek93 ,

you are not proving any test value on variable so try to give some values some do test.

please refer below screen and try again

Pavankumar_1_0-1700377477164.png

you will get the response as below

Screenshot 2023-11-19 at 12.36.46 PM.png

Fatima Zahra At
Tera Contributor

@Pavankumar_1  

when I tested with the values i got this error message. can you help me please to solve it 

FatimaZahraAt_0-1702573985286.png

 

Pavankumar_1
Mega Patron

Hi @Fatima Zahra At ,

you have exceeded you limit. Please check below link for limits

https://platform.openai.com/account/limits

You have to buy credits or try with different account.

Danny Mortensen
Tera Contributor
gpt-3.5-turbo

Thanks for this example Pavan. It is a great starting point for tinkering with Generative AI. This is my first integration attempt with ServiceNow .. I've not succeeded yet but your example is pretty clear.
I'm not getting a message to say I've exceeded my current quota but I assume that is possibly because I need to pay now or create a new account. I'll give that a go.

Just FYI .. InstructGPT model "text-davinci-003" is deprecated and replaced with "gpt-3.5-turbo-instruct". There might be more suitable models for your purpose so check them out here: https://platform.openai.com/docs/models

senator013
Tera Explorer

What is the business value of this integration?  It's not clear to me why I would do this.

SylvainHauserN2
ServiceNow Employee
ServiceNow Employee

For the ones whowould like to make it work with gpt-3.5-turbo, here is the updated script after having updated the endpoints to "https://api.openai.com/v1/chat/completions":

 

 

(function executeRule(current, previous /*null when async*/ ) {
    try {
       
        var r = new sn_ws.RESTMessageV2('ChatGPT', 'POST');//give your rest mesage name and method name
        r.setStringParameterNoEscape('model', "gpt-3.5-turbo"); //https://platform.openai.com/docs/guides/text-generation

		var messages = [
            { role: "user", content: current.u_question.toString() }   // Add the current question
        ];

		var requestBody = {
            model: "gpt-3.5-turbo",
            max_tokens: 100,
            temperature: 0,
            messages: messages // Adding the messages array to the request body
        };

		r.setRequestBody(JSON.stringify(requestBody));
		
		r.setStringParameterNoEscape('max_tokens', 100);
        r.setStringParameterNoEscape('temperature', 0);

        var response = r.execute();
        var responseBody = response.getBody();//it will give the response body

        var code = response.getStatusCode();//get status code

        var responseObj = JSON.parse(responseBody);
        current.u_request = r.getRequestBody();//it will give the request body

        current.u_response = responseBody;
        current.u_status_code = code;
        //current.u_answer = responseObj.choices[0].text;//it will give the text from response
		current.u_answer = responseObj.choices[0].message.content;
        current.setWorkflow(false);
        current.update();
        current.setWorkflow(true);

    } catch (ex) {
        var message = ex.message;
    }

})(current, previous);

 

Souvick6917
Tera Contributor

Hello @Pavankumar_1 

First , thank you for this article. But with everything configured as required I am getting below error which is somewhat new to me. Can you please help me with the issue:

 

{
"error": {
"message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}

 

Screenshots are also added.Billing error.png

fathahhkf
Tera Contributor

@Pavankumar_1& everyone,

 

New update on the post.

Replace the below to make integration works,

End Point: https://api.openai.com/v1/chat/completions

Content:

{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "user",
        "content": "${content}"
      }
    ]
  }

 

Thanks,

Abdul Fathah

MadhusaiK
Tera Contributor

MadhusaiK_0-1738661414215.pngMadhusaiK_1-1738661427282.pngMadhusaiK_2-1738661460559.pngMadhusaiK_3-1738661474056.png

MadhusaiK_4-1738661493303.png


i have followed the above steps you mentioned and still unable to get response 
could you please help me with that 
i am attaching the configuration screenshots.

MadhusaiK
Tera Contributor

MadhusaiK_0-1738662840296.png

this error i am getting

Aniket Shukla
Tera Contributor

Hi @Pavankumar_1 , Needed help  i am getting this error.

AniketShukla_0-1747828351003.png

 and also i have attached 4 scrennshot of rest ,http method with br .

AniketShukla_1-1747828418987.png

AniketShukla_2-1747828441602.pngAniketShukla_3-1747828467176.png

 

AniketShukla_4-1747828502394.png

Please Help @Pavankumar_1 

Pavankumar_1
Mega Patron

@Aniket Shukla 

Maybe that is related to the license, and you reached your limit. Try with other account

Version history
Last update:
‎02-04-2023 07:53 AM
Updated by:
Contributors