(C) Questetra, Inc. (MIT License)
2023-08-10
3
2
https://support.questetra.com/bpmn-icons/service-task-openai-chatgpt-chat/
https://support.questetra.com/ja/bpmn-icons/service-task-openai-chatgpt-chat/
This item sends a message to OpenAI ChatGPT and stores the
response in the specified data item.
この工程は、OpenAI ChatGPT へメッセージを送信し、回答をデータ項目に保存します。
-
-
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAABclJREFUWEfN
V3tQlFUU/12esiCwrCIhKugiL0VAUASc8pEZmI2K5Wu3MRtDK5Wm0RlndAEbK2VEa/JRYQZkmu9I
a/KVvBQfgOKKDwhwDUXQXcTl4QK3uRd2XdhvgbFm6P73fd895/zOOb/z+Aj6+JDe2nfflSBqtMds
2kYngdBgAJ4AEbfLUzWAClBSSCzIWTstDle9n9DQG909Ahj4vcJNZ4k1hCCOAv16qbSJUuy0bsUX
NYsTH3Qn0y0AcZoijhKaDBD73hg2vUO1hJJP1PLEnebkzQJwSk/YTkCXvZjhzlIUZEedLGG5kC5B
AE7p6/cSkPn/hXG9Dgr6U50saUFXnSYAevL8Q78IvD08EEMdnKFra8PVx/exveQ8TleV9ohXKBKd
ALTnHDuENDlY2yI1ag4mu0tR3ViPwkdVcLLphzEuL8ECBPv+ugoLQnC59m9kqkrwVNcsCIhQLDPm
hAEAZ7sVLTVHuM1h0ZB7j8WxSiU+vnicG3AXOWJj6HTEDPGFFbEwGNQ8a0Tq7Uv4tOiMAAiqtW4h
Un11GAA4pylSQLCqqwTzfIV/BN6RjsWDxnrEnNzDja8KiMJyvwkQ29ohr7oSm4vPIae6AjOG+GF1
4MvwdpRge8kFbCg6LVAc2KqRJ8azDxwAbzIi+qhrnU9198bG0NcgdZSAgCC9tABppQXYFBaNQBc3
XFdXcwMs/+yuSqvBrboa+DgNRGpULBysbfBeziFcrr3XCQQBmuwaiIQ1Kw5AnJGwiFKabnxLr2Sw
vSMOlhdj3vAxOFKpRO7DSiiCpmDPnSvYVHwOM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UYM8kRQyjU9Ey449oLFFh19VJYZdwZz7ZhcSvUBPK5mxYjYxR4vd8Kyt
FWful3XjdbtUjyvZcxB9uJS+SCTMs82853qZ/++PiR5hn/6aGYe2z35Oe8rvv/3+D7A/zj/ftsWS
AAAAAElFTkSuQmCC
{
const authSetting = httpClient.createAuthSettingToken('ChatGPT', authKey);
configs.putObject('conf_Auth', authSetting);
configs.put('conf_Model', model);
configs.put('conf_GPT_Role', gptRole);
configs.put('conf_Message1', message);
const answerDef = engine.createDataDefinition('回答', 1, 'q_answer', 'STRING_TEXTAREA');
engine.setData(answerDef, '事前文字列');
configs.putObject('conf_Answer1', answerDef);
return answerDef;
};
/**
* 異常系のテスト
* @param errorMsg
*/
const assertError = (errorMsg) => {
try {
main();
fail();
} catch (e) {
expect(e.message).toEqual(errorMsg);
}
};
/**
* メッセージが空
*/
test('Message is empty', () => {
prepareConfigs('key', 'gpt-4', '', '');
assertError('User Message is empty.');
});
/**
* API リクエストのテスト
* @param {Object} request
* @param request.url
* @param request.method
* @param request.contentType
* @param request.body
* @param {String} authKey
* @param {String} model
* @param {String} gptRole
* @param {String} message
*/
const assertRequest = ({
url,
method,
contentType,
body
}, authKey, model, gptRole, message) => {
expect(url).toEqual("https://api.openai.com/v1/chat/completions");
expect(method).toEqual('POST');
expect(contentType).toEqual('application/json');
const bodyObj = JSON.parse(body);
expect(bodyObj.model).toEqual(model);
if (gptRole === '') {
expect(bodyObj.messages.length).toEqual(1);
} else {
expect(bodyObj.messages.length).toEqual(2);
expect(bodyObj.messages[0]).toEqual({
role: 'system',
content: gptRole
});
}
const userMessage = bodyObj.messages[bodyObj.messages.length - 1];
expect(userMessage).toEqual({
role: 'user',
content: message
});
};
/**
* API リクエストでエラー
*/
test('Fail to request', () => {
prepareConfigs('key2', 'gpt-3.5-turbo', '', 'こんにちは');
httpClient.setRequestHandler((request) => {
assertRequest(request, 'key2', 'gpt-3.5-turbo', '', 'こんにちは');
return httpClient.createHttpResponse(400, 'application/json', '{}');
});
assertError('Failed to request. status: 400');
});
/**
* API のレスポンスボディを作成
* @param {String} answerText
* @returns {String} response
*/
const createResponse = (answerText) => {
const responseObj = {
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": answerText,
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
;
return JSON.stringify(responseObj);
};
/**
* 成功
*/
test('Success to request', () => {
const answerDef = prepareConfigs('key3', 'gpt-4', 'Robot', 'Hello World');
httpClient.setRequestHandler(request => {
assertRequest(request, 'key3', 'gpt-4', 'Robot', 'Hello World');
return httpClient.createHttpResponse(200, 'application/json', createResponse("\n\nHello there, how may I assist you today?"));
});
main();
// データ項目の値をチェック
expect(engine.findData(answerDef)).toEqual("\n\nHello there, how may I assist you today?");
});
]]>