C
C
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 C Downloads
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *id;
int created;
const char *owned_by;
const char *root;
const char *parent;
int j;
int count_j;
BOOL allow_create_engine;
BOOL allow_sampling;
BOOL allow_logprobs;
BOOL allow_search_indices;
BOOL allow_view;
BOOL allow_fine_tuning;
const char *organization;
const char *group;
BOOL is_blocking;
const char *v_object;
int i;
int count_i;
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) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttp_getLastStatus(http);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttp_lastHeader(http));
printf("Failed.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
// 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) {
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) {
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;
}
i = i + 1;
}
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}