合并上游、支持已有渠道获取模型

This commit is contained in:
bubu
2024-05-21 22:21:25 +08:00
7 changed files with 117 additions and 159 deletions

View File

@@ -37,8 +37,6 @@ const STATUS_CODE_MAPPING_EXAMPLE = {
400: '500',
};
const fetchButtonTips = "1. 新建渠道时请求通过当前浏览器发出2. 编辑已有渠道,请求通过后端服务器发出"
function type2secretPrompt(type) {
// inputs.type === 15 ? '按照如下格式输入APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
switch (type) {
@@ -90,55 +88,6 @@ const EditChannel = (props) => {
const [basicModels, setBasicModels] = useState([]);
const [fullModels, setFullModels] = useState([]);
const [customModel, setCustomModel] = useState('');
const fetchUpstreamModelList = async (name) => {
if (inputs["type"] !== 1) {
showError("仅支持 OpenAI 接口格式")
return;
}
const models = inputs["models"] || []
let err = false;
if (isEdit) {
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("请填写密钥")
return;
}
try {
const host = new URL((inputs["base_url"] || "https://api.openai.com"))
const url = `https://${host.hostname}/v1/models`;
const key = inputs["key"];
const res = await axios.get(url, {
headers: {
'Authorization': `Bearer ${key}`
}
})
if (res.data && res.data?.success) {
models.push(...es.data.data.map((model) => model.id))
} else {
err = true
}
}
catch (error) {
err = true
}
}
if (!err) {
handleInputChange(name, Array.from(new Set(models)));
showSuccess("获取模型列表成功");
} else {
showError('获取模型列表失败');
}
}
const handleInputChange = (name, value) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
if (name === 'type') {
@@ -284,7 +233,7 @@ const EditChannel = (props) => {
fetchModels().then();
fetchGroups().then();
if (isEdit) {
loadChannel().then(() => { });
loadChannel().then(() => {});
} else {
setInputs(originInputs);
let localModels = getChannelModels(inputs.type);
@@ -354,17 +303,18 @@ const EditChannel = (props) => {
const addCustomModels = () => {
if (customModel.trim() === '') return;
// 使用逗号分隔字符串,然后去除每个模型名称前后的空格
const modelArray = customModel.split(',').map(model => model.trim());
const modelArray = customModel.split(',').map((model) => model.trim());
let localModels = [...inputs.models];
let localModelOptions = [...modelOptions];
let hasError = false;
modelArray.forEach(model => {
modelArray.forEach((model) => {
// 检查模型是否已存在,且模型名称非空
if (model && !localModels.includes(model)) {
localModels.push(model); // 添加到模型列表
localModelOptions.push({ // 添加到下拉选项
localModelOptions.push({
// 添加到下拉选项
key: model,
text: model,
value: model,
@@ -486,11 +436,15 @@ const EditChannel = (props) => {
{inputs.type === 8 && (
<>
<div style={{ marginTop: 10 }}>
<Typography.Text strong>Base URL</Typography.Text>
<Typography.Text strong>
完整的 Base URL支持变量{'{model}'}
</Typography.Text>
</div>
<Input
name='base_url'
placeholder={'请输入自定义渠道的 Base URL'}
placeholder={
'请输入完整的URL例如https://api.openai.com/v1/chat/completions'
}
onChange={(value) => {
handleInputChange('base_url', value);
}}
@@ -723,7 +677,7 @@ const EditChannel = (props) => {
onChange={() => {
setAutoBan(!autoBan);
}}
// onChange={handleInputChange}
// onChange={handleInputChange}
/>
<Typography.Text strong>
是否自动禁用仅当自动禁用开启时有效关闭后不会自动禁用该渠道