-
-
Save ujnak/3cb786a578c7884872d00789ceba70c9 to your computer and use it in GitHub Desktop.
OpenAIのFiles APIを呼び出すPL/SQLパッケージ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace package utl_openai_files_api | |
as | |
/** | |
* OpenAI Upload fileを呼び出す。 | |
* 参照: https://platform.openai.com/docs/api-reference/files/create | |
* | |
* @param p_filename アップロードされるファイル名 | |
* @param p_content_type ファイル形式 | |
* @param p_file_content ファイル本体 | |
* @param p_purpose OpenAIでのファイルの用途 fine-tuneかassistantsのどちらか | |
* @param p_credential_static_id Web資格証明 | |
* @param p_file アップロードされたファイルのメタデータ JSON形式 | |
* @return file_id | |
*/ | |
function upload_file( | |
p_filename in varchar2 | |
,p_content_type in varchar2 | |
,p_file_content in blob | |
,p_purpose in varchar2 default 'assistants' | |
,p_credential_static_id in varchar2 default 'OPENAI_API_KEY' | |
,p_file out clob | |
) | |
return varchar2; | |
/** | |
* OpenAI Retrieve file、Retrieve file contentを呼び出す。 | |
* 参照: https://platform.openai.com/docs/api-reference/files/retrieve | |
* 参照: https://platform.openai.com/docs/api-reference/files/retrieve-contents | |
* | |
* @param p_file_id file_id | |
* @param p_credential_static_id Web資格証明 | |
* @param p_file ファイルのメタデータ JSON形式 | |
* @param p_file_content ファイル本体 BLOB形式 | |
* @return file_id | |
*/ | |
function retrieve_file( | |
p_file_id in varchar2 | |
,p_credential_static_id in varchar2 default 'OPENAI_API_KEY' | |
,p_file out clob | |
,p_file_content out blob | |
) | |
return varchar2; | |
/** | |
* OpenAI delete fileを呼び出す。 | |
* 参照: https://platform.openai.com/docs/api-reference/files/delete | |
* | |
* @param p_file_id file_id | |
* @param p_credential_static_id Web資格証明 | |
*/ | |
function delete_file( | |
p_file_id in varchar2 | |
,p_credential_static_id in varchar2 default 'OPENAI_API_KEY' | |
,p_file out clob | |
) | |
return varchar2; | |
end utl_openai_files_api; | |
/ | |
create or replace package body utl_openai_files_api | |
as | |
/** | |
* ファイルのアップロード | |
*/ | |
function upload_file( | |
p_filename in varchar2 | |
,p_content_type in varchar2 | |
,p_file_content in blob | |
,p_purpose in varchar2 | |
,p_credential_static_id in varchar2 | |
,p_file out clob | |
) | |
return varchar2 | |
as | |
l_response clob; | |
l_multipart apex_web_service.t_multipart_parts; | |
l_multipart_request blob; | |
e_api_call_failed exception; | |
l_file json_object_t; | |
begin | |
apex_web_service.clear_request_headers; | |
/* | |
* アップロードするファイルを指定する。 | |
*/ | |
apex_web_service.append_to_multipart( | |
p_multipart => l_multipart | |
,p_name => 'file' | |
,p_filename => p_filename | |
,p_content_type => p_content_type | |
,p_body_blob => p_file_content | |
); | |
/* | |
* purposeを指定する。 | |
*/ | |
apex_web_service.append_to_multipart( | |
p_multipart => l_multipart | |
,p_name => 'purpose' | |
,p_content_type => 'text/plain' | |
,p_body => p_purpose | |
); | |
l_multipart_request := apex_web_service.generate_request_body(l_multipart); | |
l_response := apex_web_service.make_rest_request( | |
p_url => 'https://api.openai.com/v1/files' | |
,p_http_method => 'POST' | |
,p_body_blob => l_multipart_request | |
,p_credential_static_id => p_credential_static_id | |
); | |
if apex_web_service.g_status_code <> 200 then | |
apex_debug.info('status_code = %s, response = %s', apex_web_service.g_status_code, l_response); | |
raise e_api_call_failed; | |
end if; | |
p_file := l_response; | |
l_file := json_object_t(p_file); | |
return l_file.get_string('id'); | |
end upload_file; | |
/** | |
* ファイルのダウンロード | |
*/ | |
function retrieve_file( | |
p_file_id in varchar2 | |
,p_credential_static_id in varchar2 | |
,p_file out clob | |
,p_file_content out blob | |
) | |
return varchar2 | |
as | |
l_url varchar2(400); | |
l_response clob; | |
e_api_call_failed exception; | |
l_file json_object_t; | |
begin | |
/* ファイルのメタデータの取得 */ | |
l_url := 'https://api.openai.com/v1/files/' || p_file_id; | |
apex_web_service.clear_request_headers; | |
l_response := apex_web_service.make_rest_request( | |
p_url => l_url | |
,p_http_method => 'GET' | |
,p_credential_static_id => p_credential_static_id | |
); | |
if apex_web_service.g_status_code <> 200 then | |
apex_debug.info('status_code = %s, response = %s', apex_web_service.g_status_code, l_response); | |
raise e_api_call_failed; | |
end if; | |
/* ファイルのメタデータをp_fileとして返す。 */ | |
p_file := l_response; | |
/* ファイルの内容をBOBとして取得する */ | |
l_url := l_url || '/content'; | |
apex_web_service.clear_request_headers; | |
p_file_content := apex_web_service.make_rest_request_b( | |
p_url => l_url | |
,p_http_method => 'GET' | |
,p_credential_static_id => p_credential_static_id | |
); | |
if apex_web_service.g_status_code <> 200 then | |
apex_debug.info('status_code = %s, failed to download file', apex_web_service.g_status_code); | |
raise e_api_call_failed; | |
end if; | |
l_file := json_object_t(p_file); | |
return l_file.get_string('id'); | |
end retrieve_file; | |
/** | |
* ファイルの削除 | |
*/ | |
function delete_file( | |
p_file_id in varchar2 | |
,p_credential_static_id in varchar2 | |
,p_file out clob | |
) | |
return varchar2 | |
as | |
l_url varchar2(400); | |
l_response clob; | |
e_api_call_failed exception; | |
l_file json_object_t; | |
begin | |
/* ファイルのメタデータの取得 */ | |
l_url := 'https://api.openai.com/v1/files/' || p_file_id; | |
apex_web_service.clear_request_headers; | |
l_response := apex_web_service.make_rest_request( | |
p_url => l_url | |
,p_http_method => 'DELETE' | |
,p_credential_static_id => p_credential_static_id | |
); | |
if apex_web_service.g_status_code <> 200 then | |
apex_debug.info('status_code = %s, response = %s', apex_web_service.g_status_code, l_response); | |
raise e_api_call_failed; | |
end if; | |
/* 削除したファイルのメタデータをp_fileとして返す。 */ | |
p_file := l_response; | |
l_file := json_object_t(p_file); | |
return l_file.get_string('id'); | |
end delete_file; | |
end utl_openai_files_api; | |
/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment