How to call ChatGPT from Maximo
This blog has a basic implementation for using chatGPT in Maximo through an OpenAI API call.
To Integrate ChatGPT with Maximo, we will have to create a button like “Ask ChatGPT“ in the below image that will open a dialog box “New Chat“. The dialog box will contain a text field for user input, a multi-line text box for response and a send message button. At the click of the send button, the automation script will run, which will call the OpenAI API and retrieve the response.
First, Let’s look at the prerequisites. To call ChatGPT from your code, you need an API key from OpenAI.
Create the OpenAI API Key
OpenAI offers free API keys but most users get an error in API calls. Somehow when you set up a paid account, the error goes away. It looks like they are prioritizing API calls from a paid account to reduce spamming and overload.
1. Head to platform.openai.com/signup and create a free account. If you already have an OpenAI account, simply log in.
2. Next, click on your profile in the top-right corner and select “View API keys” from the drop-down menu.
3. Here, click on “Create new secret key” and copy the API key. Do note that you can’t copy or view the entire API key later. So it’s strongly recommended to copy and paste the API key to a Notepad file immediately.
4. Also, do not share or display the API key in public. It’s a private key meant only for access to your account. You can also delete API keys and create multiple private keys (up to five).
If you had to set up a paid account make sure you put the usage limits under the billing section.
Automation Script
I am assuming that you can invoke an action launch point automation script from a push button. Let’s look at the Jython code.
from com.ibm.json.java import JSONObject
from psdi.server import *
from psdi.mbo import MboConstants
try:
# Define the API endpoint URL
url = "https://api.openai.com/v1/completions"
# Define the request headers with YOUR-API-KEY
headers = 'Content-type:application/json,Authorization:Bearer YOUR-API-KEY';
# Define the request body
jsonBody = JSONObject()
jsonBody.put("prompt","The following text after colon is a question from user related to Maximo. Please answer it as accurately as possible: "+mbo.getString("CXINPUT"))
jsonBody.put("max_tokens",100)
jsonBody.put("model","text-davinci-003")
jsonBody.put("temperature",0.1)
#Call Open AI API
responseJson = service.httppostasjson(url,None,None,headers,jsonBody)
#Display Response
mbo.setValue("CXOUTPUT",responseJson["choices"][0]["text"],MboConstants.NOVALIDATION | MboConstants.NOACCESSCHECK)
except Exception as e:
raise Exception(str(e), None, None);
The url variable contains the API endpoint URL for the OpenAI API.
The headers variable contains the request headers with the API key. Replace YOUR-API-KEY with the OpenAI API key that you created previously.
The jsonBody variable contains the request body with the user input prompt, number of tokens, model to use, and temperature. you can limit response size to save cost by limiting tokens through max_tokens.
The model ‘text-davinci-003’
used here has a cost of $0.0200 / 1K tokens. You can also use a relatively cheap model like Ada.
The responseJson variable contains the response received from the API call just like the sample response JSON below.
{
created: 1681559906,
usage: {"completion_tokens":10,"prompt_tokens":1,"total_tokens":11},
model: davinci,
id: cmpl-75YpuIVkCR3oAP7WK8a9RbBTXo8Xc,
choices: [
{"finish_reason":"length",
"index":0,
"text":" response message",
"logprobs":null}],
object: text_completion
}
We are only interested in the value of the ‘text’ key field. So we ignored everything else and passed that to CXOUTPUT field.
responseJson["choices"][0]["text"]
Conclusion
In this blog post, we have seen how to call ChatGPT from Maximo using an automation script. With the power of OpenAI Models, we can optimize various business processes by accessing valuable insights from Maximo data.
Hi Himanshu,
When I click button to call automation script, maximo raise an error like this
BMXAA1477E - The connection failed to the HTTP handler for the endpoint. Review the error and server log flies for information to indicate the cause of the issue, for example, incorrect properties in the DefaultHTTPExit.java handler class.
com.ibm.jsse2.util.h: PKIX path building failed: com.ibm.security.cert.IBMCertPathBuilderException: unable to find valid certification path to requested target.
Could you please give me suggestion for this error?
Thanks.
IS IT RESOLVED?