EditChannel.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. import React, { useEffect, useState } from 'react';
  2. import { useNavigate } from 'react-router-dom';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. API,
  6. isMobile,
  7. showError,
  8. showInfo,
  9. showSuccess,
  10. verifyJSON,
  11. } from '../../helpers';
  12. import { CHANNEL_OPTIONS } from '../../constants';
  13. import {
  14. SideSheet,
  15. Space,
  16. Spin,
  17. Button,
  18. Input,
  19. Typography,
  20. Select,
  21. TextArea,
  22. Checkbox,
  23. Banner,
  24. Modal,
  25. ImagePreview,
  26. Card,
  27. Tag,
  28. } from '@douyinfe/semi-ui';
  29. import { getChannelModels } from '../../helpers';
  30. import {
  31. IconSave,
  32. IconClose,
  33. IconServer,
  34. IconSetting,
  35. IconCode,
  36. IconGlobe,
  37. } from '@douyinfe/semi-icons';
  38. const { Text, Title } = Typography;
  39. const MODEL_MAPPING_EXAMPLE = {
  40. 'gpt-3.5-turbo': 'gpt-3.5-turbo-0125',
  41. };
  42. const STATUS_CODE_MAPPING_EXAMPLE = {
  43. 400: '500',
  44. };
  45. const REGION_EXAMPLE = {
  46. default: 'us-central1',
  47. 'claude-3-5-sonnet-20240620': 'europe-west1',
  48. };
  49. function type2secretPrompt(type) {
  50. // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
  51. switch (type) {
  52. case 15:
  53. return '按照如下格式输入:APIKey|SecretKey';
  54. case 18:
  55. return '按照如下格式输入:APPID|APISecret|APIKey';
  56. case 22:
  57. return '按照如下格式输入:APIKey-AppId,例如:fastgpt-0sp2gtvfdgyi4k30jwlgwf1i-64f335d84283f05518e9e041';
  58. case 23:
  59. return '按照如下格式输入:AppId|SecretId|SecretKey';
  60. case 33:
  61. return '按照如下格式输入:Ak|Sk|Region';
  62. default:
  63. return '请输入渠道对应的鉴权密钥';
  64. }
  65. }
  66. const EditChannel = (props) => {
  67. const { t } = useTranslation();
  68. const navigate = useNavigate();
  69. const channelId = props.editingChannel.id;
  70. const isEdit = channelId !== undefined;
  71. const [loading, setLoading] = useState(isEdit);
  72. const handleCancel = () => {
  73. props.handleClose();
  74. };
  75. const originInputs = {
  76. name: '',
  77. type: 1,
  78. key: '',
  79. openai_organization: '',
  80. max_input_tokens: 0,
  81. base_url: '',
  82. other: '',
  83. model_mapping: '',
  84. status_code_mapping: '',
  85. models: [],
  86. auto_ban: 1,
  87. test_model: '',
  88. groups: ['default'],
  89. priority: 0,
  90. weight: 0,
  91. tag: '',
  92. };
  93. const [batch, setBatch] = useState(false);
  94. const [autoBan, setAutoBan] = useState(true);
  95. // const [autoBan, setAutoBan] = useState(true);
  96. const [inputs, setInputs] = useState(originInputs);
  97. const [originModelOptions, setOriginModelOptions] = useState([]);
  98. const [modelOptions, setModelOptions] = useState([]);
  99. const [groupOptions, setGroupOptions] = useState([]);
  100. const [basicModels, setBasicModels] = useState([]);
  101. const [fullModels, setFullModels] = useState([]);
  102. const [customModel, setCustomModel] = useState('');
  103. const [modalImageUrl, setModalImageUrl] = useState('');
  104. const [isModalOpenurl, setIsModalOpenurl] = useState(false);
  105. const handleInputChange = (name, value) => {
  106. if (name === 'base_url' && value.endsWith('/v1')) {
  107. Modal.confirm({
  108. title: '警告',
  109. content:
  110. '不需要在末尾加/v1,New API会自动处理,添加后可能导致请求失败,是否继续?',
  111. onOk: () => {
  112. setInputs((inputs) => ({ ...inputs, [name]: value }));
  113. },
  114. });
  115. return;
  116. }
  117. setInputs((inputs) => ({ ...inputs, [name]: value }));
  118. if (name === 'type') {
  119. let localModels = [];
  120. switch (value) {
  121. case 2:
  122. localModels = [
  123. 'mj_imagine',
  124. 'mj_variation',
  125. 'mj_reroll',
  126. 'mj_blend',
  127. 'mj_upscale',
  128. 'mj_describe',
  129. 'mj_uploads',
  130. ];
  131. break;
  132. case 5:
  133. localModels = [
  134. 'swap_face',
  135. 'mj_imagine',
  136. 'mj_variation',
  137. 'mj_reroll',
  138. 'mj_blend',
  139. 'mj_upscale',
  140. 'mj_describe',
  141. 'mj_zoom',
  142. 'mj_shorten',
  143. 'mj_modal',
  144. 'mj_inpaint',
  145. 'mj_custom_zoom',
  146. 'mj_high_variation',
  147. 'mj_low_variation',
  148. 'mj_pan',
  149. 'mj_uploads',
  150. ];
  151. break;
  152. case 36:
  153. localModels = ['suno_music', 'suno_lyrics'];
  154. break;
  155. default:
  156. localModels = getChannelModels(value);
  157. break;
  158. }
  159. if (inputs.models.length === 0) {
  160. setInputs((inputs) => ({ ...inputs, models: localModels }));
  161. }
  162. setBasicModels(localModels);
  163. }
  164. //setAutoBan
  165. };
  166. const loadChannel = async () => {
  167. setLoading(true);
  168. let res = await API.get(`/api/channel/${channelId}`);
  169. if (res === undefined) {
  170. return;
  171. }
  172. const { success, message, data } = res.data;
  173. if (success) {
  174. if (data.models === '') {
  175. data.models = [];
  176. } else {
  177. data.models = data.models.split(',');
  178. }
  179. if (data.group === '') {
  180. data.groups = [];
  181. } else {
  182. data.groups = data.group.split(',');
  183. }
  184. if (data.model_mapping !== '') {
  185. data.model_mapping = JSON.stringify(
  186. JSON.parse(data.model_mapping),
  187. null,
  188. 2,
  189. );
  190. }
  191. setInputs(data);
  192. if (data.auto_ban === 0) {
  193. setAutoBan(false);
  194. } else {
  195. setAutoBan(true);
  196. }
  197. setBasicModels(getChannelModels(data.type));
  198. // console.log(data);
  199. } else {
  200. showError(message);
  201. }
  202. setLoading(false);
  203. };
  204. const fetchUpstreamModelList = async (name) => {
  205. // if (inputs['type'] !== 1) {
  206. // showError(t('仅支持 OpenAI 接口格式'));
  207. // return;
  208. // }
  209. setLoading(true);
  210. const models = inputs['models'] || [];
  211. let err = false;
  212. if (isEdit) {
  213. // 如果是编辑模式,使用已有的channel id获取模型列表
  214. const res = await API.get('/api/channel/fetch_models/' + channelId);
  215. if (res.data && res.data?.success) {
  216. models.push(...res.data.data);
  217. } else {
  218. err = true;
  219. }
  220. } else {
  221. // 如果是新建模式,通过后端代理获取模型列表
  222. if (!inputs?.['key']) {
  223. showError(t('请填写密钥'));
  224. err = true;
  225. } else {
  226. try {
  227. const res = await API.post('/api/channel/fetch_models', {
  228. base_url: inputs['base_url'],
  229. type: inputs['type'],
  230. key: inputs['key'],
  231. });
  232. if (res.data && res.data.success) {
  233. models.push(...res.data.data);
  234. } else {
  235. err = true;
  236. }
  237. } catch (error) {
  238. console.error('Error fetching models:', error);
  239. err = true;
  240. }
  241. }
  242. }
  243. if (!err) {
  244. handleInputChange(name, Array.from(new Set(models)));
  245. showSuccess(t('获取模型列表成功'));
  246. } else {
  247. showError(t('获取模型列表失败'));
  248. }
  249. setLoading(false);
  250. };
  251. const fetchModels = async () => {
  252. try {
  253. let res = await API.get(`/api/channel/models`);
  254. let localModelOptions = res.data.data.map((model) => ({
  255. label: model.id,
  256. value: model.id,
  257. }));
  258. setOriginModelOptions(localModelOptions);
  259. setFullModels(res.data.data.map((model) => model.id));
  260. setBasicModels(
  261. res.data.data
  262. .filter((model) => {
  263. return model.id.startsWith('gpt-') || model.id.startsWith('text-');
  264. })
  265. .map((model) => model.id),
  266. );
  267. } catch (error) {
  268. showError(error.message);
  269. }
  270. };
  271. const fetchGroups = async () => {
  272. try {
  273. let res = await API.get(`/api/group/`);
  274. if (res === undefined) {
  275. return;
  276. }
  277. setGroupOptions(
  278. res.data.data.map((group) => ({
  279. label: group,
  280. value: group,
  281. })),
  282. );
  283. } catch (error) {
  284. showError(error.message);
  285. }
  286. };
  287. useEffect(() => {
  288. let localModelOptions = [...originModelOptions];
  289. inputs.models.forEach((model) => {
  290. if (!localModelOptions.find((option) => option.label === model)) {
  291. localModelOptions.push({
  292. label: model,
  293. value: model,
  294. });
  295. }
  296. });
  297. setModelOptions(localModelOptions);
  298. }, [originModelOptions, inputs.models]);
  299. useEffect(() => {
  300. fetchModels().then();
  301. fetchGroups().then();
  302. if (isEdit) {
  303. loadChannel().then(() => { });
  304. } else {
  305. setInputs(originInputs);
  306. let localModels = getChannelModels(inputs.type);
  307. setBasicModels(localModels);
  308. setInputs((inputs) => ({ ...inputs, models: localModels }));
  309. }
  310. }, [props.editingChannel.id]);
  311. const submit = async () => {
  312. if (!isEdit && (inputs.name === '' || inputs.key === '')) {
  313. showInfo(t('请填写渠道名称和渠道密钥!'));
  314. return;
  315. }
  316. if (inputs.models.length === 0) {
  317. showInfo(t('请至少选择一个模型!'));
  318. return;
  319. }
  320. if (inputs.model_mapping !== '' && !verifyJSON(inputs.model_mapping)) {
  321. showInfo(t('模型映射必须是合法的 JSON 格式!'));
  322. return;
  323. }
  324. let localInputs = { ...inputs };
  325. if (localInputs.base_url && localInputs.base_url.endsWith('/')) {
  326. localInputs.base_url = localInputs.base_url.slice(
  327. 0,
  328. localInputs.base_url.length - 1,
  329. );
  330. }
  331. if (localInputs.type === 18 && localInputs.other === '') {
  332. localInputs.other = 'v2.1';
  333. }
  334. let res;
  335. if (!Array.isArray(localInputs.models)) {
  336. showError(t('提交失败,请勿重复提交!'));
  337. handleCancel();
  338. return;
  339. }
  340. localInputs.auto_ban = autoBan ? 1 : 0;
  341. localInputs.models = localInputs.models.join(',');
  342. localInputs.group = localInputs.groups.join(',');
  343. if (isEdit) {
  344. res = await API.put(`/api/channel/`, {
  345. ...localInputs,
  346. id: parseInt(channelId),
  347. });
  348. } else {
  349. res = await API.post(`/api/channel/`, localInputs);
  350. }
  351. const { success, message } = res.data;
  352. if (success) {
  353. if (isEdit) {
  354. showSuccess(t('渠道更新成功!'));
  355. } else {
  356. showSuccess(t('渠道创建成功!'));
  357. setInputs(originInputs);
  358. }
  359. props.refresh();
  360. props.handleClose();
  361. } else {
  362. showError(message);
  363. }
  364. };
  365. const addCustomModels = () => {
  366. if (customModel.trim() === '') return;
  367. const modelArray = customModel.split(',').map((model) => model.trim());
  368. let localModels = [...inputs.models];
  369. let localModelOptions = [...modelOptions];
  370. let hasError = false;
  371. modelArray.forEach((model) => {
  372. if (model && !localModels.includes(model)) {
  373. localModels.push(model);
  374. localModelOptions.push({
  375. key: model,
  376. text: model,
  377. value: model,
  378. });
  379. } else if (model) {
  380. showError(t('某些模型已存在!'));
  381. hasError = true;
  382. }
  383. });
  384. if (hasError) return;
  385. setModelOptions(localModelOptions);
  386. setCustomModel('');
  387. handleInputChange('models', localModels);
  388. };
  389. return (
  390. <>
  391. <SideSheet
  392. placement={isEdit ? 'right' : 'left'}
  393. title={
  394. <Space>
  395. <Tag color="blue" shape="circle">{isEdit ? t('编辑') : t('新建')}</Tag>
  396. <Title heading={4} className="m-0">
  397. {isEdit ? t('更新渠道信息') : t('创建新的渠道')}
  398. </Title>
  399. </Space>
  400. }
  401. headerStyle={{
  402. borderBottom: '1px solid var(--semi-color-border)',
  403. padding: '24px'
  404. }}
  405. bodyStyle={{
  406. backgroundColor: 'var(--semi-color-bg-0)',
  407. padding: '0'
  408. }}
  409. visible={props.visible}
  410. width={isMobile() ? '100%' : 600}
  411. footer={
  412. <div className="flex justify-end bg-white">
  413. <Space>
  414. <Button
  415. theme="solid"
  416. size="large"
  417. className="!rounded-full"
  418. onClick={submit}
  419. icon={<IconSave />}
  420. >
  421. {t('提交')}
  422. </Button>
  423. <Button
  424. theme="light"
  425. size="large"
  426. className="!rounded-full"
  427. type="primary"
  428. onClick={handleCancel}
  429. icon={<IconClose />}
  430. >
  431. {t('取消')}
  432. </Button>
  433. </Space>
  434. </div>
  435. }
  436. closeIcon={null}
  437. onCancel={() => handleCancel()}
  438. >
  439. <Spin spinning={loading}>
  440. <div className="p-6">
  441. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  442. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  443. background: 'linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%)',
  444. position: 'relative'
  445. }}>
  446. <div className="absolute inset-0 overflow-hidden">
  447. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  448. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  449. </div>
  450. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  451. <IconServer size="large" style={{ color: '#ffffff' }} />
  452. </div>
  453. <div className="relative">
  454. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('基本信息')}</Text>
  455. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('渠道的基本配置信息')}</div>
  456. </div>
  457. </div>
  458. <div className="space-y-4">
  459. <div>
  460. <Text strong className="block mb-2">{t('类型')}</Text>
  461. <Select
  462. name='type'
  463. required
  464. optionList={CHANNEL_OPTIONS}
  465. value={inputs.type}
  466. onChange={(value) => handleInputChange('type', value)}
  467. style={{ width: '100%' }}
  468. filter
  469. searchPosition='dropdown'
  470. placeholder={t('请选择渠道类型')}
  471. size="large"
  472. className="!rounded-lg"
  473. />
  474. </div>
  475. <div>
  476. <Text strong className="block mb-2">{t('名称')}</Text>
  477. <Input
  478. required
  479. name='name'
  480. placeholder={t('请为渠道命名')}
  481. onChange={(value) => {
  482. handleInputChange('name', value);
  483. }}
  484. value={inputs.name}
  485. autoComplete='new-password'
  486. size="large"
  487. className="!rounded-lg"
  488. />
  489. </div>
  490. <div>
  491. <Text strong className="block mb-2">{t('密钥')}</Text>
  492. {batch ? (
  493. <TextArea
  494. name='key'
  495. required
  496. placeholder={t('请输入密钥,一行一个')}
  497. onChange={(value) => {
  498. handleInputChange('key', value);
  499. }}
  500. value={inputs.key}
  501. style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
  502. autoComplete='new-password'
  503. className="!rounded-lg"
  504. />
  505. ) : (
  506. <>
  507. {inputs.type === 41 ? (
  508. <TextArea
  509. name='key'
  510. required
  511. placeholder={
  512. '{\n' +
  513. ' "type": "service_account",\n' +
  514. ' "project_id": "abc-bcd-123-456",\n' +
  515. ' "private_key_id": "123xxxxx456",\n' +
  516. ' "private_key": "-----BEGIN PRIVATE KEY-----xxxx\n' +
  517. ' "client_email": "xxx@developer.gserviceaccount.com",\n' +
  518. ' "client_id": "111222333",\n' +
  519. ' "auth_uri": "https://accounts.google.com/o/oauth2/auth",\n' +
  520. ' "token_uri": "https://oauth2.googleapis.com/token",\n' +
  521. ' "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",\n' +
  522. ' "client_x509_cert_url": "https://xxxxx.gserviceaccount.com",\n' +
  523. ' "universe_domain": "googleapis.com"\n' +
  524. '}'
  525. }
  526. onChange={(value) => {
  527. handleInputChange('key', value);
  528. }}
  529. autosize={{ minRows: 10 }}
  530. value={inputs.key}
  531. autoComplete='new-password'
  532. className="!rounded-lg font-mono"
  533. />
  534. ) : (
  535. <Input
  536. name='key'
  537. required
  538. placeholder={t(type2secretPrompt(inputs.type))}
  539. onChange={(value) => {
  540. handleInputChange('key', value);
  541. }}
  542. value={inputs.key}
  543. autoComplete='new-password'
  544. size="large"
  545. className="!rounded-lg"
  546. />
  547. )}
  548. </>
  549. )}
  550. </div>
  551. {!isEdit && (
  552. <div className="flex items-center">
  553. <Checkbox
  554. checked={batch}
  555. onChange={() => setBatch(!batch)}
  556. />
  557. <Text strong className="ml-2">{t('批量创建')}</Text>
  558. </div>
  559. )}
  560. </div>
  561. </Card>
  562. {/* API Configuration Card */}
  563. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  564. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  565. background: 'linear-gradient(135deg, #065f46 0%, #059669 50%, #10b981 100%)',
  566. position: 'relative'
  567. }}>
  568. <div className="absolute inset-0 overflow-hidden">
  569. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  570. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  571. </div>
  572. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  573. <IconGlobe size="large" style={{ color: '#ffffff' }} />
  574. </div>
  575. <div className="relative">
  576. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('API 配置')}</Text>
  577. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('API 地址和相关配置')}</div>
  578. </div>
  579. </div>
  580. <div className="space-y-4">
  581. {inputs.type === 40 && (
  582. <Banner
  583. type='info'
  584. description={
  585. <div>
  586. <Text strong>{t('邀请链接')}:</Text>
  587. <Text
  588. link
  589. underline
  590. className="ml-2 cursor-pointer"
  591. onClick={() => window.open('https://cloud.siliconflow.cn/i/hij0YNTZ')}
  592. >
  593. https://cloud.siliconflow.cn/i/hij0YNTZ
  594. </Text>
  595. </div>
  596. }
  597. className='!rounded-lg'
  598. />
  599. )}
  600. {inputs.type === 3 && (
  601. <>
  602. <Banner
  603. type='warning'
  604. description={t('2025年5月10日后添加的渠道,不需要再在部署的时候移除模型名称中的"."')}
  605. className='!rounded-lg'
  606. />
  607. <div>
  608. <Text strong className="block mb-2">AZURE_OPENAI_ENDPOINT</Text>
  609. <Input
  610. name='azure_base_url'
  611. placeholder={t('请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com')}
  612. onChange={(value) => handleInputChange('base_url', value)}
  613. value={inputs.base_url}
  614. autoComplete='new-password'
  615. size="large"
  616. className="!rounded-lg"
  617. />
  618. </div>
  619. <div>
  620. <Text strong className="block mb-2">{t('默认 API 版本')}</Text>
  621. <Input
  622. name='azure_other'
  623. placeholder={t('请输入默认 API 版本,例如:2025-04-01-preview')}
  624. onChange={(value) => handleInputChange('other', value)}
  625. value={inputs.other}
  626. autoComplete='new-password'
  627. size="large"
  628. className="!rounded-lg"
  629. />
  630. </div>
  631. </>
  632. )}
  633. {inputs.type === 8 && (
  634. <>
  635. <Banner
  636. type='warning'
  637. description={t('如果你对接的是上游One API或者New API等转发项目,请使用OpenAI类型,不要使用此类型,除非你知道你在做什么。')}
  638. className='!rounded-lg'
  639. />
  640. <div>
  641. <Text strong className="block mb-2">{t('完整的 Base URL,支持变量{model}')}</Text>
  642. <Input
  643. name='base_url'
  644. placeholder={t('请输入完整的URL,例如:https://api.openai.com/v1/chat/completions')}
  645. onChange={(value) => handleInputChange('base_url', value)}
  646. value={inputs.base_url}
  647. autoComplete='new-password'
  648. size="large"
  649. className="!rounded-lg"
  650. />
  651. </div>
  652. </>
  653. )}
  654. {inputs.type === 37 && (
  655. <Banner
  656. type='warning'
  657. description={t('Dify渠道只适配chatflow和agent,并且agent不支持图片!')}
  658. className='!rounded-lg'
  659. />
  660. )}
  661. {inputs.type !== 3 && inputs.type !== 8 && inputs.type !== 22 && inputs.type !== 36 && inputs.type !== 45 && (
  662. <div>
  663. <Text strong className="block mb-2">{t('API地址')}</Text>
  664. <Input
  665. name='base_url'
  666. placeholder={t('此项可选,用于通过自定义API地址来进行 API 调用,末尾不要带/v1和/')}
  667. onChange={(value) => handleInputChange('base_url', value)}
  668. value={inputs.base_url}
  669. autoComplete='new-password'
  670. size="large"
  671. className="!rounded-lg"
  672. />
  673. <Text type="tertiary" className="mt-1 text-xs">
  674. {t('对于官方渠道,new-api已经内置地址,除非是第三方代理站点或者Azure的特殊接入地址,否则不需要填写')}
  675. </Text>
  676. </div>
  677. )}
  678. {inputs.type === 22 && (
  679. <div>
  680. <Text strong className="block mb-2">{t('私有部署地址')}</Text>
  681. <Input
  682. name='base_url'
  683. placeholder={t('请输入私有部署地址,格式为:https://fastgpt.run/api/openapi')}
  684. onChange={(value) => handleInputChange('base_url', value)}
  685. value={inputs.base_url}
  686. autoComplete='new-password'
  687. size="large"
  688. className="!rounded-lg"
  689. />
  690. </div>
  691. )}
  692. {inputs.type === 36 && (
  693. <div>
  694. <Text strong className="block mb-2">
  695. {t('注意非Chat API,请务必填写正确的API地址,否则可能导致无法使用')}
  696. </Text>
  697. <Input
  698. name='base_url'
  699. placeholder={t('请输入到 /suno 前的路径,通常就是域名,例如:https://api.example.com')}
  700. onChange={(value) => handleInputChange('base_url', value)}
  701. value={inputs.base_url}
  702. autoComplete='new-password'
  703. size="large"
  704. className="!rounded-lg"
  705. />
  706. </div>
  707. )}
  708. </div>
  709. </Card>
  710. {/* Model Configuration Card */}
  711. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  712. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  713. background: 'linear-gradient(135deg, #4c1d95 0%, #6d28d9 50%, #7c3aed 100%)',
  714. position: 'relative'
  715. }}>
  716. <div className="absolute inset-0 overflow-hidden">
  717. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  718. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  719. </div>
  720. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  721. <IconCode size="large" style={{ color: '#ffffff' }} />
  722. </div>
  723. <div className="relative">
  724. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('模型配置')}</Text>
  725. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('模型选择和映射设置')}</div>
  726. </div>
  727. </div>
  728. <div className="space-y-4">
  729. <div>
  730. <Text strong className="block mb-2">{t('模型')}</Text>
  731. <Select
  732. placeholder={t('请选择该渠道所支持的模型')}
  733. name='models'
  734. required
  735. multiple
  736. selection
  737. filter
  738. searchPosition='dropdown'
  739. onChange={(value) => handleInputChange('models', value)}
  740. value={inputs.models}
  741. autoComplete='new-password'
  742. optionList={modelOptions}
  743. size="large"
  744. className="!rounded-lg"
  745. />
  746. </div>
  747. <div className="flex flex-wrap gap-2">
  748. <Button
  749. type='primary'
  750. onClick={() => handleInputChange('models', basicModels)}
  751. size="large"
  752. className="!rounded-lg"
  753. >
  754. {t('填入相关模型')}
  755. </Button>
  756. <Button
  757. type='secondary'
  758. onClick={() => handleInputChange('models', fullModels)}
  759. size="large"
  760. className="!rounded-lg"
  761. >
  762. {t('填入所有模型')}
  763. </Button>
  764. <Button
  765. type='tertiary'
  766. onClick={() => fetchUpstreamModelList('models')}
  767. size="large"
  768. className="!rounded-lg"
  769. >
  770. {t('获取模型列表')}
  771. </Button>
  772. <Button
  773. type='warning'
  774. onClick={() => handleInputChange('models', [])}
  775. size="large"
  776. className="!rounded-lg"
  777. >
  778. {t('清除所有模型')}
  779. </Button>
  780. </div>
  781. <div>
  782. <Input
  783. addonAfter={
  784. <Button type='primary' onClick={addCustomModels} className="!rounded-r-lg">
  785. {t('填入')}
  786. </Button>
  787. }
  788. placeholder={t('输入自定义模型名称')}
  789. value={customModel}
  790. onChange={(value) => setCustomModel(value.trim())}
  791. size="large"
  792. className="!rounded-lg"
  793. />
  794. </div>
  795. <div>
  796. <Text strong className="block mb-2">{t('模型重定向')}</Text>
  797. <TextArea
  798. placeholder={
  799. t('此项可选,用于修改请求体中的模型名称,为一个 JSON 字符串,键为请求中模型名称,值为要替换的模型名称,例如:') +
  800. `\n${JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2)}`
  801. }
  802. name='model_mapping'
  803. onChange={(value) => handleInputChange('model_mapping', value)}
  804. autosize
  805. value={inputs.model_mapping}
  806. autoComplete='new-password'
  807. className="!rounded-lg font-mono"
  808. />
  809. <Text
  810. className="!text-semi-color-primary cursor-pointer mt-1 block"
  811. onClick={() => handleInputChange('model_mapping', JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2))}
  812. >
  813. {t('填入模板')}
  814. </Text>
  815. </div>
  816. <div>
  817. <Text strong className="block mb-2">{t('默认测试模型')}</Text>
  818. <Input
  819. name='test_model'
  820. placeholder={t('不填则为模型列表第一个')}
  821. onChange={(value) => handleInputChange('test_model', value)}
  822. value={inputs.test_model}
  823. size="large"
  824. className="!rounded-lg"
  825. />
  826. </div>
  827. </div>
  828. </Card>
  829. {/* Advanced Settings Card */}
  830. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  831. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  832. background: 'linear-gradient(135deg, #92400e 0%, #d97706 50%, #f59e0b 100%)',
  833. position: 'relative'
  834. }}>
  835. <div className="absolute inset-0 overflow-hidden">
  836. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  837. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  838. </div>
  839. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  840. <IconSetting size="large" style={{ color: '#ffffff' }} />
  841. </div>
  842. <div className="relative">
  843. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('高级设置')}</Text>
  844. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('渠道的高级配置选项')}</div>
  845. </div>
  846. </div>
  847. <div className="space-y-4">
  848. <div>
  849. <Text strong className="block mb-2">{t('分组')}</Text>
  850. <Select
  851. placeholder={t('请选择可以使用该渠道的分组')}
  852. name='groups'
  853. required
  854. multiple
  855. selection
  856. allowAdditions
  857. additionLabel={t('请在系统设置页面编辑分组倍率以添加新的分组:')}
  858. onChange={(value) => handleInputChange('groups', value)}
  859. value={inputs.groups}
  860. autoComplete='new-password'
  861. optionList={groupOptions}
  862. size="large"
  863. className="!rounded-lg"
  864. />
  865. </div>
  866. {inputs.type === 18 && (
  867. <div>
  868. <Text strong className="block mb-2">{t('模型版本')}</Text>
  869. <Input
  870. name='other'
  871. placeholder={'请输入星火大模型版本,注意是接口地址中的版本号,例如:v2.1'}
  872. onChange={(value) => handleInputChange('other', value)}
  873. value={inputs.other}
  874. autoComplete='new-password'
  875. size="large"
  876. className="!rounded-lg"
  877. />
  878. </div>
  879. )}
  880. {inputs.type === 41 && (
  881. <div>
  882. <Text strong className="block mb-2">{t('部署地区')}</Text>
  883. <TextArea
  884. name='other'
  885. placeholder={t(
  886. '请输入部署地区,例如:us-central1\n支持使用模型映射格式\n' +
  887. '{\n' +
  888. ' "default": "us-central1",\n' +
  889. ' "claude-3-5-sonnet-20240620": "europe-west1"\n' +
  890. '}'
  891. )}
  892. autosize={{ minRows: 2 }}
  893. onChange={(value) => handleInputChange('other', value)}
  894. value={inputs.other}
  895. autoComplete='new-password'
  896. className="!rounded-lg font-mono"
  897. />
  898. <Text
  899. className="!text-semi-color-primary cursor-pointer mt-1 block"
  900. onClick={() => handleInputChange('other', JSON.stringify(REGION_EXAMPLE, null, 2))}
  901. >
  902. {t('填入模板')}
  903. </Text>
  904. </div>
  905. )}
  906. {inputs.type === 21 && (
  907. <div>
  908. <Text strong className="block mb-2">{t('知识库 ID')}</Text>
  909. <Input
  910. name='other'
  911. placeholder={'请输入知识库 ID,例如:123456'}
  912. onChange={(value) => handleInputChange('other', value)}
  913. value={inputs.other}
  914. autoComplete='new-password'
  915. size="large"
  916. className="!rounded-lg"
  917. />
  918. </div>
  919. )}
  920. {inputs.type === 39 && (
  921. <div>
  922. <Text strong className="block mb-2">Account ID</Text>
  923. <Input
  924. name='other'
  925. placeholder={'请输入Account ID,例如:d6b5da8hk1awo8nap34ube6gh'}
  926. onChange={(value) => handleInputChange('other', value)}
  927. value={inputs.other}
  928. autoComplete='new-password'
  929. size="large"
  930. className="!rounded-lg"
  931. />
  932. </div>
  933. )}
  934. {inputs.type === 49 && (
  935. <div>
  936. <Text strong className="block mb-2">{t('智能体ID')}</Text>
  937. <Input
  938. name='other'
  939. placeholder={'请输入智能体ID,例如:7342866812345'}
  940. onChange={(value) => handleInputChange('other', value)}
  941. value={inputs.other}
  942. autoComplete='new-password'
  943. size="large"
  944. className="!rounded-lg"
  945. />
  946. </div>
  947. )}
  948. <div>
  949. <Text strong className="block mb-2">{t('渠道标签')}</Text>
  950. <Input
  951. name='tag'
  952. placeholder={t('渠道标签')}
  953. onChange={(value) => handleInputChange('tag', value)}
  954. value={inputs.tag}
  955. autoComplete='new-password'
  956. size="large"
  957. className="!rounded-lg"
  958. />
  959. </div>
  960. <div>
  961. <Text strong className="block mb-2">{t('渠道优先级')}</Text>
  962. <Input
  963. name='priority'
  964. placeholder={t('渠道优先级')}
  965. onChange={(value) => {
  966. const number = parseInt(value);
  967. if (isNaN(number)) {
  968. handleInputChange('priority', value);
  969. } else {
  970. handleInputChange('priority', number);
  971. }
  972. }}
  973. value={inputs.priority}
  974. autoComplete='new-password'
  975. size="large"
  976. className="!rounded-lg"
  977. />
  978. </div>
  979. <div>
  980. <Text strong className="block mb-2">{t('渠道权重')}</Text>
  981. <Input
  982. name='weight'
  983. placeholder={t('渠道权重')}
  984. onChange={(value) => {
  985. const number = parseInt(value);
  986. if (isNaN(number)) {
  987. handleInputChange('weight', value);
  988. } else {
  989. handleInputChange('weight', number);
  990. }
  991. }}
  992. value={inputs.weight}
  993. autoComplete='new-password'
  994. size="large"
  995. className="!rounded-lg"
  996. />
  997. </div>
  998. <div>
  999. <Text strong className="block mb-2">{t('渠道额外设置')}</Text>
  1000. <TextArea
  1001. placeholder={
  1002. t('此项可选,用于配置渠道特定设置,为一个 JSON 字符串,例如:') +
  1003. '\n{\n "force_format": true\n}'
  1004. }
  1005. name='setting'
  1006. onChange={(value) => handleInputChange('setting', value)}
  1007. autosize
  1008. value={inputs.setting}
  1009. autoComplete='new-password'
  1010. className="!rounded-lg font-mono"
  1011. />
  1012. <div className="flex gap-2 mt-1">
  1013. <Text
  1014. className="!text-semi-color-primary cursor-pointer"
  1015. onClick={() => {
  1016. handleInputChange(
  1017. 'setting',
  1018. JSON.stringify({ force_format: true }, null, 2),
  1019. );
  1020. }}
  1021. >
  1022. {t('填入模板')}
  1023. </Text>
  1024. <Text
  1025. className="!text-semi-color-primary cursor-pointer"
  1026. onClick={() => {
  1027. window.open(
  1028. 'https://github.com/QuantumNous/new-api/blob/main/docs/channel/other_setting.md',
  1029. );
  1030. }}
  1031. >
  1032. {t('设置说明')}
  1033. </Text>
  1034. </div>
  1035. </div>
  1036. <div>
  1037. <Text strong className="block mb-2">{t('参数覆盖')}</Text>
  1038. <TextArea
  1039. placeholder={
  1040. t('此项可选,用于覆盖请求参数。不支持覆盖 stream 参数。为一个 JSON 字符串,例如:') +
  1041. '\n{\n "temperature": 0\n}'
  1042. }
  1043. name='param_override'
  1044. onChange={(value) => handleInputChange('param_override', value)}
  1045. autosize
  1046. value={inputs.param_override}
  1047. autoComplete='new-password'
  1048. className="!rounded-lg font-mono"
  1049. />
  1050. </div>
  1051. {inputs.type === 1 && (
  1052. <div>
  1053. <Text strong className="block mb-2">{t('组织')}</Text>
  1054. <Input
  1055. name='openai_organization'
  1056. placeholder={t('请输入组织org-xxx')}
  1057. onChange={(value) => handleInputChange('openai_organization', value)}
  1058. value={inputs.openai_organization}
  1059. size="large"
  1060. className="!rounded-lg"
  1061. />
  1062. <Text type="tertiary" className="mt-1 text-xs">
  1063. {t('组织,可选,不填则为默认组织')}
  1064. </Text>
  1065. </div>
  1066. )}
  1067. <div className="flex items-center">
  1068. <Checkbox
  1069. checked={autoBan}
  1070. onChange={() => setAutoBan(!autoBan)}
  1071. />
  1072. <Text strong className="ml-2">
  1073. {t('是否自动禁用(仅当自动禁用开启时有效),关闭后不会自动禁用该渠道')}
  1074. </Text>
  1075. </div>
  1076. <div>
  1077. <Text strong className="block mb-2">
  1078. {t('状态码复写(仅影响本地判断,不修改返回到上游的状态码)')}
  1079. </Text>
  1080. <TextArea
  1081. placeholder={
  1082. t('此项可选,用于复写返回的状态码,比如将claude渠道的400错误复写为500(用于重试),请勿滥用该功能,例如:') +
  1083. '\n' +
  1084. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2)
  1085. }
  1086. name='status_code_mapping'
  1087. onChange={(value) => handleInputChange('status_code_mapping', value)}
  1088. autosize
  1089. value={inputs.status_code_mapping}
  1090. autoComplete='new-password'
  1091. className="!rounded-lg font-mono"
  1092. />
  1093. <Text
  1094. className="!text-semi-color-primary cursor-pointer mt-1 block"
  1095. onClick={() => {
  1096. handleInputChange(
  1097. 'status_code_mapping',
  1098. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2),
  1099. );
  1100. }}
  1101. >
  1102. {t('填入模板')}
  1103. </Text>
  1104. </div>
  1105. </div>
  1106. </Card>
  1107. </div>
  1108. </Spin>
  1109. <ImagePreview
  1110. src={modalImageUrl}
  1111. visible={isModalOpenurl}
  1112. onVisibleChange={(visible) => setIsModalOpenurl(visible)}
  1113. />
  1114. </SideSheet>
  1115. </>
  1116. );
  1117. };
  1118. export default EditChannel;