OpenAI: Chat, Start

OpenAI: Chat, Start

OpenAI: Chat, Start

OpenAI: Chat, 開始

Starts a conversation with the OpenAI API (ChatGPT). The model ID used is “gpt-3.5-turbo”. A response (COMPLETION) to an instruction question (PROMPT) is automatically obtained.

Auto Step icon
Configs for this Auto Step
AuthzConfU
U: Select HTTP_Authz Setting (Secret API Key as “Fixed Value”) *
StrConfA1
A1: Set Request Message PROMPT *#{EL}
SelectConfB1
B1: Select STRING that stores Response COMPLETION (update) *
SelectConfC1
C1: Select NUMERIC that stores PROMPT Tokens (update)
SelectConfC2
C2: Select NUMERIC that stores COMPLETION Tokens (update)
SelectConfC3
C3: Select NUMERIC that stores Total Tokens (update)
Script (click to open)
// GraalJS Script (engine type: 2)

//////// START "main()" /////////////////////////////////////////////////////////////////

main();
function main(){ 

////// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting   = configs.get      ( "AuthzConfU" );   /// REQUIRED
  engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strPrompt         = configs.get      ( "StrConfA1" );    /// REQUIRED
  if( strPrompt       === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: Prompt} must be non-empty \n" );
  }
const strPocketCompletion       = configs.getObject( "SelectConfB1" ); /// REQUIRED
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
let strJson = {};
    strJson.model = "gpt-3.5-turbo";
    strJson.messages = [];
    strJson.messages[0] = {};
    strJson.messages[0].role = "user";
    strJson.messages[0].content = strPrompt;

/// prepare request1
let request1Uri = "https://api.openai.com/v1/chat/completions";
let request1 = httpClient.begin(); // HttpRequestWrapper
    request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    request1 = request1.body( JSON.stringify( strJson ), "application/json" );

/// try request1
const response1     = request1.post( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + ""; // JavaNum to string
const response1Body = response1.getResponseAsString();
engine.log( " AutomatedTask ApiResponse1 Status: " + response1Code );
if( response1Code !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response1Code + "\n" + response1Body + "\n" );
}

/// parse response1
/* engine.log( response1Body ); // debug
{
  "id":"chatcmpl-6pttfxxxxxmzOKmyyyyy9A2gIwwww",
  "object":"chat.completion",
  "created":1677827615,
  "model":"gpt-3.5-turbo-0301",
  "usage":{
    "prompt_tokens":41,
    "completion_tokens":424,
    "total_tokens":465
  },
  "choices":[{
    "message":{
      "role":"assistant",
      "content":"\n\n京都の良さは、数え切れないほどたくさんありますが...\n\n清水寺は..."
    },
    "finish_reason":null,
    "index":0
  }]
}
*/
const response1Obj = JSON.parse( response1Body );


////// == Data Updating / ワークフローデータへの代入 ==

if( strPocketCompletion !== null ){
  engine.setData( strPocketCompletion,
                  response1Obj.choices[0].message.content ?? ""
                );
}
if( numPocketPromptTokens !== null ){
  engine.setData( numPocketPromptTokens, new java.math.BigDecimal(
                  response1Obj.usage.prompt_tokens ?? 0
                ));
}
if( numPocketCompletionTokens !== null ){
  engine.setData( numPocketCompletionTokens, new java.math.BigDecimal(
                  response1Obj.usage.completion_tokens ?? 0
                ));
}
if( numPocketTotalTokens !== null ){
  engine.setData( numPocketTotalTokens, new java.math.BigDecimal(
                  response1Obj.usage.total_tokens ?? 0
                ));
}
// "??": Nullish coalescing operator (ES11)
// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing

} //////// END "main()" /////////////////////////////////////////////////////////////////


/*
Notes:
- About "OpenAI API"
    - https://platform.openai.com/docs/introduction/overview
    - The OpenAI API can be applied to virtually any task that involves understanding or generating natural language or code.
    - For example, if you give the API the prompt, "Write a tagline for an ice cream shop",
    - it will return a completion like "We serve up smiles with every scoop!"
- If you place this "Addon Automated Step" on the Workflow diagram, 
    - a response will be retrieved automatically when the token reaches the automated step.
    - A request prompt is automatically sent to the OpenAI API server. (REST API communication)
- API key is required to use the OpenAI API.
    - Obtain the API key to be used in advance.

APPENDIX
- Header for developers belonging to multiple organizations is not supported (as of 202303)
     - `OpenAI-Organization`
- Requests with context are not supported.
     - You cannot set the flow of conversation in this auto-step.
- To activate the Workflow App including this "Add-on automated Step", "HTTP Authorization Setting" is required.
     - Set "Secret API Key" as communication token. (Token Fixed Value)

Notes-ja:
- "OpenAI API" とは
    - https://platform.openai.com/docs/introduction/overview
    - OpenAI API は、自然言語やコードを理解し、自然言語やコードを生成します。あらゆる仕事に適用可能です。
    - たとえば、「アイスクリーム屋のキャッチフレーズを書いて」というプロンプト(Prompt)を与えると、
    - 「全てのスクープを笑顔で!」といった応答(Completion)が返されます。 (Questetra社の意訳)
- この[アドオン自動工程]をワークフロー図に配置すれば、案件が工程に到達した際、応答が取得されます。
    - OpenAI API サーバに対してリクエスト文が自動的に送信されます。(REST API通信)
- OpenAI API の利用には API key が必要です。
    - あらかじめ "Secret API Key" を取得しておいてください。

APPENDIX-ja
- 複数組織に所属する開発者向けのヘッダには未対応です(202303時点)
    - `OpenAI-Organization`
- 文脈を使ったリクエストには未対応です。
    - この自動工程では会話の流れを設定することはできません。
- この[アドオン自動工程]を含むワークフローアプリを運用するには[HTTP 認証設定]が必要です。
    - "Secret API Key" を通信トークンとしてセットします。[トークン直接指定]
*/

Download

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

  • About “OpenAI API”
    • https://platform.openai.com/docs/introduction/overview
    • The OpenAI API can be applied to virtually any task that involves understanding or generating natural language or code.
    • For example, if you give the API the prompt, “Write a tagline for an ice cream shop”,
    • it will return a completion like “We serve up smiles with every scoop!”
  • If you place this “Addon Automated Step” on the Workflow diagram,
    • a response will be retrieved automatically when the token reaches the automated step.
    • A request prompt is automatically sent to the OpenAI API server. (REST API communication)
  • API key is required to use the OpenAI API.
    • Obtain the API key to be used in advance.

Capture

OpenAI: Chat, Start

Appendix

  • Header for developers belonging to multiple organizations is not supported (as of 202303)
    • OpenAI-Organization
  • Requests with context are not supported.
    • You cannot set the flow of conversation in this auto-step.
  • To activate the Workflow App including this “Add-on automated Step”, “HTTP Authorization Setting” is required.
    • Set “Secret API Key” as communication token. (Token Fixed Value)

See Also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading