Skip to content

OpenAI API

The OpenAI library supports access to the OpenAI cloud-based LLM.

It provides the ability to create on-device LLM prompts and send to powerful cloud-based LLMs.

Function Index

Json *openaiChatCompletion(Json *props)
 Submit a request to OpenAI Chat Completion API.
intopenaiInit(cchar *endpoint, cchar *key, Json *config, int flags)
 Initialize the OpenAI API.
Url *openaiRealTimeConnect(Json *props)
 Submit a request to OpenAI Real Time API.
Json *openaiResponses(Json *props, OpenAIAgent agent, void *arg)
 Submit a request to OpenAI Responses API.
Url *openaiStream(Json *props, UrlSseProc callback, void *arg)
 Submit a request to OpenAI Response API and stream the response.
voidopenaiTerm(void)
 Terminate the OpenAI API.

Typedef Index

OpenAIAgentOpenAI Agent callback function.

Defines

#defineAI_SHOW_NONE   0x1
 Trace nothing.
#defineAI_SHOW_REQ   0x2
 Trace request.
#defineAI_SHOW_RESP   0x8
 Trace response.

Typedefs

typedef char *(* OpenAIAgent) (cchar *name, Json *request, Json *response, void *arg)

OpenAI Agent callback function.

Parameters:
nameis the name of the agent.
requestis the request JSON object.
responseis the response JSON object.
argis a user argument.
Returns:
Allocated text to be added to the response from the agent/tool.

Functions

Json * openaiChatCompletion (Json *props)

Submit a request to OpenAI Chat Completion API.

Description:
The following defaults are set: model: gpt-4o-mini.
Parameters:
propsis a JSON object of Response API parameters.
Returns:
Returns a JSON object with the response from the OpenAI Response API. Caller must free the returned JSON object with jsonFree.
API Stability:
Evolving.

int openaiInit (cchar *endpoint, cchar *key, Json *config, int flags)

Initialize the OpenAI API.

Parameters:
endpointis the OpenAI endpoint.
keyis the OpenAI key.
configis a JSON object of configuration parameters.
flagsis a bitmask of flags.
API Stability:
Internal.

Url * openaiRealTimeConnect (Json *props)

Submit a request to OpenAI Real Time API.

Parameters:
propsis a JSON object of Real Time API parameters.
Returns:
Returns an OpenAIRealTime object on success, or 0 on failure.
API Stability:
Evolving.

Json * openaiResponses (Json *props, OpenAIAgent agent, void *arg)

Submit a request to OpenAI Responses API.

Description:
The following defaults are set: {model: 'gpt-4o-mini', truncation: 'auto'}. The API will aggregate the output text into "output_text" for convenience.
Parameters:
propsis a JSON object of Response API parameters.
agentis a callback function that will be called for each chunk of the response.
argis a user argument that will be passed to the callback function.
Returns:
Returns a JSON object with the response from the OpenAI Response API. Caller must free the returned JSON object with jsonFree.
API Stability:
Evolving.

Url * openaiStream (Json *props, UrlSseProc callback, void *arg)

Submit a request to OpenAI Response API and stream the response.

Description:
The following defaults are set: {model: 'gpt-4o-mini', truncation: 'auto'}. The API will aggregate the output text into "output_text" for convenience.
Parameters:
propsis a JSON object of Response API parameters.
callbackis a callback function that will be called for each chunk of the response.
argis a user argument that will be passed to the callback function.
Returns:
Returns an Url object on success, or 0 on failure.
API Stability:
Evolving.

void openaiTerm (void )

Terminate the OpenAI API.

API Stability:
Internal.