Sfoglia il codice sorgente

feat: 完善获取模型列表功能 (close #237)

1808837298@qq.com 1 anno fa
parent
commit
0867d36fc7
1 ha cambiato i file con 53 aggiunte e 3 eliminazioni
  1. 53 3
      web/src/pages/Channel/EditChannel.js

+ 53 - 3
web/src/pages/Channel/EditChannel.js

@@ -37,6 +37,8 @@ const STATUS_CODE_MAPPING_EXAMPLE = {
   400: '500',
   400: '500',
 };
 };
 
 
+const fetchButtonTips = "1. 新建渠道时,请求通过当前浏览器发出;2. 编辑已有渠道,请求通过后端服务器发出"
+
 function type2secretPrompt(type) {
 function type2secretPrompt(type) {
   // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
   // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
   switch (type) {
   switch (type) {
@@ -175,12 +177,60 @@ const EditChannel = (props) => {
     setLoading(false);
     setLoading(false);
   };
   };
 
 
+
+  const fetchUpstreamModelList = async (name) => {
+    if (inputs["type"] !== 1) {
+      showError("仅支持 OpenAI 接口格式")
+      return;
+    }
+    setLoading(true)
+    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("请填写密钥")
+        err = true
+      } else {
+        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('获取模型列表失败');
+    }
+    setLoading(false);
+  }
+
   const fetchModels = async () => {
   const fetchModels = async () => {
     try {
     try {
       let res = await API.get(`/api/channel/models`);
       let res = await API.get(`/api/channel/models`);
-      if (res === undefined) {
-        return;
-      }
       let localModelOptions = res.data.data.map((model) => ({
       let localModelOptions = res.data.data.map((model) => ({
         label: model.id,
         label: model.id,
         value: model.id,
         value: model.id,