From 38bff1a0e041c02a6ef6692d8b62c2b50676b8a3 Mon Sep 17 00:00:00 2001 From: RedwindA Date: Thu, 7 Aug 2025 00:54:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20GoogleOpen?= =?UTF-8?q?AI=20=E5=85=BC=E5=AE=B9=E6=A8=A1=E5=9E=8B=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BD=93=EF=BC=8C=E7=AE=80=E5=8C=96=20FetchU?= =?UTF-8?q?pstreamModels=20=E5=87=BD=E6=95=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/channel.go | 57 ++++++------------------------------------- 1 file changed, 8 insertions(+), 49 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index 9f46ca35..3361cbf5 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -36,30 +36,11 @@ type OpenAIModel struct { Parent string `json:"parent"` } -type GoogleOpenAICompatibleModels []struct { - Name string `json:"name"` - Version string `json:"version"` - DisplayName string `json:"displayName"` - Description string `json:"description,omitempty"` - InputTokenLimit int `json:"inputTokenLimit"` - OutputTokenLimit int `json:"outputTokenLimit"` - SupportedGenerationMethods []string `json:"supportedGenerationMethods"` - Temperature float64 `json:"temperature,omitempty"` - TopP float64 `json:"topP,omitempty"` - TopK int `json:"topK,omitempty"` - MaxTemperature int `json:"maxTemperature,omitempty"` -} - type OpenAIModelsResponse struct { Data []OpenAIModel `json:"data"` Success bool `json:"success"` } -type GoogleOpenAICompatibleResponse struct { - Models []GoogleOpenAICompatibleModels `json:"models"` - NextPageToken string `json:"nextPageToken"` -} - func parseStatusFilter(statusParam string) int { switch strings.ToLower(statusParam) { case "enabled", "1": @@ -203,7 +184,7 @@ func FetchUpstreamModels(c *gin.Context) { switch channel.Type { case constant.ChannelTypeGemini: // curl https://example.com/v1beta/models?key=$GEMINI_API_KEY - url = fmt.Sprintf("%s/v1beta/openai/models?key=%s", baseURL, channel.Key) + url = fmt.Sprintf("%s/v1beta/openai/models", baseURL) // Remember key in url since we need to use AuthHeader case constant.ChannelTypeAli: url = fmt.Sprintf("%s/compatible-mode/v1/models", baseURL) default: @@ -213,7 +194,7 @@ func FetchUpstreamModels(c *gin.Context) { // 获取响应体 - 根据渠道类型决定是否添加 AuthHeader var body []byte if channel.Type == constant.ChannelTypeGemini { - body, err = GetResponseBody("GET", url, channel, nil) // I don't know why, but Gemini requires no AuthHeader + body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key)) // Use AuthHeader since Gemini now forces it } else { body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key)) } @@ -223,34 +204,12 @@ func FetchUpstreamModels(c *gin.Context) { } var result OpenAIModelsResponse - var parseSuccess bool - - // 适配特殊格式 - switch channel.Type { - case constant.ChannelTypeGemini: - var googleResult GoogleOpenAICompatibleResponse - if err = json.Unmarshal(body, &googleResult); err == nil { - // 转换Google格式到OpenAI格式 - for _, model := range googleResult.Models { - for _, gModel := range model { - result.Data = append(result.Data, OpenAIModel{ - ID: gModel.Name, - }) - } - } - parseSuccess = true - } - } - - // 如果解析失败,尝试OpenAI格式 - if !parseSuccess { - if err = json.Unmarshal(body, &result); err != nil { - c.JSON(http.StatusOK, gin.H{ - "success": false, - "message": fmt.Sprintf("解析响应失败: %s", err.Error()), - }) - return - } + if err = json.Unmarshal(body, &result); err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": fmt.Sprintf("解析响应失败: %s", err.Error()), + }) + return } var ids []string From 76d71a032acf65e1433a54e7e4b10ea28dc7b162 Mon Sep 17 00:00:00 2001 From: RedwindA Date: Thu, 7 Aug 2025 01:01:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20FetchUpstreamMo?= =?UTF-8?q?dels=20=E5=87=BD=E6=95=B0=E4=B8=AD=20AuthHeader=20=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=EF=BC=8C=E7=A1=AE=E4=BF=9D=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=20=E5=A4=9Akey=E8=81=9A=E5=90=88=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/channel.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index 3361cbf5..284597c3 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -193,10 +193,11 @@ func FetchUpstreamModels(c *gin.Context) { // 获取响应体 - 根据渠道类型决定是否添加 AuthHeader var body []byte + key := strings.Split(channel.Key, "\n")[0] if channel.Type == constant.ChannelTypeGemini { - body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key)) // Use AuthHeader since Gemini now forces it + body, err = GetResponseBody("GET", url, channel, GetAuthHeader(key)) // Use AuthHeader since Gemini now forces it } else { - body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key)) + body, err = GetResponseBody("GET", url, channel, GetAuthHeader(key)) } if err != nil { common.ApiError(c, err) From ed95a9f2b27124de1f20adc6b876ffc6502fbbce Mon Sep 17 00:00:00 2001 From: RedwindA Date: Thu, 7 Aug 2025 01:06:50 +0800 Subject: [PATCH 3/3] fix a typo in comment --- controller/channel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/channel.go b/controller/channel.go index 284597c3..020a3327 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -184,7 +184,7 @@ func FetchUpstreamModels(c *gin.Context) { switch channel.Type { case constant.ChannelTypeGemini: // curl https://example.com/v1beta/models?key=$GEMINI_API_KEY - url = fmt.Sprintf("%s/v1beta/openai/models", baseURL) // Remember key in url since we need to use AuthHeader + url = fmt.Sprintf("%s/v1beta/openai/models", baseURL) // Remove key in url since we need to use AuthHeader case constant.ChannelTypeAli: url = fmt.Sprintf("%s/compatible-mode/v1/models", baseURL) default: