-
Notifications
You must be signed in to change notification settings - Fork 4
/
uframe_question.pas
148 lines (125 loc) · 3.58 KB
/
uframe_question.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
unit UFrame_Question;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls, ComCtrls, Dialogs,
UThread, USetting, Clipbrd, Menus, ButtonPanel, EditBtn, Buttons, RTTICtrls;
type
{ TFrame1 }
TFrame1 = class(TFrame)
btnAsk: TButton;
btnClipBoard: TButton;
chkAutoCopy: TCheckBox;
Label1: TLabel;
Label2: TLabel;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
mmoQuestion: TMemo;
mmoAnswer: TMemo;
pnlQuestion: TPanel;
pnlAnswer: TPanel;
pnlBottom: TPanel;
pnlTop: TPanel;
pnlCenter: TPanel;
pnlMain: TPanel;
PopupClear: TPopupMenu;
ProgressBar: TProgressBar;
btnClear: TSpeedButton;
Splitter1: TSplitter;
procedure btnAskClick(Sender: TObject);
procedure btnClearClick(Sender: TObject);
procedure btnClipBoardClick(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
procedure MenuItem3Click(Sender: TObject);
procedure mmoQuestionKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
procedure ShowAnswer(AAnswer: string; AAnimated: Boolean; ADone: Boolean); // This is an event that will be called inworker Thread to update UI.
public
FAnswer: TStringList;
procedure CallThread(AQuestion: String);
end;
implementation
{$R *.lfm}
{ TFrame1 }
procedure TFrame1.btnAskClick(Sender: TObject);
begin
if TSingletonSettingObj.Instance.ApiKey.IsEmpty then
begin
ShowMessage('You need an API key, please fill the setting parameters in setting form.');
Frm_Setting := TFrm_Setting.Create(Application);
try
Frm_Setting.ChangeFocus := True;
Frm_Setting.ShowModal;
finally
Frm_Setting.Free;
end;
Exit;
end;
if mmoQuestion.Lines.Text.Trim.IsEmpty then
begin
ShowMessage('You need to type a question first!');
if mmoQuestion.CanFocus then
mmoQuestion.SetFocus;
Exit;
end;
mmoAnswer.Lines.Clear;
CallThread(mmoQuestion.Lines.Text);
ProgressBar.Visible := True;
end;
procedure TFrame1.btnClearClick(Sender: TObject);
begin
PopupClear.PopUp;
end;
procedure TFrame1.btnClipBoardClick(Sender: TObject);
begin
Clipboard.SetTextBuf(PChar(mmoAnswer.Lines.Text));
end;
procedure TFrame1.MenuItem1Click(Sender: TObject);
begin
mmoQuestion.Clear;
end;
procedure TFrame1.MenuItem2Click(Sender: TObject);
begin
mmoAnswer.Clear;
end;
procedure TFrame1.MenuItem3Click(Sender: TObject);
begin
mmoQuestion.Clear;
mmoAnswer.Clear;
end;
procedure TFrame1.mmoQuestionKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Shift = [ssCtrl]) and (Ord(Key) = 13) then
btnAsk.Click;
end;
procedure TFrame1.ShowAnswer(AAnswer: string; AAnimated: Boolean; ADone: Boolean);
begin
if AAnimated then
begin
if AAnswer = #13 then
mmoAnswer.Lines.Add('')
else
mmoAnswer.Lines[Pred(mmoAnswer.Lines.Count)] := mmoAnswer.Lines[Pred(mmoAnswer.Lines.Count)] + AAnswer
end
else
mmoAnswer.Lines.Add(AAnswer);
ProgressBar.Visible := not ADone;
if (ADone) and (chkAutoCopy.Checked) then
btnClipBoard.Click;
end;
procedure TFrame1.CallThread(AQuestion: String);
var
LvChatGPTTrd: TExecutorTrd;
LvSetting: TSingletonSettingObj;
begin
LvSetting := TSingletonSettingObj.Instance;
LvChatGPTTrd := TExecutorTrd.Create(LvSetting.ApiKey, LvSetting.Model, AQuestion, LvSetting.URL, LvSetting.MaxToken,
LvSetting.Temperature, LvSetting.AnimatedLetters, LvSetting.TimeOut);
LvChatGPTTrd.OnShowAnswer := @ShowAnswer;
LvChatGPTTrd.Start;
end;
end.