Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.2.0+

AI: Simple Conversation

See more AI Examples

Initiates a conversation and sends two requests. The second request includes the transcript of the messages between the user and assistant from the first request.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  ai: TAi;
  systemMsg: string;
  developerMsg: string;
  conversationName: string;
  sbResponse: TStringBuilder;
  json: TJsonObject;

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 := 'claude';

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

  //  Choose a model.
  ai.Model := 'claude-opus-5';

  //  Create a new conversation to be maintained locally in memory.
  //  If the conversation is the first to be created, it is also automatically selected.
  systemMsg := 'You are a helpful assistant';
  developerMsg := 'Respond only with markdown';
  conversationName := 'test_conversation';
  ai.NewConvo(conversationName,systemMsg,developerMsg);

  //  -------------------------------------------------------------------------------------------
  //  Make the first request...
  //  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());
  WriteLn('----');

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

  //  Sample output:

  //  # Hello! 👋
  //  ## Welcome!
  //  It's **great** to meet you. How can I *assist* you today?
  //  ---
  //  Feel free to ask me anything, and I'll respond in `markdown` format.

  //  -------------------------------------------------------------------------------------------
  //  Send the 2nd request in the conversation.
  ai.InputAddText('Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious.');
  success := ai.Ask('text');
  if (success = False) then
    begin
      WriteLn(ai.LastErrorText);
      Exit;
    end;

  //  Get the text response.
  sbResponse.Clear();
  ai.GetOutputTextSb(sbResponse);
  WriteLn(sbResponse.GetAsString());

  //  Sample output:

  //  ## Wisdom from Confucius
  //  > *"The man who moves a mountain begins by carrying away small stones. It does not matter how slowly you go as long as you do not stop."*
  //  These words remind us that **persistence** and **patience** are the keys to achieving even the most daunting goals. 🏔️

  //  -------------------------------------------------------------------------------------------
  //  Look at the conversation so far...
  //  
  json := TJsonObject.Create;
  ai.ExportConvo(conversationName,json);
  json.EmitCompact := False;
  WriteLn(json.Emit());

  //  Note: The conversation transcript format exported and imported by Chilkat is always the OpenAI format,
  //  regardless of the AI provider.  (Chilkat translates the format from OpenAI to whatever is needed by the actual AI provider being used.)

  //  {
  //    "input": [
  //      {
  //        "role": "system",
  //        "content": [
  //          {
  //            "type": "input_text",
  //            "text": "You are a helpful assistant"
  //          }
  //        ]
  //      },
  //      {
  //        "role": "developer",
  //        "content": [
  //          {
  //            "type": "input_text",
  //            "text": "Respond only with markdown"
  //          }
  //        ]
  //      },
  //      {
  //        "role": "user",
  //        "content": [
  //          {
  //            "type": "input_text",
  //            "text": "Say Hello"
  //          }
  //        ]
  //      },
  //      {
  //        "role": "assistant",
  //        "content": [
  //          {
  //            "type": "output_text",
  //            "text": "# Hello! 👋\n\n**Welcome!** It's *great* to meet you.\n\nHow can I assist you today?"
  //          }
  //        ]
  //      },
  //      {
  //        "role": "user",
  //        "content": [
  //          {
  //            "type": "input_text",
  //            "text": "Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious."
  //          }
  //        ]
  //      },
  //      {
  //        "role": "assistant",
  //        "content": [
  //          {
  //            "type": "output_text",
  //            "text": "## Wisdom from Confucius\n\n> *\"The man who moves a mountain begins by carrying away small stones. It does not matter how slowly you go as long as you do not stop.\"*\n\nThese words remind us that **persistence** and **patience** are the keys to achieving even the most daunting goals. 🏔️"
  //          }
  //        ]
  //      }
  //    ]
  //  }


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