DelphiGemini icon indicating copy to clipboard operation
DelphiGemini copied to clipboard

Unable to compile chat example

Open lovehyunju opened this issue 9 months ago • 1 comments

I followed the manual exactly as described, installed the library, and tried to run the provided example code, but none of the examples are working.

For example, methods like Gemini.Chat.Create do not exist.

I am getting the following errors:

[dcc32 Error] Unit1.pas(31): E2003 Undeclared identifier: 'Create'
[dcc32 Error] Unit1.pas(38): E2003 Undeclared identifier: 'Display'
[dcc32 Error] Unit1.pas(40): E2003 Undeclared identifier: 'Free'
[dcc32 Fatal Error] TestAI.dpr(9): F2063 Could not compile used unit 'Unit1.pas'
  • Could you please check if there is a mismatch between the manual and the actual library code?
  • If there is a correct usage example or updated code, I would appreciate it if you could provide it.
Image

lovehyunju avatar Jul 15 '25 10:07 lovehyunju

Hello and thank you for your feedback,

This error is expected given the code snippet you provided. To resolve it, apply the following changes:

1. Import the Gemini units in your interface section

Make sure to include all Gemini units up front so you have access to every API:

interface

uses
  ....,
  Gemini, Gemini.Models, Gemini.Embeddings, Gemini.Chat, Gemini.Safety, Gemini.Schema,
  Gemini.Tools, Gemini.Functions.Example, Gemini.Functions.Core, Gemini.Files,
  Gemini.Caching, Gemini.FineTunings;

2. Declare an IGemini client

In your main form’s class, add a private field of type IGemini, then obtain your API key from the Google platform: (refer to my documentation)

type
  TForm1 = class(TForm)
      ...
  private
     Gemini: IGemini;
  public
  end;

Instantiate the client in the OnCreate handler:

type
  TForm1 = class(TForm)
      ...
     procedure FormCreate(Sender: TObject);
     ---
  private
     Gemini: IGemini;
  public
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
  Gemini := TGeminiFactory.CreateInstance(Your_API_KEY);
end;

3. Send a chat request and display the response

Drop a button onto your form and implement its OnClick like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Text := 'Please wait...';
  var Chat := Gemini.Chat.Create('models/gemini-2.5-flash',
    procedure (Params: TChatParams)
    begin
      Params.Contents([TPayload.Add('Write a story about a magic backpack.')]);
    end);
  try
    for var Item in Chat.Candidates do
      begin
        if Item.FinishReason = STOP then
          for var SubItem in Item.Content.Parts do
            begin
              Memo1.Lines.Text := SubItem.Text;
            end;
      end;
  finally
    Chat.Free;
  end;
end;

After a few seconds, the generated text will appear in your TMemo. Let me know if you need anything else!

MaxiDonkey avatar Jul 16 '25 07:07 MaxiDonkey