OpenAI #Chat: Start

OpenAI: Chat, Start

OpenAI: Chat, Start

OpenAI: Chat, 開始

Starts a conversation with the OpenAI API (ChatGPT). The MODEL used is “gpt-4” by default (configurable). 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”) *
StrConfM
M: Set MODEL Name (default “gpt-4”)#{EL}
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 strModel          = configs.get( "StrConfM" ) !== "" ? // NotRequired
                          configs.get( "StrConfM" ) : "gpt-4"; // (default)
  engine.log( " AutomatedTask Config: OpenAI Model: " + strModel );
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 = strModel;
    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
/* 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:
- If you place this "Automated Step" in the Workflow diagram,
    - the request will be automatically sent every time the process token arrives.
    - A request is automatically sent to the OpenAI API server. (REST API)
    - The response from the OpenAI API server is automatically parsed.
    - You can incorporate "AI assistance" into your business processes.
- 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!"
- API key is required to use the OpenAI API.
    - Obtain the API key to be used in advance.
- Configurable MODEL names:
    - https://platform.openai.com/docs/models/model-endpoint-compatibility

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.
    - Use "OpenAI: Chat, Interact with Parameters" instead.
        - https://support.questetra.com/addons/openai-chat-interact-with-parameters-2023/
- 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)
- Older versions
    - https://support.questetra.com/addons/openai-chat-start-2023/
        - Compatible.
        - Overwrite to update "Add-on Service Task".
        - No need for re-release of "Workflow App"
- If access to the latest Model is restricted, 404 error ("That model does not exist")
    - Even if the latest Model is released by OpenAI, it is not open to all developers.
- An error may occur depending on the timing.
    - 429 error ("Too Many Requests")
    - Timeout script error 'java.util.concurrent.TimeoutException'

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


APPENDIX-ja
- Older versions
    - https://support.questetra.com/ja/addons/openai-chat-start-2023/
        - 互換性あります。
        - [アドオンサービスタスク]を上書きアップデートしてください。
        - [ワークフローアプリ]の更新リリースは、必ずしも必要ではありません。
            - モデル(AIエンジン)を変更することが出来るようになりました。
- 複数組織に所属する開発者向けのヘッダには未対応です(202303時点)
    - `OpenAI-Organization`
- 文脈を使ったリクエストには未対応です。
    - この自動工程では会話の流れを設定することはできません。
    - 代わりに『OpenAI: Chat, パラメータ付き対話』を利用してください。
        - https://support.questetra.com/ja/addons/openai-chat-interact-with-parameters-2023/
- この[アドオン自動工程]を含むワークフローアプリを運用するには[HTTP 認証設定]が必要です。
    - "Secret API Key" を通信トークンとしてセットします。[トークン直接指定]
- 最新Modelへのアクセスが制限されている場合、404エラー("That model does not exist")
    - 最新Modelが発表されても、全ての開発者アカウントがアクセスできる訳ではありません。
    - eg. `GPT-4 is currently in a limited beta and only accessible to those who have been granted access.`
    - eg. `AutomatedTask UnexpectedResponseError: 404 { "error": { "message": "That model does not exist",`
- タイミングによって、エラーになる場合があります。
    - 429エラー("Too Many Requests")
    - Timeout スクリプトエラー 'java.util.concurrent.TimeoutException'
*/

Download

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

Notes

  • If you place this automated step in the Workflow diagram,
    • the request will be automatically sent every time the process token arrives.
    • A request is automatically sent to the OpenAI API server. (REST API)
    • The response from the OpenAI API server is automatically parsed.
    • You can incorporate AI assistance into your business processes.
  • 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!”
  • API key is required to use the OpenAI API.
    • Obtain the API key to be used in advance.
  • Configurable MODEL names:

Capture

Starts a conversation with the OpenAI API (ChatGPT). The MODEL used is "gpt-4" by default (configurable). A response (COMPLETION) to an instruction question (PROMPT) is automatically obtained.

Appendix

  • Header for developers belonging to multiple organizations is not supported (as of 202303)
    • OpenAI-Organization
  • Requests with context are not supported.
  • To activate the Workflow App including this add-on automated Step, HTTP Authorization setting is required.
    • Set the Secret API Key as the communication token. (Token Fixed Value)
  • Older versions
  • If access to the latest Model is restricted, 404 error (“That model does not exist”)
    • Even if the latest Model is released by OpenAI, it is not open to all developers.
  • An error may occur depending on the timing.
    • 429 error (“Too Many Requests”)
    • Timeout script error ‘java.util.concurrent.TimeoutException’

See Also

OpenAI: Audio, Transcribe to WebVTT
OpenAI: Chat, Interact with Parameters

Leave a Reply

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

Discover more from Questetra Support

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

Continue reading

Scroll to Top