EditChannel.js 10 KB

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