-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusetting.pas
303 lines (261 loc) · 8.86 KB
/
usetting.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
unit USetting;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
fgl, UConsts, fpjson, IniFiles;
const
DefaultChatGPTURL = 'https://api.openai.com/v1/completions';
DefaultChatGPT3_5TurboURL = 'https://api.openai.com/v1/chat/completions';
DefaultModel = 'text-davinci-003';
DefaultMaxToken = 2048;
DefaultTemperature = 0;
type
{ TSingletonSettingObj }
TSingletonSettingObj = class(TObject)
private
FApiKey: string;
FURL: string;
FModel: string;
FMaxToken: Integer;
FTemperature: Integer;
FIdentifier: string;
FCodeFormatter: Boolean;
FHistoryEnabled: Boolean;
FHistoryPath: string;
FShouldReloadHistory: Boolean;
FHighlightColor: TColor;
FAnimatedLetters: Boolean;
FTimeOut: Integer;
FLastQuestion: string;
FTaskList: specialize TFPGList<string>;
class var FInstance: TSingletonSettingObj;
class function GetInstance: TSingletonSettingObj; static;
procedure LoadDefaults;
public
constructor Create;
destructor Destroy; override;
procedure ReadConfig;
procedure WriteConfig;
function GetSetting: string;
function GetHistoryFullPath: string;
class function IsValidJson(const AJsonString: string): Boolean;
class property Instance: TSingletonSettingObj read GetInstance;
property ApiKey: string read FApiKey write FApiKey;
property URL: string read FURL write FURL;
property Model: string read FModel write FModel;
property MaxToken: Integer read FMaxToken write FMaxToken;
property Temperature: Integer read FTemperature write FTemperature;
property CodeFormatter: Boolean read FCodeFormatter write FCodeFormatter;
property Identifier: string read FIdentifier write FIdentifier;
property HistoryEnabled: Boolean read FHistoryEnabled write FHistoryEnabled;
property HistoryPath: string read FHistoryPath write FHistoryPath;
property ShouldReloadHistory: Boolean read FShouldReloadHistory write FShouldReloadHistory;
property HighlightColor: TColor read FHighlightColor write FHighlightColor;
property AnimatedLetters: Boolean read FAnimatedLetters write FAnimatedLetters;
property TimeOut: Integer read FTimeOut write FTimeOut;
property LastQuestion: string read FLastQuestion write FLastQuestion;
property TaskList: specialize TFPGList<string> read FTaskList write FTaskList;
end;
{ TFrm_Setting }
TFrm_Setting = class(TForm)
Btn_Default: TButton;
Btn_Save: TButton;
cbbModel: TComboBox;
chk_AnimatedLetters: TCheckBox;
edt_ApiKey: TEdit;
edt_MaxToken: TEdit;
edt_Temperature: TEdit;
edt_Url: TEdit;
grp_OpenAI: TGroupBox;
edtTimeout: TLabeledEdit;
lbl_1: TLabel;
lbl_2: TLabel;
lbl_3: TLabel;
lbl_4: TLabel;
lbl_5: TLabel;
pnlMain: TPanel;
pnlBottom: TPanel;
pnlOpenAI: TPanel;
procedure Btn_DefaultClick(Sender: TObject);
procedure Btn_SaveClick(Sender: TObject);
procedure cbbModelChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
private
procedure LoadDefaults;
public
ChangeFocus: Boolean;
end;
var
Frm_Setting: TFrm_Setting;
implementation
{$R *.lfm}
{ TFrm_Setting }
procedure TFrm_Setting.Btn_DefaultClick(Sender: TObject);
begin
LoadDefaults;
end;
procedure TFrm_Setting.Btn_SaveClick(Sender: TObject);
begin
with TSingletonSettingObj.Instance do
begin
ApiKey := edt_ApiKey.Text;
URL := edt_Url.Text;
Model := cbbModel.Text;
MaxToken := StrToInt(edt_MaxToken.Text);
Temperature := StrToInt(edt_Temperature.Text);
AnimatedLetters := chk_AnimatedLetters.Checked;
TimeOut := StrToInt(edtTimeout.Text);
LastQuestion := 'Create a class to make a Zip file in Delphi.';
WriteConfig;
end;
Close;
end;
procedure TFrm_Setting.cbbModelChange(Sender: TObject);
begin
if (cbbModel.ItemIndex in [4, 5]) and (edt_Url.Text = DefaultChatGPTURL) then
begin
if MessageDlg('gpt-3.5-turbo uses a different base URL, would you like me to set it automatically?' + #13 +
'The base URL should be something like the following URL but in case it doesn''t work ' + #13 +
'Please visit the online documentation from OpenAI in this regard' + #13 +
'Base URL: https://api.openai.com/v1/chat/completions',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
edt_Url.Text := DefaultChatGPT3_5TurboURL;
end;
if not(cbbModel.ItemIndex in [4, 5]) and (edt_Url.Text = DefaultChatGPT3_5TurboURL) then
begin
if MessageDlg('Any model except "gpt-3.5-turbo" uses a different base URL, would you like me to set it automatically?' + #13 +
'The base URL should be something like the following URL but in case it doesn''t work ' + #13 +
'Please visit the online documentation from OpenAI in this regard' + #13 +
'Base URL: https://api.openai.com/v1/completions',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
edt_Url.Text := DefaultChatGPTURL;
end;
end;
procedure TFrm_Setting.FormCreate(Sender: TObject);
begin
ChangeFocus := False;
end;
procedure TFrm_Setting.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Ord(Key) = 27 then
Close;
end;
procedure TFrm_Setting.FormShow(Sender: TObject);
begin
if ChangeFocus then
begin
edt_ApiKey.Color := clYellow;
edt_ApiKey.SetFocus;
end;
end;
procedure TFrm_Setting.LoadDefaults;
begin
edt_ApiKey.Text := '';
edt_Url.Text := DefaultChatGPTURL;
cbbModel.Text := DefaultModel;
edt_MaxToken.Text := IntToStr(DefaultMaxToken);
edt_Temperature.Text := IntToStr(DefaultTemperature);
chk_AnimatedLetters.Checked := True;
edtTimeout.Text := '20';
end;
{ TSingletonSettingObj }
class function TSingletonSettingObj.GetInstance: TSingletonSettingObj;
begin
if not Assigned(FInstance) then
FInstance := TSingletonSettingObj.Create;
Result := FInstance;
end;
constructor TSingletonSettingObj.Create;
begin
inherited;
FTaskList := specialize TFPGList<string>.Create;
LoadDefaults;
end;
destructor TSingletonSettingObj.Destroy;
begin
FTaskList.Free;
inherited Destroy;
end;
procedure TSingletonSettingObj.LoadDefaults;
begin
FApiKey := '';
FURL := DefaultChatGPTURL;
FModel := DefaultModel;
FMaxToken := DefaultMaxToken;
FTemperature := DefaultTemperature;
FHistoryEnabled := False;
FShouldReloadHistory := False;
FHistoryPath := '';
FHighlightColor := clRed;
FAnimatedLetters := True;
FTimeOut := 20;
FLastQuestion := 'Create a class to make a Zip file in Delphi.';
end;
procedure TSingletonSettingObj.ReadConfig;
var
LvIniFile: TIniFile;
begin
LvIniFile := TIniFile.Create(GetAppConfigDir(False) + 'ChatGPTConfigFile');
try
FURL := LvIniFile.ReadString('ChatGPT', 'URL', DefaultChatGPTURL);
FApiKey := LvIniFile.ReadString('ChatGPT', 'APIKey', '');
FModel := LvIniFile.ReadString('ChatGPT', 'Model', DefaultModel);
FMaxToken := LvIniFile.ReadInteger('ChatGPT', 'MaxToken', DefaultMaxToken);
FTemperature := LvIniFile.ReadInteger('ChatGPT', 'Temperature', DefaultTemperature);
FAnimatedLetters := LvIniFile.ReadBool('ChatGPT', 'AnimatedLetters', True);
FTimeOut := LvIniFile.ReadInteger('ChatGPT', 'TimeOut', 20);
FLastQuestion := LvIniFile.ReadString('ChatGPT', 'LastQuestion', 'Create a class in Lazarus to manage reading and writing XML files');
finally
LvIniFile.Free;
end;
end;
procedure TSingletonSettingObj.WriteConfig;
var
LvIniFile: TIniFile;
begin
LvIniFile := TIniFile.Create(GetAppConfigDir(False) + 'ChatGPTConfigFile');
try
LvIniFile.WriteString('ChatGPT', 'URL', FURL);
LvIniFile.WriteString('ChatGPT', 'APIKey', FApiKey);
LvIniFile.WriteString('ChatGPT', 'Model', FModel);
LvIniFile.WriteInteger('ChatGPT', 'MaxToken', FMaxToken);
LvIniFile.WriteInteger('ChatGPT', 'Temperature', FTemperature);
LvIniFile.WriteBool('ChatGPT', 'AnimatedLetters', FAnimatedLetters);
LvIniFile.WriteInteger('ChatGPT', 'TimeOut', FTimeOut);
finally
LvIniFile.Free;
end;
end;
function TSingletonSettingObj.GetSetting: string;
begin
Result := EmptyStr;
ShowMessage('You need an API key, please fill the setting parameters in setting form.');
Frm_Setting := TFrm_Setting.Create(nil);
try
Frm_Setting.ShowModal;
finally
FreeAndNil(Frm_Setting);
end;
Result := TSingletonSettingObj.Instance.ApiKey;
end;
function TSingletonSettingObj.GetHistoryFullPath: string;
begin
Result := FHistoryPath + '\History.sdb';
end;
class function TSingletonSettingObj.IsValidJson(const AJsonString: string): Boolean;
var
LvJsonObj: TJSONObject;
begin
Result := False;
try
LvJsonObj := TJSONObject(GetJSON(AJsonString));
Result := Assigned(LvJsonObj); // If parsing succeeds, JSON is valid
LvJsonObj.Free;
except
Result := False;
end;
end;
end.