Sample code for 30+ languages & platforms
Delphi DLL

Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads an image file to an AI provider using the provider's File API and returns the id that can later be used to reference the file in an query. This currently works with ChatGPT and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, BinData, Ai;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
ai: HCkAi;
contentType: PWideChar;
localFilePath: PWideChar;
filenameOnServer: PWideChar;
file_id: PWideChar;
bd: HCkBinData;

begin
success := False;

ai := CkAi_Create();

// Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
CkAi_putProvider(ai,'openai');
// Use your provider's API key.
CkAi_putApiKey(ai,'MY_API_KEY');

// We can upload directly from a file in the filesystem, or from a Chilkat BinData.

contentType := 'image/jpeg';
localFilePath := 'qa_data/jpg/starfish.jpg';
filenameOnServer := 'starfish.jpg';

// Upload directly from a file.
file_id := CkAi__uploadFile(ai,localFilePath,contentType);
if (CkAi_getLastMethodSuccess(ai) = False) then
  begin
    Memo1.Lines.Add(CkAi__lastErrorText(ai));
    Memo1.Lines.Add('AI File Upload Failed.');
  end
else
  begin
    Memo1.Lines.Add('File ID: ' + file_id);
    Memo1.Lines.Add('File uploaded.');
  end;

// Upload from the contents of a Chilkat BinData
bd := CkBinData_Create();
success := CkBinData_LoadFile(bd,localFilePath);
if (success = False) then
  begin
    Memo1.Lines.Add(CkBinData__lastErrorText(bd));
    Exit;
  end;
file_id := CkAi__uploadFileBd(ai,bd,filenameOnServer,contentType);
if (CkAi_getLastMethodSuccess(ai) = False) then
  begin
    Memo1.Lines.Add(CkAi__lastErrorText(ai));
    Memo1.Lines.Add('AI File Upload Failed.');
  end
else
  begin
    Memo1.Lines.Add('File ID: ' + file_id);
    Memo1.Lines.Add('File uploaded.');
  end;

CkAi_Dispose(ai);
CkBinData_Dispose(bd);

end;