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

AI: Simple Stateless Query

See more AI Examples

Demonstrates the simplest stateless query: asking an AI to "Say Hello". This query is standalone and not part of a conversation.

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.Ai,
  Chilkat.StringBuilder;

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

procedure RunDemo;
var
  success: Boolean;
  ai: TAi;
  sbResponse: TStringBuilder;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  ai := TAi.Create;

  //  The provider can be "openai", "google", "claude", "deepseek", "xai", or "perplexity".
  //  Support for additional providers will be added in future versions of Chilkat.
  ai.Provider := 'openai';

  //  Use your provider's API key.
  ai.ApiKey := 'MY_API_KEY';

  //  Choose a model.
  ai.Model := 'gpt-4o';

  //  Add a text input.
  ai.InputAddText('Say Hello.');

  //  Ask the AI for text output.
  success := ai.Ask('text');
  if (success = False) then
    begin
      WriteLn(ai.LastErrorText);
      Exit;
    end;

  //  Get the text response.
  sbResponse := TStringBuilder.Create;
  ai.GetOutputTextSb(sbResponse);
  WriteLn(sbResponse.GetAsString());

  //  Sample output:
  //  Hello! How can I assist you today?

  //  -------------------------------------------------------------
  //  The response is in markdown format.
  //  Also see Markdown to HTML Conversion Examples.
  //  -------------------------------------------------------------


  ai.Free;
  sbResponse.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.