Skip to content

OpenAI Chat Completions

The OpenAI Chat Completions API allows developers to integrate OpenAI's conversational models, such as GPT-4 and GPT-3.5, into their applications to create interactive, natural-language conversations with users. It's designed specifically for building chat-based interactions, from simple Q&A chatbots to sophisticated conversational agents.

Many other foundation models offer APIs that are compatible with the OpenAI Chat Completions API and for these models, the Ioto support may usable.

It is expected that the newer Responses API will replace the Chat Completions API over time and so developers should use the Response API for new projects.

API Tour

Here is an example calling the Chat Completions API to ask a simple question.

c
PUBLIC void aiChatCompletionExample(void)
{
    Json  *request, *response;
    cchar *model;
    char  buf[1024];

    request = jsonParse(\
        "{messages: [{"
            "role: \"system\","
            "content: \"You are a helpful assistant.\""
        "},{"
            "role: \"user\","
            "content: \"What is the capital of the moon?\""
        "}]}", 0);
    response = openaiChatCompletion(request);

    // Extract the LLM response text from the json payload
    text = jsonGet(response, "choices[0].message.content", 0);
    printf("Response: %s\n", text);

    jsonFree(request);
    jsonFree(response);
}

See the ai app in the Ioto Agent source download for an example completion.html web page that uses the Chat Completion API.

References

Consult the OpenAI documentation for API details: