OpenAI #File: Download

OpenAI #File: Download

translate OpenAI #File: ダウンロード

Downloads a file on the OpenAI API server. Files matching the specified ID are stored. The file ID is in the format “file-xxxxxyyyyyyyzzzzzXXXXXXXYYYYY” and can be obtained during upload.

Auto Step icon
Configs for this Auto Step
AuthzConfU1
U1: Select HTTP_Authz Setting (Secret API Key as “Fixed Value”) *
StrConfA1
A1: Set ID of File *#{EL}
SelectConfB1
B1: Select FILE that stores Download File (update)
StrConfU2
U2: Set OpenAI Organization ID (“org-xxxx”)#{EL}
SelectConfB1b
B1b: Select FILE that stores Result File (append)
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 strId               = configs.get( "StrConfA1" );               /// REQUIRED
  if( strId             === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: Id} must be non-empty string \n" );
  }
const filesPocketUpdate = configs.getObject( "SelectConfB1" );        // NotRequired
  let filesUpdate       = new java.util.ArrayList();
const filesPocketAppend = configs.getObject( "SelectConfB1b" );       // NotRequired
  let filesAppend       = new java.util.ArrayList();
  if ( filesPocketAppend !== null ) {
    if ( engine.findData( filesPocketAppend ) !== null ) {
      filesAppend       = engine.findData( filesPocketAppend ); // java.util.ArrayList
      engine.log( " AutomatedTask FilesArray {B1b}: " +
                  filesAppend.size() + " files" );
    }
  }



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



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

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

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

/// try request1
const response1     = request1.get( 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-P6mdMBuR56sxgco3pr9qHAVm",
  "purpose": "fine-tune",
  "filename": "training-data-questetra-bpm-suite-assistant.jsonl",
  "bytes": 6823,
  "created_at": 1690252892,
  "status": "processed",
  "status_details": null
}
*/

/// parse response1
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask OpenAI #retrieved: " + response1Obj.id );
const strFilename  = response1Obj.filename;

//// OpenAI API > Documentation > API REFERENCE > FILES > Retrieve file content
//// https://platform.openai.com/docs/api-reference/files/retrieve-content

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

/// try request2
const response2     = request2.get( request2Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest2 Start: " + request2Uri );
const response2Code = response2.getStatusCode() + ""; // JavaNum to string
const response2Body = response2.getResponseAsString();
engine.log( " AutomatedTask ApiResponse1 Status: " + response2Code );
if( response2Code !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response2Code + "\n" + response2Body + "\n" );
}

const qfileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
  strFilename, response2.getContentType(), response2.getResponse()
);
filesUpdate.add( qfileTmp );
filesAppend.add( qfileTmp );



////// == Data Updating / ワークフローデータへの代入 ==
if( filesPocketUpdate !== null ){
  engine.setData( filesPocketUpdate, filesUpdate );
}
if( filesPocketAppend !== null ){
  engine.setData( filesPocketAppend, filesAppend );
}


} //////// 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 download the file on the OpenAI server.

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"


Notes-ja:
- この[自動工程]をワークフロー図に配置すれば、案件が到達する度にリクエストが自動送信されます。
    - OpenAI API サーバに対してリクエストが自動送出されます。(REST API通信)
    - OpenAI API サーバからのレスポンスが自動保存解析されます。
- この[自動工程]は、OpenAI サーバ上のファイルをダウンロードします。

APPENDIX-ja
- この[アドオン自動工程]を含むワークフローアプリを運用するには[HTTP 認証設定]が必要です。
    - あらかじめ "Secret API Key" を取得しておいてください。
    - "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

  • 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 download the file on the OpenAI server.

Capture

Downloads the file on the OpenAI API server. Specify a file ID, the file will be stored. The file ID is in a format such as "file-xxxxxyyyyyzzzzzXXXXXYYYY" and can be saved when uploading.

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”

See Also

Leave a Reply

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

%d bloggers like this: