OpenAI #File: Upload

translate OpenAI #File: アップロード

Uploads files to the OpenAI API server. File documents are used by various endpoints and functions, such as fine tuning.

Auto Step icon
Configs for this Auto Step
AuthzConfU1
U1: Select HTTP_Authz Setting (Secret API Key as “Fixed Value”) *
SelectConfA1
A1: Select FILE in which Target File is Stored *
SelectConfB1
B1: Select STRING that stores ID of Uploaded File (update) *
StrConfU2
U2: Set OpenAI Organization ID (“org-xxxx”)#{EL}
StrConfA2
A2: Set PURPOSE (default “fine-tune”)#{EL}
SelectConfB2
B2: Select NUMERIC that stores Size of Uploaded File (update)
Script (click to open)
// GraalJS standard mode Script Script (engine type: 3)
// cf. 'engine type: 2': "GraalJS Nashorn compatible mode" (renamed from "GraalJS" at 20230526)


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

main();
function main(){ 

////// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting     = configs.get( "AuthzConfU1" );             /// REQUIRED
  engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strOrgId            = configs.get( "StrConfU2" );               // NotRequired
  engine.log( " AutomatedTask Config: OpenAI-Organization: " + strOrgId );
const filesPocketTarget   = configs.getObject( "SelectConfA1" );      /// REQUIRED
  let filesTarget         = engine.findData( filesPocketTarget );     // java.util.ArrayList
  if( filesTarget       === null ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: TargetFile} must be non-empty \n" );
  }
  engine.log( " AutomatedTask Config: #of Files: " + filesTarget.size() );
  engine.log( " AutomatedTask Config: Target File: " + filesTarget.get(0).getName() );
const strPurpose          = configs.get( "StrConfA2" ) !== "" ?       // NotRequired
                            configs.get( "StrConfA2" ) : "fine-tune"; // (default)
const strPocketFileId     = configs.getObject( "SelectConfB1" );      /// REQUIRED
const numPocketFileSize   = configs.getObject( "SelectConfB2" );      // NotRequired



////// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)



////// == Calculating / 演算 ==

//// OpenAI API > Documentation > API REFERENCE > FILES > Upload file
//// https://platform.openai.com/docs/api-reference/files/upload

/// prepare request1
let request1Uri = "https://api.openai.com/v1/files";
let request1 = httpClient.begin(); // HttpRequestWrapper
    request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    if ( strOrgId !== "" ){
      request1 = request1.header( "OpenAI-Organization", strOrgId );
    }

/// prepare multipart/form-data (curl -F)
    request1 = request1.multipart ( "purpose",        strPurpose );
    request1 = request1.multipart ( "file",           filesTarget.get(0) );

/// 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" );
}
/* engine.log( response1Body ); // debug
{
  "object": "file",
  "id": "file-jNyuVhzDqTRYS38pRJ7QTOG6",
  "purpose": "fine-tune",
  "filename": "training-data-questetra-bpm-suite-assistant.jsonl",
  "bytes": 6823,
  "created_at": 1690249733,
  "status": "uploaded",
  "status_details": null
}
*/

/// parse response1
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask OpenAI #created: " + response1Obj.created_at );
const strFileId   = response1Obj.id;
const numFileSize = response1Obj.bytes;



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

if( strPocketFileId !== null ){
  engine.setData( strPocketFileId, strFileId );
}
if( numPocketFileSize !== null ){
  engine.setData( numPocketFileSize, new java.math.BigDecimal( numFileSize ) );
}

} //////// 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.
- This "Automated Step" will upload the file to the OpenAI server.
    - Only one file stored in file type data is uploaded.
    - The size of all the files uploaded by one organization can be up to 1 GB. (as of 202307)

APPENDIX
- To activate a Workflow App that includes this Automated Step, "HTTP Authz Setting" is required
    - Obtain a "Secret API Key" in advance.
    - Set the key as the communication token in "Token Fixed Value"
- If the PURPOSE is set to "fine-tune", 
    - each line is a JSON record with "prompt" and "completion" fields representing your training examples.
    - There is no PURPOSE that can be set other than "fine-tune". (as of 202307)


Notes-ja:
- この[自動工程]をワークフロー図に配置すれば、案件が到達する度にリクエストが自動送信されます。
    - OpenAI API サーバに対してリクエストが自動送出されます。(REST API通信)
    - OpenAI API サーバからのレスポンスが自動保存解析されます。
    - "AI による支援" を業務プロセスに組み込むことが出来ます。
- この[自動工程]は、OpenAI サーバにファイルをアップロードします。
    - アップロードされるのは、ファイル型データに格納されている1ファイルのみです。
    - 1組織がアップロードできるファイルサイズ合計は1GBです。(202307現在)

APPENDIX-ja
- この[アドオン自動工程]を含むワークフローアプリを運用するには[HTTP 認証設定]が必要です。
    - あらかじめ "Secret API Key" を取得しておいてください。
    - "Secret API Key" を通信トークンとしてセットします。[トークン直接指定]
- PURPOSE が "fine-tune" に設定されている場合、
    - 各行は「prompt」フィールドと「completion」フィールドを含む JSON レコードで構成されます。
    - "fine-tune" 以外に設定可能な PURPOSE はありません。(202307現在)
*/

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.
  • This Automated Step will upload the file to the OpenAI server.
    • Only one file stored in file type data is uploaded.
    • The size of all the files uploaded by one organization can be up to 1 GB. (as of 202307)

Capture

Uploads a file to the OpenAI API server. The file contains documents to be used across various endpoints/features like Fine-tuning.

Appendix

  • To activate a Workflow App that includes this Automated Step, “HTTP Authz Setting” is required
    • Obtain a “Secret API Key” in advance.
    • Set the key as the communication token in “Token Fixed Value”
  • If the PURPOSE is set to “fine-tune”,
    • each line is a JSON record with “prompt” and “completion” fields representing your training examples.
    • There is no PURPOSE that can be set other than “fine-tune”. (as of 202307)

See Also

Leave a Reply

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

%d bloggers like this: