TsgcHTTP_API_OpenAIMethods › CreateChatCompletion

CreateChatCompletion Method

Sends a chat completion request to the OpenAI Chat Completions endpoint and returns the model reply

Syntax

function CreateChatCompletion(const aRequest : TsgcOpenAIClass_Request_ChatCompletion) : TsgcOpenAIClass_Response_ChatCompletion;

Parameters

NameTypeDescription
aRequestconst TsgcOpenAIClass_Request_ChatCompletionRequest payload containing the model, the list of messages, temperature and any other Chat Completions parameter.

Return Value

Parsed response object exposing the assistant message, finish reason, token usage and any tool calls returned by the model. (TsgcOpenAIClass_Response_ChatCompletion)

Remarks

Issues a POST to /v1/chat/completions with the JSON body built from aRequest. Use it to run GPT-4o, GPT-4.1, GPT-5 and legacy chat-capable models with the classic messages-based schema, including tool calling, JSON mode and multimodal vision inputs. The call is synchronous; when aRequest.Stream is enabled the incremental chunks are surfaced through OnHTTPAPISSE while the final aggregated object is still returned here. Any exception raised during the call is routed through OnHTTPAPIException when a handler is attached.

Example

oRequest := TsgcOpenAIClass_Request_ChatCompletion.Create;
try
  oRequest.Model := 'gpt-4o-mini';
  oRequest.Messages.Add('user', 'Hello, who are you?');
  oResponse := oAPI.CreateChatCompletion(oRequest);
  ShowMessage(oResponse.Choices[0].Message.Content);
finally
  oRequest.Free;
end;

Back to Methods