![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi ActiveX) Upload a File to an AI Provider (OpenAI, Google, Antropic, X)See more AI ExamplesUploads 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.Note: This example requires Chilkat v11.4.0 or greater.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB; ... procedure TForm1.Button1Click(Sender: TObject); var success: Integer; ai: TChilkatAi; contentType: WideString; localFilePath: WideString; filenameOnServer: WideString; file_id: WideString; sb: TChilkatStringBuilder; bd: TChilkatBinData; begin success := 0; ai := TChilkatAi.Create(Self); // 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 = 0) then begin Memo1.Lines.Add(ai.LastErrorText); 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 StringBuilder sb := TChilkatStringBuilder.Create(Self); success := sb.LoadFile(localFilePath,'utf-8'); if (success = 0) then begin Memo1.Lines.Add(sb.LastErrorText); Exit; end; file_id := ai.UploadFileSb(sb.ControlInterface,filenameOnServer,contentType); if (ai.LastMethodSuccess = 0) then begin Memo1.Lines.Add(ai.LastErrorText); 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 BinData bd := TChilkatBinData.Create(Self); success := bd.LoadFile(localFilePath); if (success = 0) then begin Memo1.Lines.Add(bd.LastErrorText); Exit; end; file_id := ai.UploadFileBd(bd.ControlInterface,filenameOnServer,contentType); if (ai.LastMethodSuccess = 0) then begin Memo1.Lines.Add(ai.LastErrorText); Memo1.Lines.Add('AI File Upload Failed.'); end else begin Memo1.Lines.Add('File ID: ' + file_id); Memo1.Lines.Add('File uploaded.'); end; end; |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.