TsgcHTTP_API_OpenAIMethods › CreateCompletion

CreateCompletion Method

Calls the legacy OpenAI Completions endpoint for prompt-based text generation

Syntax

function CreateCompletion(const aRequest : TsgcOpenAIClass_Request_Completion) : TsgcOpenAIClass_Response_Completion;

Parameters

NameTypeDescription
aRequestconst TsgcOpenAIClass_Request_CompletionRequest payload holding the model name, prompt, max tokens, temperature and other completion parameters.

Return Value

Parsed response object containing the generated choices, finish reasons and token usage. (TsgcOpenAIClass_Response_Completion)

Remarks

Issues a POST to the legacy /v1/completions endpoint, which accepts a single prompt (or list of prompts) and returns one or more completion choices. This API is maintained for compatibility with instruct-style models such as gpt-3.5-turbo-instruct; for modern conversational workloads prefer CreateChatCompletion or CreateResponse. Streaming is supported by setting aRequest.Stream := True and handling OnHTTPAPISSE.

Example

oRequest := TsgcOpenAIClass_Request_Completion.Create;
try
  oRequest.Model := 'gpt-3.5-turbo-instruct';
  oRequest.Prompt := 'Translate to French: Hello, how are you?';
  oRequest.MaxTokens := 64;
  oResponse := oAPI.CreateCompletion(oRequest);
  ShowMessage(oResponse.Choices[0].Text);
finally
  oRequest.Free;
end;

Back to Methods