Skip to content

Commit bf82a7a

Browse files
committed
perf: Add AI model
1 parent 3b4b3d2 commit bf82a7a

6 files changed

Lines changed: 48 additions & 263 deletions

File tree

chat2db-client/src/blocks/Setting/AiSetting/aiTypeConfig.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
import i18n from '@/i18n';
12
import { IAiConfig } from '@/typings';
23
import { AIType } from '@/typings/ai';
34

45
export type IAiConfigBooleans = {
56
[K in keyof IAiConfig]?: boolean | string;
67
};
78

8-
const formConfig: Record<AIType, IAiConfigBooleans> = {
9+
const AITypeName = {
10+
[AIType.CHAT2DBAI]: 'Chat2DB',
11+
[AIType.ZHIPUAI]: i18n('setting.tab.aiType.zhipu'),
12+
[AIType.BAICHUANAI]: i18n('setting.tab.aiType.baichuan'),
13+
[AIType.WENXINAI]: i18n('setting.tab.aiType.wenxin'),
14+
[AIType.TONGYIQIANWENAI]: i18n('setting.tab.aiType.tongyiqianwen'),
15+
[AIType.OPENAI]: 'Open AI',
16+
[AIType.AZUREAI]: 'Azure AI',
17+
[AIType.RESTAI]: i18n('setting.tab.custom'),
18+
};
19+
20+
const AIFormConfig: Record<AIType, IAiConfigBooleans> = {
921
[AIType.CHAT2DBAI]: {
1022
apiKey: true,
1123
},
@@ -20,6 +32,15 @@ const formConfig: Record<AIType, IAiConfigBooleans> = {
2032
apiHost: true,
2133
model: true,
2234
},
35+
[AIType.WENXINAI]: {
36+
apiKey: true,
37+
apiHost: true,
38+
},
39+
[AIType.TONGYIQIANWENAI]: {
40+
apiKey: true,
41+
apiHost: true,
42+
model: true,
43+
},
2344
[AIType.OPENAI]: {
2445
apiKey: true,
2546
apiHost: true,
@@ -39,4 +60,4 @@ const formConfig: Record<AIType, IAiConfigBooleans> = {
3960
},
4061
};
4162

42-
export { formConfig };
63+
export { AIFormConfig, AITypeName };

chat2db-client/src/blocks/Setting/AiSetting/index.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
.aiSqlSourceTitle {
77
margin-right: 20px;
8+
min-width: 50px;
89
}
910

1011
.title {

chat2db-client/src/blocks/Setting/AiSetting/index.tsx

Lines changed: 14 additions & 259 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,21 @@ import configService from '@/service/config';
33
import { AIType } from '@/typings/ai';
44
import { Alert, Button, Form, Input, Radio, RadioChangeEvent, Spin } from 'antd';
55
import i18n from '@/i18n';
6-
import classnames from 'classnames';
76
import { IAiConfig } from '@/typings/setting';
8-
import styles from './index.less';
97
import { getUser } from '@/service/user';
108
import { ILoginUser, IRole } from '@/typings/user';
11-
import { formConfig } from './aiTypeConfig';
9+
import { AIFormConfig, AITypeName } from './aiTypeConfig';
10+
import styles from './index.less';
1211

1312
interface IProps {
1413
handleApplyAiConfig: (aiConfig: IAiConfig) => void;
1514
aiConfig: IAiConfig;
1615
}
17-
type IAiConfigKeys = keyof IAiConfig;
1816

1917
function capitalizeFirstLetter(string) {
2018
return string.charAt(0).toUpperCase() + string.slice(1);
2119
}
2220

23-
const AITypeName = {
24-
[AIType.CHAT2DBAI]: 'Chat2DB',
25-
[AIType.ZHIPUAI]: i18n('setting.tab.aiType.zhipu'),
26-
[AIType.BAICHUANAI]: i18n('setting.tab.aiType.baichuan'),
27-
[AIType.OPENAI]: 'Open AI',
28-
[AIType.AZUREAI]: 'Azure AI',
29-
[AIType.RESTAI]: i18n('setting.tab.custom'),
30-
};
31-
3221
// openAI 的设置项
3322
export default function SettingAI(props: IProps) {
3423
const [aiConfig, setAiConfig] = useState<IAiConfig>();
@@ -56,11 +45,15 @@ export default function SettingAI(props: IProps) {
5645
setAiConfig(props.aiConfig);
5746
}, [props.aiConfig]);
5847

48+
if (loading) {
49+
return <Spin spinning={loading} />;
50+
}
51+
5952
if (!aiConfig) {
6053
return <Alert description={i18n('setting.ai.tips')} type="warning" showIcon />;
6154
}
6255

63-
if (userInfo?.roleCode === IRole.USER) {
56+
if (userInfo?.roleCode && userInfo?.roleCode === IRole.USER) {
6457
// 如果是用户,不能配置ai
6558
return <Alert description={i18n('setting.ai.user.hidden')} type="warning" showIcon />;
6659
}
@@ -96,20 +89,20 @@ export default function SettingAI(props: IProps) {
9689
<div className={styles.aiSqlSourceTitle}>{i18n('setting.title.aiSource')}:</div>
9790
<Radio.Group onChange={handleAiTypeChange} value={aiConfig?.aiSqlSource}>
9891
{Object.keys(AIType).map((key) => (
99-
<Radio key={key} value={key}>
92+
<Radio key={key} value={key} style={{ marginBottom: '8px' }}>
10093
{AITypeName[key]}
10194
</Radio>
10295
))}
10396
</Radio.Group>
10497
</div>
10598

10699
<Form layout="vertical">
107-
{Object.keys(formConfig[aiConfig?.aiSqlSource]).map((key: string) => (
100+
{Object.keys(AIFormConfig[aiConfig?.aiSqlSource]).map((key: string) => (
108101
<Form.Item key={key} label={capitalizeFirstLetter(key)} className={styles.title}>
109102
<Input
110103
autoComplete="off"
111104
value={aiConfig[key]}
112-
defaultValue={formConfig[aiConfig?.aiSqlSource]?.[key]}
105+
defaultValue={AIFormConfig[aiConfig?.aiSqlSource]?.[key]}
113106
onChange={(e) => {
114107
setAiConfig({ ...aiConfig, [key]: e.target.value });
115108
}}
@@ -118,249 +111,11 @@ export default function SettingAI(props: IProps) {
118111
))}
119112
</Form>
120113

121-
{/* {aiConfig?.aiSqlSource === AIType.CHAT2DBAI && (
122-
<div>
123-
<div className={styles.title}>Api Key</div>
124-
<div className={classnames(styles.content, styles.chatGPTKey)}>
125-
<Input
126-
autoComplete="off"
127-
placeholder={i18n('setting.placeholder.chat2dbApiKey')}
128-
value={aiConfig.apiKey}
129-
onChange={(e) => {
130-
setAiConfig({ ...aiConfig, apiKey: e.target.value });
131-
}}
132-
/>
133-
</div>
134-
</div>
135-
)}
136-
{aiConfig?.aiSqlSource === AIType.ZHIPUAI && (
137-
<div>
138-
<div className={styles.title}>Api Key</div>
139-
<div className={classnames(styles.content, styles.chatGPTKey)}>
140-
<Input
141-
autoComplete="off"
142-
// placeholder={i18n('setting.placeholder.chat2dbApiHost')}
143-
value={aiConfig.apiKey}
144-
onChange={(e) => {
145-
setAiConfig({ ...aiConfig, apiKey: e.target.value });
146-
}}
147-
/>
148-
</div>
149-
<div className={styles.title}>Host</div>
150-
<div className={classnames(styles.content, styles.chatGPTKey)}>
151-
<Input
152-
autoComplete="off"
153-
// placeholder={i18n('setting.placeholder.apiHost')}
154-
defaultValue={'https://open.bigmodel.cn/api/paas/v3/model-api/'}
155-
value={aiConfig.apiHost}
156-
onChange={(e) => {
157-
setAiConfig({ ...aiConfig, apiHost: e.target.value });
158-
}}
159-
/>
160-
</div>
161-
<div className={styles.title}>Model</div>
162-
<div className={classnames(styles.content, styles.chatGPTKey)}>
163-
<Input
164-
autoComplete="off"
165-
// placeholder={i18n('setting.placeholder.azureEndpoint')}
166-
defaultValue={'chatglm_turbo'}
167-
value={aiConfig.model}
168-
onChange={(e) => {
169-
setAiConfig({ ...aiConfig, model: e.target.value });
170-
}}
171-
/>
172-
</div>
173-
</div>
174-
)}
175-
{aiConfig?.aiSqlSource === AIType.BAICHUANAI && (
176-
<div>
177-
<div className={styles.title}>Api Key</div>
178-
<div className={classnames(styles.content, styles.chatGPTKey)}>
179-
<Input
180-
autoComplete="off"
181-
// placeholder={i18n('setting.placeholder.chat2dbApiHost')}
182-
value={aiConfig.apiKey}
183-
onChange={(e) => {
184-
setAiConfig({ ...aiConfig, apiKey: e.target.value });
185-
}}
186-
/>
187-
</div>
188-
<div className={styles.title}>SecretKey Key</div>
189-
<div className={classnames(styles.content, styles.chatGPTKey)}>
190-
<Input
191-
autoComplete="off"
192-
// placeholder={i18n('setting.placeholder.chat2dbApiHost')}
193-
value={aiConfig.secretKey}
194-
onChange={(e) => {
195-
setAiConfig({ ...aiConfig, secretKey: e.target.value });
196-
}}
197-
/>
198-
</div>
199-
<div className={styles.title}>Host</div>
200-
<div className={classnames(styles.content, styles.chatGPTKey)}>
201-
<Input
202-
autoComplete="off"
203-
// placeholder={i18n('setting.placeholder.apiHost')}
204-
defaultValue={'https://open.bigmodel.cn/api/paas/v3/model-api/'}
205-
value={aiConfig.apiHost}
206-
onChange={(e) => {
207-
setAiConfig({ ...aiConfig, apiHost: e.target.value });
208-
}}
209-
/>
210-
</div>
211-
<div className={styles.title}>Model</div>
212-
<div className={classnames(styles.content, styles.chatGPTKey)}>
213-
<Input
214-
autoComplete="off"
215-
// placeholder={i18n('setting.placeholder.azureEndpoint')}
216-
defaultValue={'chatglm_turbo'}
217-
value={aiConfig.model}
218-
onChange={(e) => {
219-
setAiConfig({ ...aiConfig, model: e.target.value });
220-
}}
221-
/>
222-
</div>
223-
</div>
224-
)}
225-
{aiConfig?.aiSqlSource === AIType.OPENAI && (
226-
<div>
227-
<div className={styles.title}>Api Key</div>
228-
<div className={classnames(styles.content, styles.chatGPTKey)}>
229-
<Input
230-
autoComplete="off"
231-
placeholder={i18n('setting.placeholder.apiKey')}
232-
value={aiConfig.apiKey}
233-
onChange={(e) => {
234-
setAiConfig({ ...aiConfig, apiKey: e.target.value });
235-
}}
236-
/>
237-
</div>
238-
<div className={styles.title}>Api Host</div>
239-
<div className={classnames(styles.content, styles.chatGPTKey)}>
240-
<Input
241-
autoComplete="off"
242-
placeholder={i18n('setting.placeholder.apiHost')}
243-
value={aiConfig.apiHost}
244-
onChange={(e) => {
245-
setAiConfig({ ...aiConfig, apiHost: e.target.value });
246-
}}
247-
/>
248-
</div>
249-
<div className={styles.title}>HTTP Proxy Host</div>
250-
<div className={classnames(styles.content, styles.chatGPTKey)}>
251-
<Input
252-
autoComplete="off"
253-
placeholder={i18n('setting.placeholder.httpsProxy', 'host')}
254-
value={aiConfig.httpProxyHost}
255-
onChange={(e) => {
256-
setAiConfig({
257-
...aiConfig,
258-
httpProxyHost: e.target.value,
259-
});
260-
}}
261-
/>
262-
</div>
263-
<div className={styles.title}>HTTP Proxy Port</div>
264-
<div className={classnames(styles.content, styles.chatGPTKey)}>
265-
<Input
266-
autoComplete="off"
267-
placeholder={i18n('setting.placeholder.httpsProxy', 'port')}
268-
value={aiConfig.httpProxyPort}
269-
onChange={(e) => {
270-
setAiConfig({
271-
...aiConfig,
272-
httpProxyPort: e.target.value,
273-
});
274-
}}
275-
/>
276-
</div>
277-
</div>
278-
)}
279-
{aiConfig?.aiSqlSource === AIType.AZUREAI && (
280-
<div>
281-
<div className={styles.title}>Api Key</div>
282-
<div className={classnames(styles.content, styles.chatGPTKey)}>
283-
<Input
284-
autoComplete="off"
285-
placeholder={i18n('setting.placeholder.azureOpenAIKey')}
286-
value={aiConfig.apiKey}
287-
onChange={(e) => {
288-
setAiConfig({ ...aiConfig, apiKey: e.target.value });
289-
}}
290-
/>
291-
</div>
292-
<div className={styles.title}>Endpoint</div>
293-
<div className={classnames(styles.content, styles.chatGPTKey)}>
294-
<Input
295-
autoComplete="off"
296-
placeholder={i18n('setting.placeholder.azureEndpoint')}
297-
value={aiConfig.apiHost}
298-
onChange={(e) => {
299-
setAiConfig({ ...aiConfig, apiHost: e.target.value });
300-
}}
301-
/>
302-
</div>
303-
<div className={styles.title}>DeploymentId</div>
304-
<div className={classnames(styles.content, styles.chatGPTKey)}>
305-
<Input
306-
autoComplete="off"
307-
placeholder={i18n('setting.placeholder.azureDeployment')}
308-
value={aiConfig.model}
309-
onChange={(e) => {
310-
setAiConfig({
311-
...aiConfig,
312-
model: e.target.value,
313-
});
314-
}}
315-
/>
316-
</div>
317-
</div>
114+
{aiConfig.aiSqlSource === AIType.RESTAI && (
115+
<div style={{ margin: '32px 0 ', fontSize: '12px', opacity: '0.5' }}>{`Tips: ${i18n(
116+
'setting.tab.aiType.custom.tips',
117+
)}`}</div>
318118
)}
319-
{aiConfig?.aiSqlSource === AIType.RESTAI && (
320-
<div>
321-
<div className={styles.title}>{i18n('setting.label.customAiUrl')}</div>
322-
<div className={classnames(styles.content, styles.chatGPTKey)}>
323-
<Input
324-
autoComplete="off"
325-
placeholder={i18n('setting.placeholder.customUrl')}
326-
value={aiConfig.apiHost}
327-
onChange={(e) => {
328-
setAiConfig({
329-
...aiConfig,
330-
apiHost: e.target.value,
331-
});
332-
}}
333-
/>
334-
</div>
335-
<div className={styles.title}>Api Key</div>
336-
<div className={classnames(styles.content, styles.chatGPTKey)}>
337-
<Input
338-
autoComplete="off"
339-
// placeholder={i18n('setting.placeholder.apiKey')}
340-
value={aiConfig.apiKey}
341-
onChange={(e) => {
342-
setAiConfig({ ...aiConfig, apiKey: e.target.value });
343-
}}
344-
/>
345-
</div>
346-
<div className={styles.title}>Model</div>
347-
<div className={classnames(styles.content, styles.chatGPTKey)}>
348-
<Input
349-
autoComplete="off"
350-
// placeholder={i18n('setting.placeholder.azureDeployment')}
351-
value={aiConfig.model}
352-
onChange={(e) => {
353-
setAiConfig({
354-
...aiConfig,
355-
model: e.target.value,
356-
});
357-
}}
358-
/>
359-
</div>
360-
361-
<div style={{ marginTop: '32px', fontSize: '12px', opacity: '0.5' }}>{}</div>
362-
</div>
363-
)} */}
364119
<div className={styles.bottomButton}>
365120
<Button type="primary" onClick={handleApplyAiConfig}>
366121
{i18n('setting.button.apply')}

chat2db-client/src/i18n/en-us/setting.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export default {
1818
'setting.tab.custom': 'Custom',
1919
'setting.tab.aiType.zhipu': 'ZhiPu AI',
2020
'setting.tab.aiType.baichuan': 'BaiChuan AI',
21+
'setting.tab.aiType.wenxin': 'WenXin AI',
22+
'setting.tab.aiType.tongyiqianwen': 'TongYiQianWen AI',
23+
'setting.tab.aiType.custom.tips': "The return remains consistent with OpenAI",
2124
'setting.label.serviceAddress': 'Service Address',
2225
'setting.button.apply': 'Apply',
2326
'setting.text.currentEnv': 'Current Env',

0 commit comments

Comments
 (0)