2023-04-23 04:43:10 +00:00
|
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-05-13 03:36:36 +00:00
|
|
|
|
import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
|
2023-04-23 04:43:10 +00:00
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import { API, showError, showSuccess } from '../../helpers';
|
2023-04-23 07:42:23 +00:00
|
|
|
|
import { CHANNEL_OPTIONS } from '../../constants';
|
2023-04-23 04:43:10 +00:00
|
|
|
|
|
|
|
|
|
const EditChannel = () => {
|
|
|
|
|
const params = useParams();
|
2023-04-23 07:42:23 +00:00
|
|
|
|
const channelId = params.id;
|
2023-05-13 03:36:36 +00:00
|
|
|
|
const isEdit = channelId !== undefined;
|
|
|
|
|
const [loading, setLoading] = useState(isEdit);
|
|
|
|
|
const originInputs = {
|
2023-04-23 07:42:23 +00:00
|
|
|
|
name: '',
|
|
|
|
|
type: 1,
|
2023-05-13 03:36:36 +00:00
|
|
|
|
key: '',
|
2023-05-13 04:53:57 +00:00
|
|
|
|
base_url: '',
|
|
|
|
|
other: ''
|
2023-05-13 03:36:36 +00:00
|
|
|
|
};
|
2023-05-13 09:08:13 +00:00
|
|
|
|
const [batch, setBatch] = useState(false);
|
2023-05-13 03:36:36 +00:00
|
|
|
|
const [inputs, setInputs] = useState(originInputs);
|
2023-04-23 04:43:10 +00:00
|
|
|
|
const handleInputChange = (e, { name, value }) => {
|
2023-05-13 09:08:13 +00:00
|
|
|
|
console.log(name, value);
|
2023-04-23 04:43:10 +00:00
|
|
|
|
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-23 07:42:23 +00:00
|
|
|
|
const loadChannel = async () => {
|
|
|
|
|
let res = await API.get(`/api/channel/${channelId}`);
|
2023-04-23 04:43:10 +00:00
|
|
|
|
const { success, message, data } = res.data;
|
|
|
|
|
if (success) {
|
|
|
|
|
data.password = '';
|
|
|
|
|
setInputs(data);
|
|
|
|
|
} else {
|
|
|
|
|
showError(message);
|
|
|
|
|
}
|
|
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
useEffect(() => {
|
2023-05-13 03:36:36 +00:00
|
|
|
|
if (isEdit) {
|
|
|
|
|
loadChannel().then();
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
2023-05-13 03:36:36 +00:00
|
|
|
|
if (!isEdit && (inputs.name === '' || inputs.key === '')) return;
|
|
|
|
|
let localInputs = inputs;
|
|
|
|
|
if (localInputs.base_url.endsWith('/')) {
|
|
|
|
|
localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1);
|
|
|
|
|
}
|
2023-05-19 14:13:29 +00:00
|
|
|
|
if (localInputs.type === 3 && localInputs.other === '') {
|
|
|
|
|
localInputs.other = '2023-03-15-preview';
|
|
|
|
|
}
|
2023-05-13 03:36:36 +00:00
|
|
|
|
let res;
|
|
|
|
|
if (isEdit) {
|
|
|
|
|
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
|
|
|
|
|
} else {
|
|
|
|
|
res = await API.post(`/api/channel/`, localInputs);
|
2023-04-23 12:35:49 +00:00
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
const { success, message } = res.data;
|
|
|
|
|
if (success) {
|
2023-05-13 03:36:36 +00:00
|
|
|
|
if (isEdit) {
|
|
|
|
|
showSuccess('渠道更新成功!');
|
|
|
|
|
} else {
|
|
|
|
|
showSuccess('渠道创建成功!');
|
|
|
|
|
setInputs(originInputs);
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
showError(message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Segment loading={loading}>
|
2023-05-13 03:36:36 +00:00
|
|
|
|
<Header as='h3'>{isEdit ? '更新渠道信息' : '创建新的渠道'}</Header>
|
2023-05-12 03:44:38 +00:00
|
|
|
|
<Form autoComplete='new-password'>
|
2023-04-23 04:43:10 +00:00
|
|
|
|
<Form.Field>
|
2023-04-23 07:42:23 +00:00
|
|
|
|
<Form.Select
|
|
|
|
|
label='类型'
|
|
|
|
|
name='type'
|
|
|
|
|
options={CHANNEL_OPTIONS}
|
|
|
|
|
value={inputs.type}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
2023-05-13 03:36:36 +00:00
|
|
|
|
{
|
|
|
|
|
inputs.type === 3 && (
|
|
|
|
|
<>
|
|
|
|
|
<Message>
|
2023-05-13 04:29:17 +00:00
|
|
|
|
注意,<strong>模型部署名称必须和模型名称保持一致</strong>,因为 One API 会把请求体中的 model 参数替换为你的部署名称(模型名称中的点会被剔除)。
|
2023-05-13 03:36:36 +00:00
|
|
|
|
</Message>
|
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
|
|
|
|
label='AZURE_OPENAI_ENDPOINT'
|
|
|
|
|
name='base_url'
|
|
|
|
|
placeholder={'请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com'}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.base_url}
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
2023-05-13 04:53:57 +00:00
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
|
|
|
|
label='默认 API 版本'
|
|
|
|
|
name='other'
|
|
|
|
|
placeholder={'请输入默认 API 版本,例如:2023-03-15-preview,该配置可以被实际的请求查询参数所覆盖'}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.other}
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
2023-05-13 03:36:36 +00:00
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-04-23 12:35:49 +00:00
|
|
|
|
{
|
|
|
|
|
inputs.type === 8 && (
|
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
|
|
|
|
label='Base URL'
|
|
|
|
|
name='base_url'
|
2023-05-13 03:36:36 +00:00
|
|
|
|
placeholder={'请输入自定义渠道的 Base URL,例如:https://openai.justsong.cn'}
|
2023-04-23 12:35:49 +00:00
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.base_url}
|
2023-05-12 03:44:38 +00:00
|
|
|
|
autoComplete='new-password'
|
2023-04-23 12:35:49 +00:00
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
2023-04-23 07:42:23 +00:00
|
|
|
|
label='名称'
|
|
|
|
|
name='name'
|
2023-05-13 03:36:36 +00:00
|
|
|
|
placeholder={'请输入名称'}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
onChange={handleInputChange}
|
2023-04-23 07:42:23 +00:00
|
|
|
|
value={inputs.name}
|
2023-05-12 03:44:38 +00:00
|
|
|
|
autoComplete='new-password'
|
2023-04-23 04:43:10 +00:00
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
2023-05-13 09:08:13 +00:00
|
|
|
|
{
|
|
|
|
|
batch ? <Form.Field>
|
|
|
|
|
<Form.TextArea
|
|
|
|
|
label='密钥'
|
|
|
|
|
name='key'
|
|
|
|
|
placeholder={'请输入密钥,一行一个'}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.key}
|
|
|
|
|
style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field> : <Form.Field>
|
|
|
|
|
<Form.Input
|
|
|
|
|
label='密钥'
|
|
|
|
|
name='key'
|
|
|
|
|
placeholder={'请输入密钥'}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.key}
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
!isEdit && (
|
|
|
|
|
<Form.Checkbox
|
|
|
|
|
checked={batch}
|
|
|
|
|
label='批量创建'
|
|
|
|
|
name='batch'
|
|
|
|
|
onChange={() => setBatch(!batch)}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
<Button onClick={submit}>提交</Button>
|
|
|
|
|
</Form>
|
|
|
|
|
</Segment>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default EditChannel;
|