TsgcHTTP_API_OpenAI › Methods › CreateChatCompletion
Sends a chat completion request to the OpenAI Chat Completions endpoint and returns the model reply
function CreateChatCompletion(const aRequest : TsgcOpenAIClass_Request_ChatCompletion) : TsgcOpenAIClass_Response_ChatCompletion;
| Name | Type | Description |
|---|---|---|
aRequest | const TsgcOpenAIClass_Request_ChatCompletion | Request payload containing the model, the list of messages, temperature and any other Chat Completions parameter. |
Parsed response object exposing the assistant message, finish reason, token usage and any tool calls returned by the model. (TsgcOpenAIClass_Response_ChatCompletion)
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.
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;