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