-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameAIAPIEditor.pas
More file actions
293 lines (255 loc) · 7.86 KB
/
FrameAIAPIEditor.pas
File metadata and controls
293 lines (255 loc) · 7.86 KB
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
unit FrameAIAPIEditor;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
ConfigFrameBase, System.JSON, JSONHelpers, UtilsTypes;
type
TAIAPIEditorFrame = class(TBaseConfigFrame)
pnlMain: TPanel;
lblProvider: TLabel;
cboProvider: TComboBox;
lblApiKey: TLabel;
edtApiKey: TEdit;
lblModel: TLabel;
edtModel: TEdit;
lblBaseURL: TLabel;
edtBaseURL: TEdit;
lblTimeout: TLabel;
edtTimeout: TEdit;
procedure cboProviderChange(Sender: TObject);
procedure edtChange(Sender: TObject);
private
procedure UpdateProviderFields;
protected
procedure LoadFromJSON; override;
procedure SaveToJSON; override;
procedure CreateControls; override;
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TAIAPIEditorFrame }
constructor TAIAPIEditorFrame.Create(AOwner: TComponent);
begin
inherited;
CreateControls;
end;
procedure TAIAPIEditorFrame.CreateControls;
begin
// 主面�? pnlMain := TPanel.Create(Self);
pnlMain.Parent := Self;
pnlMain.Align := alClient;
pnlMain.BevelOuter := bvNone;
pnlMain.Padding.SetBounds(10, 10, 10, 10);
// API提供�? lblProvider := TLabel.Create(Self);
lblProvider.Parent := pnlMain;
lblProvider.Top := 15;
lblProvider.Left := 10;
lblProvider.Caption := 'API提供�?';
cboProvider := TComboBox.Create(Self);
cboProvider.Parent := pnlMain;
cboProvider.Top := lblProvider.Top;
cboProvider.Left := 100;
cboProvider.Width := 200;
cboProvider.Style := csDropDownList;
cboProvider.Items.Add('OpenAI');
cboProvider.Items.Add('Azure OpenAI');
cboProvider.Items.Add('Anthropic');
cboProvider.Items.Add('Custom');
cboProvider.ItemIndex := 0;
cboProvider.OnChange := cboProviderChange;
// API密钥
lblApiKey := TLabel.Create(Self);
lblApiKey.Parent := pnlMain;
lblApiKey.Top := lblProvider.Top + 30;
lblApiKey.Left := 10;
lblApiKey.Caption := 'API密钥:';
edtApiKey := TEdit.Create(Self);
edtApiKey.Parent := pnlMain;
edtApiKey.Top := lblApiKey.Top;
edtApiKey.Left := 100;
edtApiKey.Width := 300;
edtApiKey.PasswordChar := '*';
edtApiKey.OnChange := edtChange;
// 模型名称
lblModel := TLabel.Create(Self);
lblModel.Parent := pnlMain;
lblModel.Top := lblApiKey.Top + 30;
lblModel.Left := 10;
lblModel.Caption := '模型名称:';
edtModel := TEdit.Create(Self);
edtModel.Parent := pnlMain;
edtModel.Top := lblModel.Top;
edtModel.Left := 100;
edtModel.Width := 300;
edtModel.Text := 'gpt-4';
edtModel.OnChange := edtChange;
// 基础URL
lblBaseURL := TLabel.Create(Self);
lblBaseURL.Parent := pnlMain;
lblBaseURL.Top := lblModel.Top + 30;
lblBaseURL.Left := 10;
lblBaseURL.Caption := '基础URL:';
lblBaseURL.Visible := False;
edtBaseURL := TEdit.Create(Self);
edtBaseURL.Parent := pnlMain;
edtBaseURL.Top := lblBaseURL.Top;
edtBaseURL.Left := 100;
edtBaseURL.Width := 300;
edtBaseURL.Text := 'https://api.openai.com/v1';
edtBaseURL.Visible := False;
edtBaseURL.OnChange := edtChange;
// 超时设置
lblTimeout := TLabel.Create(Self);
lblTimeout.Parent := pnlMain;
lblTimeout.Top := lblBaseURL.Top + 30;
lblTimeout.Left := 10;
lblTimeout.Caption := '超时(�?:';
edtTimeout := TEdit.Create(Self);
edtTimeout.Parent := pnlMain;
edtTimeout.Top := lblTimeout.Top;
edtTimeout.Left := 100;
edtTimeout.Width := 60;
edtTimeout.Text := '30';
edtTimeout.OnChange := edtChange;
// 初始化界�? UpdateProviderFields;
end;
procedure TAIAPIEditorFrame.LoadFromJSON;
var
Value: TJSONValue;
ProviderStr: string;
begin
if not Assigned(JSONObject) then
Exit;
// 加载API提供�? Value := JSONObject.GetValue('provider');
if Assigned(Value) and (Value is TJSONString) then
begin
ProviderStr := TJSONString(Value).Value;
if ProviderStr = 'openai' then
cboProvider.ItemIndex := 0
else if ProviderStr = 'azure' then
cboProvider.ItemIndex := 1
else if ProviderStr = 'anthropic' then
cboProvider.ItemIndex := 2
else if ProviderStr = 'custom' then
cboProvider.ItemIndex := 3
else
cboProvider.ItemIndex := 0;
end
else
cboProvider.ItemIndex := 0;
// 加载API密钥
Value := JSONObject.GetValue('api_key');
if Assigned(Value) and (Value is TJSONString) then
edtApiKey.Text := TJSONString(Value).Value
else
edtApiKey.Text := '';
// 加载模型名称
Value := JSONObject.GetValue('model');
if Assigned(Value) and (Value is TJSONString) then
edtModel.Text := TJSONString(Value).Value
else
edtModel.Text := 'gpt-4';
// 加载基础URL
Value := JSONObject.GetValue('base_url');
if Assigned(Value) and (Value is TJSONString) then
edtBaseURL.Text := TJSONString(Value).Value
else
edtBaseURL.Text := 'https://api.openai.com/v1';
// 加载超时设置
Value := JSONObject.GetValue('timeout');
if Assigned(Value) and (Value is TJSONNumber) then
edtTimeout.Text := TJSONNumber(Value).ToString
else
edtTimeout.Text := '30';
// 更新界面
UpdateProviderFields;
end;
procedure TAIAPIEditorFrame.SaveToJSON;
var
ProviderStr: string;
begin
if not Assigned(JSONObject) then
Exit;
// 保存API类型信息
if JSONObject.GetValue('_type') = nil then
JSONObject.AddPair('_type', ConfigTypeToString(ctAIAPI));
// 确定提供商字符串
case cboProvider.ItemIndex of
0: ProviderStr := 'openai';
1: ProviderStr := 'azure';
2: ProviderStr := 'anthropic';
3: ProviderStr := 'custom';
else ProviderStr := 'openai';
end;
// 保存API提供�? if JSONObject.GetValue('provider') <> nil then
JSONObject.RemovePair('provider');
JSONObject.AddPair('provider', ProviderStr);
// 保存API密钥
if JSONObject.GetValue('api_key') <> nil then
JSONObject.RemovePair('api_key');
JSONObject.AddPair('api_key', edtApiKey.Text);
// 保存模型名称
if JSONObject.GetValue('model') <> nil then
JSONObject.RemovePair('model');
JSONObject.AddPair('model', edtModel.Text);
// 保存基础URL
if JSONObject.GetValue('base_url') <> nil then
JSONObject.RemovePair('base_url');
JSONObject.AddPair('base_url', edtBaseURL.Text);
// 保存超时设置
if JSONObject.GetValue('timeout') <> nil then
JSONObject.RemovePair('timeout');
JSONObject.AddPair('timeout', TJSONNumber.Create(StrToIntDef(edtTimeout.Text, 30)));
end;
procedure TAIAPIEditorFrame.cboProviderChange(Sender: TObject);
begin
UpdateProviderFields;
Modified := True;
end;
procedure TAIAPIEditorFrame.edtChange(Sender: TObject);
begin
Modified := True;
end;
procedure TAIAPIEditorFrame.UpdateProviderFields;
begin
// 确保界面控件已创�? if not Assigned(cboProvider) then
Exit;
// 根据不同的API提供商显示不同的界面元素
case cboProvider.ItemIndex of
0: // OpenAI
begin
lblBaseURL.Visible := False;
edtBaseURL.Visible := False;
edtBaseURL.Text := 'https://api.openai.com/v1';
lblModel.Caption := '模型名称:';
edtModel.Text := 'gpt-4';
end;
1: // Azure OpenAI
begin
lblBaseURL.Visible := True;
edtBaseURL.Visible := True;
edtBaseURL.Text := 'https://your-resource-name.openai.azure.com/openai/deployments/your-deployment-name';
lblModel.Caption := '部署名称:';
edtModel.Text := 'your-deployment-name';
end;
2: // Anthropic
begin
lblBaseURL.Visible := False;
edtBaseURL.Visible := False;
edtBaseURL.Text := 'https://api.anthropic.com';
lblModel.Caption := '模型名称:';
edtModel.Text := 'claude-3-opus-20240229';
end;
3: // Custom
begin
lblBaseURL.Visible := True;
edtBaseURL.Visible := True;
edtBaseURL.Text := 'https://your-custom-api-url.com';
lblModel.Caption := '模型名称:';
edtModel.Text := 'custom-model';
end;
end;
end;
end.