
OpenAI ChatGPT: Chat
This item sends a message to OpenAI ChatGPT and stores the response in the specified data item.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_Auth
- C1: Authorization Setting in which Authentication Key is set *
- conf_Model
- C2: Model *
- conf_GPT_Role
- C3: Role for ChatGPT#{EL}
- conf_Message1
- U1: User Message *#{EL}
- conf_Answer1
- A1: Data item to save response from ChatGPT *
Notes
- Authentication Key (API Key) can be created here
Capture

See Also
Script (click to open)
- An XML file that contains the code below is available to download
- chatgpt-chat-completion.xml (C) Questetra, Inc. (MIT License)
- If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step
function main() {
////// == Config Retrieving / 工程コンフィグの参照 ==
const auth = configs.getObject("conf_Auth"); /// REQUIRED
const model = configs.get('conf_Model');
// https://platform.openai.com/docs/guides/safety-best-practices
// Sending end-user IDs in your requests can be a useful tool to help OpenAI monitor and detect abuse.
const requestUser = "m" + processInstance.getProcessModelInfoId().toString();
const gptRole = configs.get("conf_GPT_Role"); // NotRequired
const message1 = configs.get("conf_Message1"); /// REQUIRED
if (message1 === "") {
throw new Error("User Message is empty.");
}
// const strParams = configs.get("StrConfA2"); // NotRequired
// const arrParams = strParams !== "" ? strParams.split("\n") : null;
//
// const numTemperature = isNaN(parseFloat(arrParams?.[0])) ? 1 : parseFloat(arrParams[0]);
// const numTopP = isNaN(parseFloat(arrParams?.[1])) ? 1 : parseFloat(arrParams[1]);
// const numPresPenalty = isNaN(parseFloat(arrParams?.[2])) ? 0 : parseFloat(arrParams[2]);
// const numFreqPenalty = isNaN(parseFloat(arrParams?.[3])) ? 0 : parseFloat(arrParams[3]);
//
// const strBias = configs.get("StrConfA3"); // NotRequired
// const arrBias = strBias !== "" ? strBias.split("\n") : null;
// const strChoises = configs.get("StrConfA4"); // NotRequired
// const numChoises = isNaN(parseInt(strChoises, 10)) ? 1 : parseInt(strChoises, 10);
// const strLimit = configs.get("StrConfA5"); // NotRequired
const numLimit = 2048;
// const strStops = configs.get("StrConfA6"); // NotRequired
// const arrStops = strStops !== "" ? strStops.split("\n") : null;
// const numPocketPromptTokens = configs.getObject("SelectConfC1"); // NotRequired
// const numPocketCompletionTokens = configs.getObject("SelectConfC2"); // NotRequired
// const numPocketTotalTokens = configs.getObject("SelectConfC3"); // NotRequired
////// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)
////// == Calculating / 演算 ==
//// OpenAI API > Documentation > API REFERENCE > CHAT
//// https://platform.openai.com/docs/api-reference/chat
/// prepare json
const requestJson = {
model,
messages: []
};
if (gptRole !== "") {
requestJson.messages.push({
role: 'system',
content: gptRole
});
}
// if (strLogPro1 !== "" && strLogCom1 !== "") {
// let objLogPro = {};
// objLogPro.role = "user";
// objLogPro.content = strLogPro1;
// requestJson.messages.push(objLogPro);
// let objLogCom = {};
// objLogCom.role = "assistant";
// objLogCom.content = strLogCom1;
// requestJson.messages.push(objLogCom);
// }
requestJson.messages.push({
role: "user",
content: message1
});
// if (arrParams?.[0] !== "") {
// requestJson.temperature = numTemperature;
// }
// if (arrParams?.[1] !== "") {
// requestJson.top_p = numTopP;
// }
// if (arrParams?.[2] !== "") {
// requestJson.presence_penalty = numPresPenalty;
// }
// if (arrParams?.[3] !== "") {
// requestJson.frequency_penalty = numFreqPenalty;
// }
requestJson.n = 1;
requestJson.max_tokens = numLimit;
requestJson.user = requestUser;
// if (arrStops !== null) {
// requestJson.stop = [];
// for (let i = 0; i < arrStops.length; i++) {
// if (arrStops[i] === "- - -") {
// requestJson.stop.push("\n");
// } else {
// requestJson.stop.push(arrStops[i]);
// }
// }
// }
// if (arrBias !== null) {
// requestJson.logit_bias = {};
// for (let i = 0; i < arrBias.length; i++) {
// let arrNumParts = arrBias[i].match(/-?\d+/g); // numbers (including with minus signs)
// if (arrNumParts.length >= 2) {
// requestJson.logit_bias[arrNumParts[0]] = Number(arrNumParts[1]);
// }
// }
// }
const response = httpClient.begin().authSetting(auth)
.body(JSON.stringify(requestJson), "application/json")
.post("https://api.openai.com/v1/chat/completions");
const responseCode = response.getStatusCode();
const responseBody = response.getResponseAsString();
engine.log(responseBody);
if (responseCode !== 200) {
throw new Error(`Failed to request. status: ${responseCode}`);
}
const responseJson = JSON.parse(responseBody);
saveData('conf_Answer1', responseJson.choices[0].message.content ?? '');
// if (numPocketPromptTokens !== null) {
// engine.setData(numPocketPromptTokens, new java.math.BigDecimal(
// responseJson.usage.prompt_tokens ?? 0
// ));
// }
// if (numPocketCompletionTokens !== null) {
// engine.setData(numPocketCompletionTokens, new java.math.BigDecimal(
// responseJson.usage.completion_tokens ?? 0
// ));
// }
// if (numPocketTotalTokens !== null) {
// engine.setData(numPocketTotalTokens, new java.math.BigDecimal(
// responseJson.usage.total_tokens ?? 0
// ));
// }
}
/**
* データ項目への保存
* @param configName
* @param data
*/
const saveData = (configName, data) => {
const def = configs.getObject(configName);
if (def === null) {
return;
}
engine.setData(def, data);
};