Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.Ai;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ai: TAi;
  contentType: string;
  localFilePath: string;
  filenameOnServer: string;
  file_id: string;
  bd: TBinData;

begin
  success := False;

  ai := TAi.Create;

  //  Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
  ai.Provider := 'openai';
  //  Use your provider's API key.
  ai.ApiKey := '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 := ai.UploadFile(localFilePath,contentType);
  if (ai.LastMethodSuccess = False) then
    begin
      WriteLn(ai.LastErrorText);
      WriteLn('AI File Upload Failed.');
    end
  else
    begin
      WriteLn('File ID: ' + file_id);
      WriteLn('File uploaded.');
    end;

  //  Upload from the contents of a Chilkat BinData
  bd := TBinData.Create;
  success := bd.LoadFile(localFilePath);
  if (success = False) then
    begin
      WriteLn(bd.LastErrorText);
      Exit;
    end;
  file_id := ai.UploadFileBd(bd,filenameOnServer,contentType);
  if (ai.LastMethodSuccess = False) then
    begin
      WriteLn(ai.LastErrorText);
      WriteLn('AI File Upload Failed.');
    end
  else
    begin
      WriteLn('File ID: ' + file_id);
      WriteLn('File uploaded.');
    end;


  ai.Free;
  bd.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.