OpenAI Component
High-level OpenAI HTTP API client — chat completions, responses, embeddings, images, audio, files, fine-tuning, batches and assistants.
High-level OpenAI HTTP API client — chat completions, responses, embeddings, images, audio, files, fine-tuning, batches and assistants.
OpenAI REST API client covering Chat Completions, Images, Audio (TTS/STT), Embeddings, Files, Fine-tuning, Moderation, Assistants, Threads, Runs, Messages and Vector Stores.
TsgcHTTP_API_OpenAI| Standards & specs | OpenAI API reference · OpenAI Chat Completions |
| Component class | TsgcHTTP_API_OpenAI (unit sgcHTTP_API_OpenAI) |
| Frameworks | VCL, FireMonkey, Lazarus / FPC, .NET |
| Platforms | Windows, macOS, Linux, iOS, Android |
The principal published / public properties used to configure and drive the component. Consult the online help for the full list.
TLSOptions | TLS/SSL configuration used for HTTPS connections to the OpenAI API |
OpenAIOptions | Container for all OpenAI REST client settings including credentials, organization, HTTP, logging and retry options |
ReadTimeout | HTTP read timeout in milliseconds applied to every OpenAI REST request |
CircuitBreaker | Retry and rate-limit handling policy used when OpenAI endpoints fail transiently |
Version | Read-only sgcWebSockets library version string |
The principal public methods exposed by the component.
ListInputItems() | Lists the input items that were provided when a response was created |
CreateThread() | Creates a new Thread optionally seeded with initial messages and tool resources |
ModifyThread() | Updates the tool resources and metadata of an existing Thread |
SubmitToolOutputsToRun() | Returns tool function outputs to a Run that is waiting in requires_action state |
DeleteResponse() | Deletes a stored response from the OpenAI Responses API |
DeleteFile() | Permanently deletes a file previously uploaded to OpenAI |
DeleteFineTuneModel() | Deletes a fine-tuned model so it is no longer available for inference |
DeleteAssistant() | Permanently deletes an Assistant by its identifier |
RetrieveThread() | Retrieves a Thread definition by its identifier |
DeleteThread() | Permanently deletes a Thread and all of its messages |
The component exposes the following published events; consult the online help for full event-handler signatures.
OnHTTPAPIException | Fires when an uncaught exception is raised during an OpenAI REST call |
OnHTTPAPISSE | Fires for each Server-Sent Events chunk received during a streaming OpenAI call |
Drop the component on a form, configure the properties below and activate it. The snippet that follows shows the typical OpenAI Chat — Simple Example configuration sourced from the online help.
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; WriteLn(OpenAI._CreateChatCompletion('gpt-3.5-turbo', 'Hello!'));
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; ShowMessage(OpenAI->_CreateChatCompletion("gpt-3.5-turbo", "Hello!"));
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; MessageBox.Show(OpenAI._CreateChatCompletion("gpt-3.5-turbo", "Hello!"));
The following scenarios are lifted verbatim from the online help. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
Create a batch job and retrieve its status.
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; // Create a batch WriteLn(OpenAI._CreateBatch('file-abc123', '/v1/chat/completions')); // List batches WriteLn(OpenAI._ListBatches());
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; // Create a batch ShowMessage(OpenAI->_CreateBatch("file-abc123", "/v1/chat/completions")); // List batches ShowMessage(OpenAI->_ListBatches());
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; // Create a batch MessageBox.Show(OpenAI._CreateBatch("file-abc123", "/v1/chat/completions")); // List batches MessageBox.Show(OpenAI._ListBatches());
Use the text-davinci-003 model to get a predicted completion.
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; WriteLn(OpenAI._CreateCompletion('text-davinci-003', 'Say this is a test'));
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; ShowMessage(OpenAI->_CreateCompletion("text-davinci-003", "Say this is a test"));
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; MessageBox.Show(OpenAI._CreateCompletion("text-davinci-003", "Say this is a test"));
Tell OpenAI to fix the spelling mistakes of a prompt.
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; WriteLn(OpenAI._CreateEdit('text-davinci-edit-001', 'Fix the spelling mistakes', 'What day of the wek is it?'));
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; ShowMessage(OpenAI->_CreateEdit("text-davinci-edit-001", "Fix the spelling mistakes", "What day of the wek is it?"));
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; MessageBox.Show(OpenAI._CreateEdit("text-davinci-edit-001", "Fix the spelling mistakes", "What day of the wek is it?"));
Create a fine-tuning job and list existing jobs.
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; // Create a fine-tuning job WriteLn(OpenAI._CreateFineTuningJob('gpt-4o-mini-2024-07-18', 'file-abc123')); // List fine-tuning jobs WriteLn(OpenAI._ListFineTuningJobs());
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; // Create a fine-tuning job ShowMessage(OpenAI->_CreateFineTuningJob("gpt-4o-mini-2024-07-18", "file-abc123")); // List fine-tuning jobs ShowMessage(OpenAI->_ListFineTuningJobs());
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; // Create a fine-tuning job MessageBox.Show(OpenAI._CreateFineTuningJob("gpt-4o-mini-2024-07-18", "file-abc123")); // List fine-tuning jobs MessageBox.Show(OpenAI._ListFineTuningJobs());
Moderate the following text
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; WriteLn(OpenAI._CreateModeration('I want to kill them.'));
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; ShowMessage(OpenAI->_CreateModeration("I want to kill them."));
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; MessageBox.Show(OpenAI._CreateModeration("I want to kill them."));
Create a response using the Responses API.
OpenAI := TsgcHTTP_API_OpenAI.Create(nil); OpenAI.OpenAIOptions.ApiKey := 'API_KEY'; WriteLn(OpenAI._CreateResponse('gpt-4o', 'What is the capital of France?'));
TsgcHTTP_API_OpenAI *OpenAI = new TsgcHTTP_API_OpenAI(NULL); OpenAI->OpenAIOptions->ApiKey = "API_KEY"; ShowMessage(OpenAI->_CreateResponse("gpt-4o", "What is the capital of France?"));
TsgcHTTP_API_OpenAI OpenAI = new TsgcHTTP_API_OpenAI(); OpenAI.OpenAIOptions.ApiKey = "API_KEY"; MessageBox.Show(OpenAI._CreateResponse("gpt-4o", "What is the capital of France?"));
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Demos\15.AI\01.QuickStart\01.OpenAI
.net\demos\15.AI\01.QuickStart\01.OpenAI