Skip to content

OpenAI API client for Delphi. Use ChatGPT, DALL-E and other products.

License

Notifications You must be signed in to change notification settings

william-pristia/DelphiOpenAI

 
 

Repository files navigation

Delphi OpenAI API

GitHub GitHub GitHub GitHub GitHub

The library provides access to the API of the OpenAI service, on the basis of which ChatGPT works and, for example, the generation of images from text using DALL-E. Delphi 10.4+ is required to work with the library. It is possible to build under 10.3. This library is a TOpenAI class for the main TComponent for more convenient work.

This is an unofficial library. OpenAI does not provide any official library for Delphi.

Coverage

API Status
Models 🟢 Done
Completions 🟢 Done
Edits 🟢 Done
Images 🟢 Done
Embeddings 🟢 Done
Files 🟢 Done
Fine-tunes 🟢 Done
Moderations 🟢 Done
Engines (Depricated) 🟢 Done

Installation

To use the library, just add the root folder to the IDE library path, or your project source path.

Usage

Initialization

uses OpenAI;

var OpenAI := TOpenAIComponent.Create(Self, API_TOKEN);

or

uses OpenAI;

var OpenAI: IOpenAI := TOpenAI.Create(API_TOKEN);

Examples

Sources: Playground (FMX)

Sources: ChatGPT (FMX)

Sources: DALL-E (FMX)

Models

var Models := OpenAI.Model.List();
try
  for var Model in Models.Data do
    MemoChat.Lines.Add(Model.Id);
finally
  Models.Free;
end;

Completaions (for chat)

var Completions := OpenAI.Completion.Create(
  procedure(Params: TCompletionParams)
  begin
    Params.Prompt(MemoPrompt.Text);
    Params.MaxTokens(2048);
  end);
try
  for var Choice in Completions.Choices do
    MemoChat.Lines.Add(Choice.Index.ToString + ' ' + Choice.Text);
finally
  Completions.Free;
end;

Images Generations

var Images := OpenAI.Image.Create(
  procedure(Params: TImageGenParams)
  begin
    Params.Prompt(MemoPrompt.Text);
    Params.ResponseFormat('url');
  end);
try
  for var Image in Images.Data do
    Image1.Bitmap.LoadFromUrl(Image.Url);
finally
  Images.Free;
end;

Error handling

try
  var Images := OpenAI.Image.Create(...);
except
  on E: OpenAIException do
    ShowError('OpenAI Error: ' + E.Message);
end;

Requirements

This library does not require any 3rd party library. It works on recent Delphi versions (10.3+). Althought not fully tested, it should also work on all supported platforms (Windows, Linux, macOS, Android, iOS).

Since the library requires your secret API key, it's not recommended you use it on client applications, as your secret key will be exposed, unless you are sure about the security risks.

About

OpenAI API client for Delphi. Use ChatGPT, DALL-E and other products.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Pascal 100.0%