EditChannel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
  3. import { useParams } from 'react-router-dom';
  4. import { API, showError, showInfo, showSuccess, verifyJSON } from '../../helpers';
  5. import { CHANNEL_OPTIONS } from '../../constants';
  6. const MODEL_MAPPING_EXAMPLE = {
  7. 'gpt-3.5-turbo-0301': 'gpt-3.5-turbo',
  8. 'gpt-4-0314': 'gpt-4',
  9. 'gpt-4-32k-0314': 'gpt-4-32k'
  10. };
  11. const EditChannel = () => {
  12. const params = useParams();
  13. const channelId = params.id;
  14. const isEdit = channelId !== undefined;
  15. const [loading, setLoading] = useState(isEdit);
  16. const originInputs = {
  17. name: '',
  18. type: 1,
  19. key: '',
  20. base_url: '',
  21. other: '',
  22. model_mapping: '',
  23. models: [],
  24. groups: ['default']
  25. };
  26. const [batch, setBatch] = useState(false);
  27. const [inputs, setInputs] = useState(originInputs);
  28. const [modelOptions, setModelOptions] = useState([]);
  29. const [groupOptions, setGroupOptions] = useState([]);
  30. const [basicModels, setBasicModels] = useState([]);
  31. const [fullModels, setFullModels] = useState([]);
  32. const handleInputChange = (e, { name, value }) => {
  33. setInputs((inputs) => ({ ...inputs, [name]: value }));
  34. };
  35. const loadChannel = async () => {
  36. let res = await API.get(`/api/channel/${channelId}`);
  37. const { success, message, data } = res.data;
  38. if (success) {
  39. if (data.models === '') {
  40. data.models = [];
  41. } else {
  42. data.models = data.models.split(',');
  43. }
  44. if (data.group === '') {
  45. data.groups = [];
  46. } else {
  47. data.groups = data.group.split(',');
  48. }
  49. if (data.model_mapping !== '') {
  50. data.model_mapping = JSON.stringify(JSON.parse(data.model_mapping), null, 2);
  51. }
  52. setInputs(data);
  53. } else {
  54. showError(message);
  55. }
  56. setLoading(false);
  57. };
  58. const fetchModels = async () => {
  59. try {
  60. let res = await API.get(`/api/channel/models`);
  61. setModelOptions(res.data.data.map((model) => ({
  62. key: model.id,
  63. text: model.id,
  64. value: model.id
  65. })));
  66. setFullModels(res.data.data.map((model) => model.id));
  67. setBasicModels(res.data.data.filter((model) => !model.id.startsWith('gpt-4')).map((model) => model.id));
  68. } catch (error) {
  69. showError(error.message);
  70. }
  71. };
  72. const fetchGroups = async () => {
  73. try {
  74. let res = await API.get(`/api/group/`);
  75. setGroupOptions(res.data.data.map((group) => ({
  76. key: group,
  77. text: group,
  78. value: group
  79. })));
  80. } catch (error) {
  81. showError(error.message);
  82. }
  83. };
  84. useEffect(() => {
  85. if (isEdit) {
  86. loadChannel().then();
  87. }
  88. fetchModels().then();
  89. fetchGroups().then();
  90. }, []);
  91. const submit = async () => {
  92. if (!isEdit && (inputs.name === '' || inputs.key === '')) {
  93. showInfo('请填写渠道名称和渠道密钥!');
  94. return;
  95. }
  96. if (inputs.models.length === 0) {
  97. showInfo('请至少选择一个模型!');
  98. return;
  99. }
  100. if (inputs.model_mapping !== '' && !verifyJSON(inputs.model_mapping)) {
  101. showInfo('模型映射必须是合法的 JSON 格式!');
  102. return;
  103. }
  104. let localInputs = inputs;
  105. if (localInputs.base_url.endsWith('/')) {
  106. localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1);
  107. }
  108. if (localInputs.type === 3 && localInputs.other === '') {
  109. localInputs.other = '2023-03-15-preview';
  110. }
  111. let res;
  112. localInputs.models = localInputs.models.join(',');
  113. localInputs.group = localInputs.groups.join(',');
  114. if (isEdit) {
  115. res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
  116. } else {
  117. res = await API.post(`/api/channel/`, localInputs);
  118. }
  119. const { success, message } = res.data;
  120. if (success) {
  121. if (isEdit) {
  122. showSuccess('渠道更新成功!');
  123. } else {
  124. showSuccess('渠道创建成功!');
  125. setInputs(originInputs);
  126. }
  127. } else {
  128. showError(message);
  129. }
  130. };
  131. return (
  132. <>
  133. <Segment loading={loading}>
  134. <Header as='h3'>{isEdit ? '更新渠道信息' : '创建新的渠道'}</Header>
  135. <Form autoComplete='new-password'>
  136. <Form.Field>
  137. <Form.Select
  138. label='类型'
  139. name='type'
  140. required
  141. options={CHANNEL_OPTIONS}
  142. value={inputs.type}
  143. onChange={handleInputChange}
  144. />
  145. </Form.Field>
  146. {
  147. inputs.type === 3 && (
  148. <>
  149. <Message>
  150. 注意,<strong>模型部署名称必须和模型名称保持一致</strong>,因为 One API 会把请求体中的 model
  151. 参数替换为你的部署名称(模型名称中的点会被剔除),<a target='_blank'
  152. href='https://github.com/songquanpeng/one-api/issues/133?notification_referrer_id=NT_kwDOAmJSYrM2NjIwMzI3NDgyOjM5OTk4MDUw#issuecomment-1571602271'>图片演示</a>。
  153. </Message>
  154. <Form.Field>
  155. <Form.Input
  156. label='AZURE_OPENAI_ENDPOINT'
  157. name='base_url'
  158. placeholder={'请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com'}
  159. onChange={handleInputChange}
  160. value={inputs.base_url}
  161. autoComplete='new-password'
  162. />
  163. </Form.Field>
  164. <Form.Field>
  165. <Form.Input
  166. label='默认 API 版本'
  167. name='other'
  168. placeholder={'请输入默认 API 版本,例如:2023-03-15-preview,该配置可以被实际的请求查询参数所覆盖'}
  169. onChange={handleInputChange}
  170. value={inputs.other}
  171. autoComplete='new-password'
  172. />
  173. </Form.Field>
  174. </>
  175. )
  176. }
  177. {
  178. inputs.type === 8 && (
  179. <Form.Field>
  180. <Form.Input
  181. label='Base URL'
  182. name='base_url'
  183. placeholder={'请输入自定义渠道的 Base URL,例如:https://openai.justsong.cn'}
  184. onChange={handleInputChange}
  185. value={inputs.base_url}
  186. autoComplete='new-password'
  187. />
  188. </Form.Field>
  189. )
  190. }
  191. {
  192. inputs.type !== 3 && inputs.type !== 8 && (
  193. <Form.Field>
  194. <Form.Input
  195. label='镜像'
  196. name='base_url'
  197. placeholder={'此项可选,输入镜像站地址,格式为:https://domain.com'}
  198. onChange={handleInputChange}
  199. value={inputs.base_url}
  200. autoComplete='new-password'
  201. />
  202. </Form.Field>
  203. )
  204. }
  205. <Form.Field>
  206. <Form.Input
  207. label='名称'
  208. required
  209. name='name'
  210. placeholder={'请输入名称'}
  211. onChange={handleInputChange}
  212. value={inputs.name}
  213. autoComplete='new-password'
  214. />
  215. </Form.Field>
  216. <Form.Field>
  217. <Form.Dropdown
  218. label='分组'
  219. placeholder={'请选择分组'}
  220. name='groups'
  221. required
  222. fluid
  223. multiple
  224. selection
  225. allowAdditions
  226. additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
  227. onChange={handleInputChange}
  228. value={inputs.groups}
  229. autoComplete='new-password'
  230. options={groupOptions}
  231. />
  232. </Form.Field>
  233. <Form.Field>
  234. <Form.Dropdown
  235. label='模型'
  236. placeholder={'请选择该通道所支持的模型'}
  237. name='models'
  238. required
  239. fluid
  240. multiple
  241. selection
  242. onChange={handleInputChange}
  243. value={inputs.models}
  244. autoComplete='new-password'
  245. options={modelOptions}
  246. />
  247. </Form.Field>
  248. <div style={{ lineHeight: '40px', marginBottom: '12px' }}>
  249. <Button type={'button'} onClick={() => {
  250. handleInputChange(null, { name: 'models', value: basicModels });
  251. }}>填入基础模型</Button>
  252. <Button type={'button'} onClick={() => {
  253. handleInputChange(null, { name: 'models', value: fullModels });
  254. }}>填入所有模型</Button>
  255. <Button type={'button'} onClick={() => {
  256. handleInputChange(null, { name: 'models', value: [] });
  257. }}>清除所有模型</Button>
  258. </div>
  259. <Form.Field>
  260. <Form.TextArea
  261. label='模型映射'
  262. placeholder={`此项可选,为一个 JSON 文本,键为用户请求的模型名称,值为要替换的模型名称,例如:\n${JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2)}`}
  263. name='model_mapping'
  264. onChange={handleInputChange}
  265. value={inputs.model_mapping}
  266. style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
  267. autoComplete='new-password'
  268. />
  269. </Form.Field>
  270. {
  271. batch ? <Form.Field>
  272. <Form.TextArea
  273. label='密钥'
  274. name='key'
  275. required
  276. placeholder={'请输入密钥,一行一个'}
  277. onChange={handleInputChange}
  278. value={inputs.key}
  279. style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
  280. autoComplete='new-password'
  281. />
  282. </Form.Field> : <Form.Field>
  283. <Form.Input
  284. label='密钥'
  285. name='key'
  286. required
  287. placeholder={'请输入密钥'}
  288. onChange={handleInputChange}
  289. value={inputs.key}
  290. autoComplete='new-password'
  291. />
  292. </Form.Field>
  293. }
  294. {
  295. !isEdit && (
  296. <Form.Checkbox
  297. checked={batch}
  298. label='批量创建'
  299. name='batch'
  300. onChange={() => setBatch(!batch)}
  301. />
  302. )
  303. }
  304. <Button positive onClick={submit}>提交</Button>
  305. </Form>
  306. </Segment>
  307. </>
  308. );
  309. };
  310. export default EditChannel;