-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathModelAccess.tsx
More file actions
389 lines (378 loc) · 10.2 KB
/
ModelAccess.tsx
File metadata and controls
389 lines (378 loc) · 10.2 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import {
Card,
Button,
Form,
Input,
Modal,
Select,
Table,
Tooltip,
Popconfirm,
message, Switch,
} from "antd";
import {
EditOutlined,
DeleteOutlined,
ReloadOutlined,
PlusOutlined,
} from "@ant-design/icons";
import { useEffect, useState } from "react";
import { SearchControls } from "@/components/SearchControls";
import useFetchData from "@/hooks/useFetchData";
import {
createModelUsingPost,
deleteModelByIdUsingDelete,
queryModelListUsingGet,
queryModelProvidersUsingGet,
updateModelByIdUsingPut,
} from "./settings.apis";
export interface ModelI {
id: string;
modelName: string;
provider: string;
model: string;
apiKey: string;
type: string;
isEnabled: boolean;
createdAt: string;
updatedAt: string;
createdBy: string;
updatedBy: string;
}
interface ProviderI {
id: string;
modelName: string;
value: string;
label: string;
baseUrl: string;
provider: string;
apiKey: string;
type: string;
isEnabled: boolean;
}
export default function EnvironmentAccess() {
const [form] = Form.useForm();
const [showModelDialog, setShowModelDialog] = useState(false);
const [isEditMode, setIsEditMode] = useState(false);
const [newModel, setNewModel] = useState({
name: "",
provider: "openai",
model: "",
apiKey: "",
endpoint: "",
});
const [typeOptions] = useState([
{ value: "CHAT", label: "CHAT" },
{ value: "EMBEDDING", label: "EMBEDDING" },
]);
const {
loading,
tableData,
pagination,
searchParams,
setSearchParams,
fetchData,
handleFiltersChange,
} = useFetchData(queryModelListUsingGet);
const handleAddModel = async () => {
try {
const formValues = await form.validateFields();
const fn = isEditMode
? () => updateModelByIdUsingPut(newModel.id, formValues)
: () => createModelUsingPost(formValues);
await fn();
setShowModelDialog(false);
fetchData();
message.success("模型添加成功");
} catch (error) {
message.error(`${error?.data?.message}:${error?.data?.data}`);
}
};
const [providerOptions, setProviderOptions] = useState<ProviderI[]>([]);
const fetchProviderOptions = async () => {
const { data } = await queryModelProvidersUsingGet();
setProviderOptions(
data.map((provider: ProviderI) => ({
...provider,
value: provider.provider,
label: provider.provider,
}))
);
};
const generateApiKey = () => {
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "sk-";
for (let i = 0; i < 48; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
};
const handleDeleteModel = async (modelId: string) => {
await deleteModelByIdUsingDelete(modelId);
fetchData();
};
useEffect(() => {
fetchProviderOptions();
}, []);
const columns = [
{
title: "模型名称",
dataIndex: "modelName",
key: "modelName",
fixed: "left",
width: 200,
ellipsis: true,
},
{
title: "创建时间",
dataIndex: "createdAt",
key: "createdAt",
ellipsis: true,
},
{
title: "模型提供商",
dataIndex: "provider",
key: "provider",
ellipsis: true,
},
{
title: "模型类型",
dataIndex: "type",
key: "type",
ellipsis: true,
},
{
title: "更新时间",
dataIndex: "updatedAt",
key: "updatedAt",
ellipsis: true,
},
{
title: "操作",
key: "action",
fixed: "right" as const,
ellipsis: true,
render: (_: any, record: ModelI) => {
return [
{
key: "edit",
label: "编辑",
icon: <EditOutlined />,
onClick: () => {
setIsEditMode(true);
setNewModel(record);
form.setFieldsValue(record);
setShowModelDialog(true);
},
},
{
key: "delete",
label: "删除",
danger: true,
icon: <DeleteOutlined />,
confirm: {
title: "确定要删除该任务吗?此操作不可撤销。",
okText: "删除",
cancelText: "取消",
okType: "danger",
},
onClick: () => handleDeleteModel(record.id),
},
].map((op) => {
const button = (
<Tooltip key={op.key} title={op.label}>
<Button
type="text"
icon={op.icon}
danger={op?.danger}
onClick={() => op.onClick(record)}
/>
</Tooltip>
);
if (op.confirm) {
return (
<Popconfirm
key={op.key}
title={op.confirm.title}
okText={op.confirm.okText}
cancelText={op.confirm.cancelText}
okType={op.danger ? "danger" : "primary"}
onConfirm={() => op.onClick(record)}
>
<Tooltip key={op.key} title={op.label}>
<Button type="text" icon={op.icon} danger={op?.danger} />
</Tooltip>
</Popconfirm>
);
}
return button;
});
},
},
];
return (
<>
<div className="flex items-top justify-between">
<h2 className="text-lg font-medium mb-4">模型接入</h2>
<Button
type="primary"
icon={<PlusOutlined />}
onClick={() => {
setIsEditMode(false);
form.resetFields();
setNewModel({
name: "",
provider: "",
model: "",
apiKey: "",
endpoint: "",
});
setShowModelDialog(true);
}}
>
添加模型
</Button>
</div>
<SearchControls
searchTerm={searchParams.keyword}
onSearchChange={(newSearchTerm) =>
setSearchParams((prev) => ({
...prev,
keyword: newSearchTerm,
current: 1,
}))
}
searchPlaceholder="搜索模型描述..."
filters={[
{
key: "provider",
label: "模型提供商",
options: [{ value: "all", label: "全部" }, ...providerOptions],
},
{
key: "type",
label: "模型类型",
options: [{ value: "all", label: "全部" }, ...typeOptions],
},
]}
onFiltersChange={handleFiltersChange}
showViewToggle={false}
onReload={fetchData}
onClearFilters={() =>
setSearchParams((prev) => ({
...prev,
filters: {},
}))
}
className="mb-4"
/>
<Card>
<Table
rowKey="id"
columns={columns}
dataSource={tableData}
loading={loading}
pagination={pagination}
scroll={{ x: "max-content", y: "calc(100vh - 26rem)" }}
/>
</Card>
<Modal
open={showModelDialog}
onCancel={() => setShowModelDialog(false)}
title={isEditMode ? "编辑模型" : "添加模型"}
footer={[
<Button key="cancel" onClick={() => setShowModelDialog(false)}>
取消
</Button>,
<Button key="ok" type="primary" onClick={handleAddModel}>
确定
</Button>,
]}
>
<Form
form={form}
onValuesChange={(changedValues) => {
setNewModel({ ...newModel, ...changedValues });
}}
layout="vertical"
>
<Form.Item
name="provider"
label="服务提供商"
required
rules={[{ required: true, message: "请选择服务提供商" }]}
>
<Select
placeholder="选择服务提供商"
options={providerOptions}
onChange={(value) => {
const selectedProvider = providerOptions.find(
(p) => p.value === value
);
form.setFieldsValue({ baseUrl: selectedProvider?.baseUrl });
}}
></Select>
</Form.Item>
<Form.Item
name="baseUrl"
label="接口地址"
required
rules={[
{ required: true, message: "请输入接口地址" },
{
pattern: /^https?:\/\/.+/,
message: "请输入有效的URL地址,必须以http://或https://开头",
},
]}
>
<Input placeholder="输入接口地址,如:https://api.openai.com" />
</Form.Item>
<Form.Item
name="modelName"
label="模型名称"
required
tooltip="请输入模型名称"
rules={[{ required: true, message: "请输入模型名称" }]}
>
<Input placeholder="输入模型名称" />
</Form.Item>
<Form.Item
name="apiKey"
label="API密钥"
required
rules={[{ required: true, message: "请输入API密钥" }]}
>
<Input
placeholder="输入或生成API密钥"
addonAfter={
<ReloadOutlined
onClick={() => {
form.setFieldsValue({ apiKey: generateApiKey() });
setNewModel({ ...newModel, apiKey: generateApiKey() });
}}
/>
}
/>
</Form.Item>
<Form.Item
name="type"
label="模型类型"
required
rules={[{ required: true, message: "请选择模型类型" }]}
>
<Select options={typeOptions} placeholder="选择模型类型"></Select>
</Form.Item>
<Form.Item
name="isDefault"
label="设为默认"
required
tooltip="当模型类型下仅有一个模型服务时,自动将其设为默认值。"
>
<Switch />
</Form.Item>
</Form>
</Modal>
</>
);
}