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
Teja11
Giga Guru

great

Nandeesh
Tera Contributor

Hi Pavan,

i'm facing below issue, could you please help.

 

Below is the Response :

 

  {
"error": {
"message": "You exceeded your current quota, please check your plan and billing details.",
"type": "insufficient_quota",
"param": null,
"code": null
}
}
Pavankumar_1
Mega Patron

Hi @Nandeesh ,

For free trail may be it reached Maximum monthly spend for the API. Create new account use below link.

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

Nandeesh
Tera Contributor

Thank you @Pavankumar_1 , I created new account and that worked.

Tejas Chaudhar2
Tera Contributor

 @Pavankumar4 ,

In my case response Body is coming correct,

But in custom table fields are not set/auto populated.

Pavankumar_1
Mega Patron

Hi @Tejas Chaudhar2 ,

You are getting the response body just need to get answer from it.

Have you verified the backend field names? 

Tejas123
Tera Contributor

Hi  @Pavankumar4 ,

Screenshot 1.pngScreenshot 2.png

I have verified the backend field names, it's correct.

kumar22
Tera Contributor

Hi Pavan, thanks for sharing this!

I'm facing the below issue, could you please help?

 

Below is the Response :

{
"error": {
"message": "'100' is not of type 'integer' - 'max_tokens'",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

Pavankumar_1
Mega Patron

Hi @kumar22 ,

can you share the screenshots of your business rule script and content in REST message?

kumar22
Tera Contributor

Hi Pavan, 

Below are the screenshots:

REST message

kumar22_0-1676611273310.png

POST method

kumar22_2-1676611515877.png

BR

kumar22_1-1676611458579.png

 

Thanks!!

 

 

 

Pavankumar_1
Mega Patron

Hi @kumar22 ,

max_tokens, temperature are not strings. Those numbers we are giving on business rule.

You can given quotes on post method for those use as below or remove quotes on your content. Then it will work.

{
"model": "${model}",
"prompt": "${prompt}",
"max_tokens": ${max_tokens},
"temperature": ${temperature}
}
kumar22
Tera Contributor

@Pavankumar_1  its worked now, thank you!

Pavankumar_1
Mega Patron

@kumar22  good to know.

HugoFirst
Kilo Sage

This is very cool!  I was able to use this in a UI Page to accept a question in a form and present the response.

Thanks for  giving me the technical chops and  inspiring me to do it.

Pavankumar_1
Mega Patron

@HugoFirst  good to know that it helped you.

Bradley Bush
Mega Sage

YE

Karishma Dubey
Tera Expert

Hi @Pavankumar_1 ,

I am not getting anything in the response.

KarishmaDubey_0-1678099522097.pngKarishmaDubey_1-1678099604160.pngKarishmaDubey_2-1678099620686.pngKarishmaDubey_3-1678099797572.png

giving me below error.

KarishmaDubey_0-1678101139183.png

 

 

Pavankumar_1
Mega Patron

Hi @Karishma Dubey ,

your HTTP method is not POST try to replace with askChatGPT in business rule as below.

 var r = new sn_ws.RESTMessageV2('ChatGPT', 'askChatGPT');//give your rest mesage name and method name

 

Karishma Dubey
Tera Expert

@Pavankumar_1 ,

After changing method names it worked. Thanks for the help.

Pavankumar_1
Mega Patron

@Karishma Dubey  great to know😊.

Nag9
Tera Expert

Hi @Pavankumar_1 ,

I have tried all the steps that you have provided. While I'm creating a record chatgpt table, Im not getting any response or output after saving the record. Can you help me on this?

Not getting errors also

 

 

Nag9
Tera Expert

Here is the script files

 

Business rules.pngChatGpt Rest message.pnghttp.png

 

Pavankumar_1
Mega Patron

Hi @Nag9 ,

URL which is on your POST method is incorrect.

Use below URL:

https://api.openai.com/v1/completions

Nag9
Tera Expert

@Pavankumar_1 

 

Tried with given URL Pavan.  Still its not working 

 

Pavankumar_1
Mega Patron

Hi @Nag9 ,

can you share the error?

Nag9
Tera Expert

@Pavankumar_1 - I'm not getting any errors. While I'm trying to create a record on the table and save it, it's not generating any responses or a status code.

 

tiagomacul
Giga Sage
Pavankumar_1
Mega Patron

Hi @Nag9 ,

You are using Content-type can you use this Content-Type on REST message HTTP request?

If still not resolved try to use logs t check the results. It will help us to identify the issue.

akshay153
Tera Explorer

Hi @Pavankumar_1,

Could you please hlep me, I'm not getting any errors. While I'm trying to create a record on the table and save it, it's not generating any responses or a status code

 

 

akshay153
Tera Explorer

Here is the error code

"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

Pavankumar_1
Mega Patron

Hi @akshay153 ,

can you share the POST method content and business rule screenshots?

Garima13
Tera Contributor

Hi @Pavankumar_1 ,

 

I am getting the same error- "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)"Screenshot 2023-04-06 at 7.27.17 PM.pngScreenshot 2023-04-06 at 7.27.26 PM.pngScreenshot 2023-04-06 at 7.28.35 PM.png"

Please find the screenshots.

Pavankumar_1
Mega Patron

Hi @Garima13 ,

on Business rule change add below line. Chat GPT is your rest message I saw there is a space. 

Rest message should be same name as you defined.

var r = new sn_ws.RESTMessageV2('Chat GPT', 'POST');

 

Garima13
Tera Contributor

@Pavankumar_1 Yes I had caught that and made the change, still the error is coming.

 

Screenshot 2023-04-06 at 10.07.52 PM.pngScreenshot 2023-04-06 at 10.08.04 PM.png

akshay153
Tera Explorer

Hi @Garima13,

 

Please manually enter below payload in HTTP Query Parameter speciallt for "prompt", I was facing same issue when i have copied the below payload.

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

Also please do the same for your Business rule line no. 6. Kindly re enter that line manually. Hope that will work for you.

Garima13
Tera Contributor

@akshay153 ,

Made the change and ran the test again. It still gives out the same error.

Garima13
Tera Contributor

@Pavankumar_1 Thanks for your help. 

Its working fine now. 

rajamma
Tera Contributor

Hi Pavan Kumar,

 

It is working but i didn't get any statuscode and i am getting result after double saving.

 

rajamma_0-1680948691321.png

 

Pavankumar_1
Mega Patron

Hi @rajamma ,

response.getStatusCode(); // it will give the status code 

have you using this on business rule ?

can you share your script that you used on business rule?

Community Alums
Not applicable

Hello @Pavankumar_1 !

Thank you for the interesting article!

I am neither getting any output nor any error in the logs. Attached the screenshots of REST Message, POST method, BR and table configurations. Please help me where I am making a mistake!


AlfrinAJ_0-1681147299343.pngAlfrinAJ_1-1681147327911.pngAlfrinAJ_2-1681147365992.png

AlfrinAJ_3-1681147444334.png

 

Output (Not getting any response):

AlfrinAJ_4-1681147463885.png

AlfrinAJ_5-1681147481598.png
Thank you!

 

priyasunku
Kilo Sage

Hi @Pavankumar_1 

 

I am getting below error. can you help me here

 

We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)",
"type": "invalid_request_error",
"param": null,
"code": null

KalyaniShaha17
Tera Contributor

Hi @Pavankumar_1 , @Garima13 ,

 

I am getting this error and not able to test the connection. GET method is working fine for me but for POST getting error.

Can you please help me here?

 

Waiting for your response.

KalyaniShaha17_0-1682080785478.png

 

priyasunku
Kilo Sage

@KalyaniShaha17  I also faced same issue. try adding content manually.

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful

Pavankumar_1
Mega Patron

Hi @KalyaniShaha17 ,

can you give some Test values on variables and then click test?

Refer below

Screenshot (762).png

Sandeep87
Tera Contributor

Hi @Pavankumar_1

I am getting this response. 

Response Body {
"error": {
"message": "'messages' is a required property",
"type": "invalid_request_error",
"param": null,
"code": null
}}

any suggestions why I am getting this error. Thanks in advance

Pavankumar_1
Mega Patron

Hi @Sandeep87 ,

can you share your rest Message content and business rule screenshots?

Sandeep87
Tera Contributor

@Pavankumar_1 

yeah, below are the screenshots.

Rest Message.pngBusiness rule.pngBusiness rule script.png 

Pavankumar_1
Mega Patron

Hi @Sandeep87 ,

can you try to Test in the post message using variable substitutions?

Click on Auto generate variables and give sample values like below and click on test.

Then See the result.

Screenshot (768).png

Sandeep87
Tera Contributor

@Pavankumar_1 

I have tried using variable substitution but still the same error. PFAerror.pngERROR 2.png

Pavankumar_1
Mega Patron

Hi @Sandeep87 ,

can you check your messages which is on top right side of community I have texted you to connect?

 

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