DelphiDeepseek icon indicating copy to clipboard operation
DelphiDeepseek copied to clipboard

Need examples.

Open rgreat opened this issue 8 months ago • 9 comments

Please add working examples, which you can compile and run.

rgreat avatar May 05 '25 13:05 rgreat

Thank you for your feedback.

The README is designed as a step-by-step tutorial that guides the user from start to finish, enabling them to build an application while acquiring all the skills needed to implement the APIs from A to Z.

Let’s assume you want to test the APIs using VCL:

  1. As indicated in Wrapper Tools Info:

    • Create a new VCL application. On the main form, place four (4) TMemo components and one (1) TButton component.

    • In the form’s OnCreate event, insert the following code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
  var BarearKey := 'YOU_DEEPSEEK_KEY';
  DeepSeek := TDeepseekFactory.CreateInstance(BarearKey);
  DeepSeekBeta := TDeepseekFactory.CreateBetaInstance(BarearKey);
  TutorialHub := TVCLTutorialHub.Create(Deepseek, Memo1, Memo2, Memo3, Memo4, Button1);
  Width := 1800;
  Height := 1000;
end;  
  • In the private section of your class, declare the DeepSeek and DeepSeekBeta fields as follows (and this code):
uses
  ....
  Deepseek, Deepseek.Types, Deepseek.Tutorial.VCL, Deepseek.Functions.Example, Deepseek.Async.Promise;

TForm1 = class(TForm)
    ...
   procedure FormCreate(Sender: TObject);
   ...
  private
    DeepSeek: IDeepseek;
    DeepSeekBeta: IDeepseek;
  public
    procedure DisplayWeather(const Value: string);
  end;  

implementation

procedure TForm1.DisplayWeather(const Value: string);
begin
  //Asynchronous example
  DeepSeek.Chat.ASynCreateStream(
    procedure (Params: TChatParams)
    begin
      Params.Model('deepseek-chat');
      Params.Messages([
        FromSystem('You are a star weather presenter on a national TV channel.'),
        FromUser(Value)
      ]);
      Params.MaxTokens(1024);
      Params.Stream;
    end,
    function : TAsynChatStream
    begin
      Result.Sender := TutorialHub;
      Result.OnProgress := DisplayStream;
      Result.OnError := Display;
      Result.OnDoCancel := DoCancellation;
      Result.OnCancellation := Cancellation;
    end);
end;  

  • Add a new TButton, and in its OnClick event, paste the code provided in the Chat/Create a message section.
procedure TForm1.Button2Click(Sender: TObject);
begin
  TutorialHub.Clear;
  Deepseek.ClientHttp.ResponseTimeout := 120000;

  //Asynchronous example
  DeepSeek.Chat.AsynCreate(
    procedure (Params: TChatParams)
    begin
      Params.Model('deepseek-chat');
      Params.Messages([
        FromUser('What is the capital of France, and then the capital of champagne?')
      ]);
      TutorialHub.JSONRequest := Params.ToFormat();
    end,
    function : TAsynChat
    begin
      Result.Sender := TutorialHub;
      Result.OnStart := Start;
      Result.OnSuccess := Display;
      Result.OnError := Display;
    end);

  //Synchronous example
//  var Value := DeepSeek.Chat.Create(
//    procedure (Params: TChatParams)
//    begin
//      Params.Model('deepseek-chat');
//      Params.Messages([
//        FromUser('What is the capital of France, and then the capital of champagne?')
//      ]);
//      TutorialHub.JSONRequest := Params.ToFormat();
//    end);
//  try
//    Display(TutorialHub, Value);
//  finally
//    Value.Free;
//  end;
end;
  1. Compile and run the application: you now have your first code example.

For each additional code example I provide, simply add a new button to your form, copy and paste the provided code into its event handler, and it will be ready to run.

Regards

MaxiDonkey avatar May 05 '25 15:05 MaxiDonkey

I kind of worked out something like that. Thanks.

Would be nice to have simpler example, without so much overhead and GUI limitations as in examples.

Simple console app, maybe? :)

Now trying to integrate into UniGUI application.

rgreat avatar May 05 '25 16:05 rgreat

Hi,

Here’s a working Delphi console example, based on how the DeepSeek wrapper exposes Chat.Create. The idea is to call Chat.Create, receive a TChat object, then iterate through its Choices list to display each message’s content.

program DeepseekConsoleDemo;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Deepseek,
  Deepseek.Types;

var
  DeepSeek: IDeepseek;
begin
  ReportMemoryLeaksOnShutdown := True;
  try

    DeepSeek := TDeepseekFactory.CreateInstance('YOUR_DEEPSEEK_KEY');

    Writeln('Envoi de la requête à DeepSeek...');
    Chat := DeepSeek.Chat.Create(
      procedure (Params: TChatParams)
      begin
        Params.Model('deepseek-chat');
        Params.MaxTokens(1024);
        Params.Messages([
          FromSystem('You are a helpful console assistant.'),
          FromUser('What is the capital of France, and then the capital of champagne?')
        ]);
      end
    );

    try
      Writeln('Response received :');
      for var Choice in Chat.Choices do
        Writeln('→ ', Choice.Message.Content);
    finally
      Chat.Free;
    end;
  except
    on E: Exception do
      Writeln('Erreur : ', E.ClassName, ' - ', E.Message);
  end;
end.

Asynchonous example:

program DeepseekConsoleDemo;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Deepseek,
  Deepseek.Types;

var
  DeepSeek: IDeepseek;
begin
  ReportMemoryLeaksOnShutdown := True;

  DeepSeek := TDeepseekFactory.CreateInstance('YOUR_DEEPSEEK_KEY');

  Writeln('Sending the request to DeepSeek...');
  DeepSeek.Chat.AsynCreate(
    procedure (Params: TChatParams)
    begin
      Params.Model('deepseek-chat');
      Params.Messages([FromUser('Hello in async!')]);
    end,
    function: TAsynChat
    begin
      Result.OnSuccess :=
        procedure(Sender: TObject; Chat: TChat)
        begin
          Writeln('Réponse async :');
          for var Choice in Chat.Choices do
            Writeln('→ ', Choice.Message.Content);
          Chat.Free;
        end;
      Result.OnError :=
        procedure(Sender: TObject; Msg: string)
        begin
          Writeln('Async error : ', Msg);
        end;
    end
  );

end.

How to use:

  1. Create a new Delphi Console Application.
  2. Paste this code into the .dpr file.
  3. Replace 'YOUR_DEEPSEEK_KEY' with your actual API key.
  4. Compile and run—you’ll see the request and the AI’s reply directly in the console.

I wrote this quickly, but the idea is there and it doesn’t affect the relevance of the examples provided in the README.

MaxiDonkey avatar May 05 '25 17:05 MaxiDonkey

Nice. Can you also add streaming multi-turn conversation example?

With user input through ReadLn.

rgreat avatar May 05 '25 17:05 rgreat

...
   Params.Messages([
        FromSystem('You are a funny domestic assistant.'),
        FromUser('Hello'),
        FromAssistant('Great to meet you. What would you like to know?'),
        FromUser('I have two dogs in my house. How many paws are in my house?')
      ]);
...

Multi-turn conversation

MaxiDonkey avatar May 05 '25 17:05 MaxiDonkey

That's not interactive.

I want to understand how to add new user questions in already present chat.

rgreat avatar May 05 '25 17:05 rgreat

You must maintain a history of queries and responses in a list, then reconstruct the parameter array using the FromUser and FromAssistant methods. Because the API does not handle interaction natively, this responsibility lies entirely with the developer.

MaxiDonkey avatar May 05 '25 18:05 MaxiDonkey

You must maintain a history of queries and responses in a list, then reconstruct the parameter array using the FromUser and FromAssistant methods. Because the API does not handle interaction natively, this responsibility lies entirely with the developer.

Oh. That's could be a lot of text to send. Thanks.

rgreat avatar May 05 '25 18:05 rgreat

To enable automatic multi-turn handling, you must call OpenAI’s v1/responses endpoint, supplying the ID of the previous response to preserve conversational context. This capability lies outside the scope of the DeepSeek API—please see my GenAI for OpenAI repository for an implementation example.

MaxiDonkey avatar May 05 '25 18:05 MaxiDonkey