Sample code for 30+ languages & platforms
PowerBuilder

OpenAI (ChatGPT) List Models

See more OpenAI ChatGPT Examples

Show how to list available OpenAI models and shows how to parse the JSON model information.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Id
integer li_Created
string ls_Owned_by
string ls_Root
string ls_Parent
integer j
integer li_Count_j
integer li_Allow_create_engine
integer li_Allow_sampling
integer li_Allow_logprobs
integer li_Allow_search_indices
integer li_Allow_view
integer li_Allow_fine_tuning
string ls_Organization
string ls_Group
integer li_Is_blocking
string ls_V_object
integer i
integer li_Count_i

li_Success = 0

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Implements the following CURL command:

// curl https://api.openai.com/v1/models \
//   -H "Authorization: Bearer $OPENAI_API_KEY"

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

// Adds the "Authorization: Bearer $OPENAI_API_KEY" header.
// This is NOT a real key.  Change the "sk-vi...." to your own key.
loo_Http.AuthToken = "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"

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.QuickGetSb("https://api.openai.com/v1/models",loo_SbResponseBody)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponseBody
    return
end if

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Http.LastStatus
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Http.LastHeader
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "object": "list",
//   "data": [
//     {
//       "id": "babbage",
//       "object": "model",
//       "created": 1649358449,
//       "owned_by": "openai",
//       "permission": [
//         {
//           "id": "modelperm-49FUp5v084tBB49tC4z8LPH5",
//           "object": "model_permission",
//           "created": 1669085501,
//           "allow_create_engine": false,
//           "allow_sampling": true,
//           "allow_logprobs": true,
//           "allow_search_indices": false,
//           "allow_view": true,
//           "allow_fine_tuning": false,
//           "organization": "*",
//           "group": null,
//           "is_blocking": false
//         }
//       ],
//       "root": "babbage",
//       "parent": null
//     },
//     {
//       "id": "davinci",
//       "object": "model",
//       "created": 1649359874,
//       "owned_by": "openai",
//       "permission": [
//         {
//           "id": "modelperm-U6ZwlyAd0LyMk4rcMdz33Yc3",
//           "object": "model_permission",
//           "created": 1669066355,
//           "allow_create_engine": false,
//           "allow_sampling": true,
//           "allow_logprobs": true,
//           "allow_search_indices": false,
//           "allow_view": true,
//           "allow_fine_tuning": false,
//           "organization": "*",
//           "group": null,
//           "is_blocking": false
//         }
//       ],
//       "root": "davinci",
//       "parent": null
//     },
// ...
// ...

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

ls_V_object = loo_JResp.StringOf("object")
i = 0
li_Count_i = loo_JResp.SizeOfArray("data")
do while i < li_Count_i
    loo_JResp.I = i
    ls_Id = loo_JResp.StringOf("data[i].id")
    ls_V_object = loo_JResp.StringOf("data[i].object")
    li_Created = loo_JResp.IntOf("data[i].created")
    ls_Owned_by = loo_JResp.StringOf("data[i].owned_by")
    ls_Root = loo_JResp.StringOf("data[i].root")
    ls_Parent = loo_JResp.StringOf("data[i].parent")
    j = 0
    li_Count_j = loo_JResp.SizeOfArray("data[i].permission")
    do while j < li_Count_j
        loo_JResp.J = j
        ls_Id = loo_JResp.StringOf("data[i].permission[j].id")
        ls_V_object = loo_JResp.StringOf("data[i].permission[j].object")
        li_Created = loo_JResp.IntOf("data[i].permission[j].created")
        li_Allow_create_engine = loo_JResp.BoolOf("data[i].permission[j].allow_create_engine")
        li_Allow_sampling = loo_JResp.BoolOf("data[i].permission[j].allow_sampling")
        li_Allow_logprobs = loo_JResp.BoolOf("data[i].permission[j].allow_logprobs")
        li_Allow_search_indices = loo_JResp.BoolOf("data[i].permission[j].allow_search_indices")
        li_Allow_view = loo_JResp.BoolOf("data[i].permission[j].allow_view")
        li_Allow_fine_tuning = loo_JResp.BoolOf("data[i].permission[j].allow_fine_tuning")
        ls_Organization = loo_JResp.StringOf("data[i].permission[j].organization")
        ls_Group = loo_JResp.StringOf("data[i].permission[j].group")
        li_Is_blocking = loo_JResp.BoolOf("data[i].permission[j].is_blocking")
        j = j + 1
    loop
    i = i + 1
loop


destroy loo_Http
destroy loo_SbResponseBody
destroy loo_JResp