Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
id: PWideChar;
created: Integer;
owned_by: PWideChar;
root: PWideChar;
parent: PWideChar;
j: Integer;
count_j: Integer;
allow_create_engine: Boolean;
allow_sampling: Boolean;
allow_logprobs: Boolean;
allow_search_indices: Boolean;
allow_view: Boolean;
allow_fine_tuning: Boolean;
organization: PWideChar;
group: PWideChar;
is_blocking: Boolean;
v_object: PWideChar;
i: Integer;
count_i: Integer;

begin
success := False;

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

http := CkHttp_Create();

// 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.
CkHttp_putAuthToken(http,'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');

sbResponseBody := CkStringBuilder_Create();
success := CkHttp_QuickGetSb(http,'https://api.openai.com/v1/models',sbResponseBody);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttp_getLastStatus(http);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttp__lastHeader(http));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

// 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

v_object := CkJsonObject__stringOf(jResp,'object');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'data');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    id := CkJsonObject__stringOf(jResp,'data[i].id');
    v_object := CkJsonObject__stringOf(jResp,'data[i].object');
    created := CkJsonObject_IntOf(jResp,'data[i].created');
    owned_by := CkJsonObject__stringOf(jResp,'data[i].owned_by');
    root := CkJsonObject__stringOf(jResp,'data[i].root');
    parent := CkJsonObject__stringOf(jResp,'data[i].parent');
    j := 0;
    count_j := CkJsonObject_SizeOfArray(jResp,'data[i].permission');
    while j < count_j do
      begin
        CkJsonObject_putJ(jResp,j);
        id := CkJsonObject__stringOf(jResp,'data[i].permission[j].id');
        v_object := CkJsonObject__stringOf(jResp,'data[i].permission[j].object');
        created := CkJsonObject_IntOf(jResp,'data[i].permission[j].created');
        allow_create_engine := CkJsonObject_BoolOf(jResp,'data[i].permission[j].allow_create_engine');
        allow_sampling := CkJsonObject_BoolOf(jResp,'data[i].permission[j].allow_sampling');
        allow_logprobs := CkJsonObject_BoolOf(jResp,'data[i].permission[j].allow_logprobs');
        allow_search_indices := CkJsonObject_BoolOf(jResp,'data[i].permission[j].allow_search_indices');
        allow_view := CkJsonObject_BoolOf(jResp,'data[i].permission[j].allow_view');
        allow_fine_tuning := CkJsonObject_BoolOf(jResp,'data[i].permission[j].allow_fine_tuning');
        organization := CkJsonObject__stringOf(jResp,'data[i].permission[j].organization');
        group := CkJsonObject__stringOf(jResp,'data[i].permission[j].group');
        is_blocking := CkJsonObject_BoolOf(jResp,'data[i].permission[j].is_blocking');
        j := j + 1;
      end;

    i := i + 1;
  end;

CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);

end;