- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-04-2023 07:54 AM
It's time to integrate ServiceNow with ChatGPT. Hope it will help you.
Reference links:
- Endpoint, headers and JSON link: https://platform.openai.com/docs/api-reference/making-requests
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:
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.
Step 2: REST Message and Post HTTP method with No authentication on
Note: Authorization should be Bearer YOUR_API_KEY
Rest Message:
POST HTTP Method:
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:
Results: when you give question and Save on ChatGPT table then we will get the response.
Will get Response as below.
To make to easy and understandable to everyone, I have provided the all the details with screenshots.
- 46,211 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
great
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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 } } |
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Nandeesh ,
For free trail may be it reached Maximum monthly spend for the API. Create new account use below link.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you @Pavankumar_1 , I created new account and that worked.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
In my case response Body is coming correct,
But in custom table fields are not set/auto populated.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Tejas Chaudhar2 ,
You are getting the response body just need to get answer from it.
Have you verified the backend field names?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
}
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @kumar22 ,
can you share the screenshots of your business rule script and content in REST message?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Pavan,
Below are the screenshots:
REST message
POST method
BR
Thanks!!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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}
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Pavankumar_1 its worked now, thank you!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@kumar22 good to know.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@HugoFirst good to know that it helped you.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
YE
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
After changing method names it worked. Thanks for the help.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Karishma Dubey great to know😊.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Here is the script files
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Nag9 ,
can you share the error?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@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.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Pavankumar_1 well done!
I linked your post here,
https://www.servicenow.com/community/br-brazil/como-fazer-a-integra%C3%A7%C3%A3o-entre-servicenow-am...
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
}
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @akshay153 ,
can you share the POST method content and business rule screenshots?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.)""
Please find the screenshots.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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');
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Made the change and ran the test again. It still gives out the same error.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Pavankumar_1 Thanks for your help.
Its working fine now.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Pavan Kumar,
It is working but i didn't get any statuscode and i am getting result after double saving.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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!
Output (Not getting any response):
Thank you!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@KalyaniShaha17 I also faced same issue. try adding content manually.
If my answer solved your issue, please mark my answer as ✅Correct & 👍Helpful
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Sandeep87 ,
can you share your rest Message content and business rule screenshots?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I have tried using variable substitution but still the same error. PFA
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Sandeep87 ,
can you check your messages which is on top right side of community I have texted you to connect?