Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) OpenAI (ChatGTP) Upload JSON StringSee more OpenAI ChatGPT ExamplesDemonstrate how to upload a file (from a JSON string) to a OpenAI user's organization. For more information, see https://platform.openai.com/docs/api-reference/files/upload
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkHttpRequest.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i ; Implements the following CURL command: ; curl https://api.openai.com/v1/files \ ; -H "Authorization: Bearer $OPENAI_API_KEY" \ ; -F purpose="fine-tune" \ ; -F file="@mydata.jsonl" ; Use the following online tool to generate HTTP code from a CURL command ; Convert a cURL Command to HTTP Source Code req.i = CkHttpRequest::ckCreate() If req.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpRequest::setCkHttpVerb(req, "POST") CkHttpRequest::setCkPath(req, "/v1/files") CkHttpRequest::setCkContentType(req, "multipart/form-data") CkHttpRequest::ckAddParam(req,"purpose","fine-tune") ; { "prompt": "aa", "completion": "bb" } ; { "prompt": "cc", "completion": "dd" } sbJsonData.i = CkStringBuilder::ckCreate() If sbJsonData.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppendLine(sbJsonData,"{ " + Chr(34) + "prompt" + Chr(34) + ": " + Chr(34) + "aa" + Chr(34) + ", " + Chr(34) + "completion" + Chr(34) + ": " + Chr(34) + "bb" + Chr(34) + " }",1) CkStringBuilder::ckAppendLine(sbJsonData,"{ " + Chr(34) + "prompt" + Chr(34) + ": " + Chr(34) + "cc" + Chr(34) + ", " + Chr(34) + "completion" + Chr(34) + ": " + Chr(34) + "dd" + Chr(34) + " }",1) success = CkHttpRequest::ckAddStringForUpload2(req,"file","mydata2.jsonl",CkStringBuilder::ckGetAsString(sbJsonData),"utf-8","application/octet-stream") ; This is NOT a real key. Change the "sk-vi...." to your own key. CkHttpRequest::ckAddHeader(req,"Authorization","Bearer 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") CkHttpRequest::ckAddHeader(req,"Expect","100-continue") resp.i = CkHttp::ckSynchronousRequest(http,"api.openai.com",443,1,req) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbJsonData) ProcedureReturn EndIf sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpResponse::ckGetBodySb(resp,sbResponseBody) jResp.i = CkJsonObject::ckCreate() If jResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jResp,sbResponseBody) CkJsonObject::setCkEmitCompact(jResp, 0) Debug "Response Body:" Debug CkJsonObject::ckEmit(jResp) respStatusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbJsonData) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) ; Sample JSON response: ; (Sample code for parsing the JSON response is shown below) ; { ; "id": "file-XjGxS3KTG0uNmNOK362iJua3", ; "object": "file", ; "bytes": 140, ; "created_at": 1613779121, ; "filename": "mydata2.jsonl", ; "purpose": "fine-tune" ; } ; Sample code for parsing the JSON response... ; Use the following online tool to generate parsing code from sample JSON: ; Generate Parsing Code from JSON id.s = CkJsonObject::ckStringOf(jResp,"id") v_object.s = CkJsonObject::ckStringOf(jResp,"object") v_bytes.i = CkJsonObject::ckIntOf(jResp,"bytes") created_at.i = CkJsonObject::ckIntOf(jResp,"created_at") filename.s = CkJsonObject::ckStringOf(jResp,"filename") purpose.s = CkJsonObject::ckStringOf(jResp,"purpose") CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbJsonData) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.