Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Upload a File to an AI Provider (OpenAI, Google, Antropic, X)
See more AI Examples
Uploads a file to an AI provider using the provider's File API and returns theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.
Chilkat Pascal (Lazarus/Delphi) Downloads
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.StringBuilder,
Chilkat.Ai,
Chilkat.BinData;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ai: TAi;
contentType: string;
localFilePath: string;
filenameOnServer: string;
file_id: string;
sb: TStringBuilder;
bd: TBinData;
begin
success := False;
ai := TAi.Create;
// The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by 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 StringBuilder, or from a Chilkat BinData.
// Some AI providers require a content-type.
// Also, some AI providers are picky about what content-type's are accepted.
// Check the AI provider's documentation.
// "application/json" is generally always acceptable.
// "text/plain" can be used as a fallback for any text file.
contentType := 'application/json';
localFilePath := 'qa_data/hamlet.json';
filenameOnServer := 'hamlet.json';
// 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 StringBuilder
sb := TStringBuilder.Create;
success := sb.LoadFile(localFilePath,'utf-8');
if (success = False) then
begin
WriteLn(sb.LastErrorText);
Exit;
end;
file_id := ai.UploadFileSb(sb,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;
// Upload from the contents of a 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;
sb.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.