pyscripter / ChatLLM

Simple chat application for querying local and cloud LLM models.
MIT License
17 stars 3 forks source link

Connection issue ? #3

Closed SkybuckFlying closed 1 month ago

SkybuckFlying commented 1 month ago

To circumvent the complexities of toolbar2000, exe risks, tbx etc, I wrote this simple app code, however last time I tried it connect issues, not sure why ?

Was it setup properly ?


unit UnitMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  LLMSupport, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    RichEditResponse: TRichEdit;
    RichEditQuestion: TRichEdit;
    Splitter1: TSplitter;
    Panel1: TPanel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }

    procedure OnLLMResponse(Sender: TObject; const Question, Answer: string);
    procedure OnLLMError(Sender: TObject; const Error: string);
  public
    { Public declarations }
    LLMChat: TLLMChat;
  end;

var
  Form1: TForm1;

  OllamaSettings: TLLMSettings = (
    EndPoint: 'http://localhost:11434/api/chat';
    ApiKey: '';
    Model: 'llama3.2:latest';
//    Model: 'codegema';
    //Model: 'starcoder2';
    //Model: 'codellama:';
    //Model: 'stable-code';
    TimeOut: 60000;
    MaxTokens: 1000;
    SystemPrompt: 'You are my expert delphi coding assistant.');

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if RichEditQuestion.Text = '' then
    Exit;
  LLMChat.Ask(RichEditQuestion.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  LLMChat := TLLMChat.Create(OllamaSettings);
  LLMChat.OnLLMError := OnLLMError;
  LLMChat.OnLLMResponse := OnLLMResponse;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  LLMChat.Free;
end;

procedure TForm1.OnLLMError(Sender: TObject; const Error: string);
begin
    RichEditResponse.Lines.Add(Error);
end;

procedure TForm1.OnLLMResponse(Sender: TObject; const Question,
  Answer: string);
begin
    RichEditResponse.Lines.Add(Question);
    RichEditResponse.Lines.Add(Answer);
end;

end.
SkybuckFlying commented 1 month ago

Never mind, today it's working, thx ! =D

I guess I didn't run ollama properly the first time ! ;) =D