Sample code for 30+ languages & platforms
Unicode C

OpenAI (ChatGPT) Create Chat Completion

See more OpenAI ChatGPT Examples

Shows how to create a model response for a given chat conversation.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    int index;
    const wchar_t *Role;
    const wchar_t *Content;
    const wchar_t *finish_reason;
    const wchar_t *id;
    const wchar_t *v_object;
    int created;
    int Prompt_tokens;
    int Completion_tokens;
    int Total_tokens;
    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 = CkHttpW_Create();

    // Implements the following CURL command:

    // curl https://api.openai.com/v1/chat/completions \
    //   -H "Content-Type: application/json" \
    //   -H "Authorization: Bearer $OPENAI_API_KEY" \
    //   -d '{
    //     "model": "gpt-3.5-turbo",
    //     "messages": [{"role": "user", "content": "Hello!"}]
    //   }'

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

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "model": "gpt-3.5-turbo",
    //   "messages": [
    //     {
    //       "role": "user",
    //       "content": "Hello!"
    //     }
    //   ]
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"model",L"gpt-3.5-turbo");
    CkJsonObjectW_UpdateString(json,L"messages[0].role",L"user");
    CkJsonObjectW_UpdateString(json,L"messages[0].content",L"Hello!");

    // Adds the "Authorization: Bearer $OPENAI_API_KEY" header.
    // This is NOT a real key.  Change the "sk-vi...." to your own key.
    CkHttpW_putAuthToken(http,L"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");
    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",L"https://api.openai.com/v1/chat/completions",json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

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

    // {
    //   "id": "chatcmpl-123",
    //   "object": "chat.completion",
    //   "created": 1677652288,
    //   "choices": [
    //     {
    //       "index": 0,
    //       "message": {
    //         "role": "assistant",
    //         "content": "\n\nHello there, how may I assist you today?"
    //       },
    //       "finish_reason": "stop"
    //     }
    //   ],
    //   "usage": {
    //     "prompt_tokens": 9,
    //     "completion_tokens": 12,
    //     "total_tokens": 21
    //   }
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    id = CkJsonObjectW_stringOf(jResp,L"id");
    v_object = CkJsonObjectW_stringOf(jResp,L"object");
    created = CkJsonObjectW_IntOf(jResp,L"created");
    Prompt_tokens = CkJsonObjectW_IntOf(jResp,L"usage.prompt_tokens");
    Completion_tokens = CkJsonObjectW_IntOf(jResp,L"usage.completion_tokens");
    Total_tokens = CkJsonObjectW_IntOf(jResp,L"usage.total_tokens");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"choices");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        index = CkJsonObjectW_IntOf(jResp,L"choices[i].index");
        Role = CkJsonObjectW_stringOf(jResp,L"choices[i].message.role");
        Content = CkJsonObjectW_stringOf(jResp,L"choices[i].message.content");
        finish_reason = CkJsonObjectW_stringOf(jResp,L"choices[i].finish_reason");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }