From b01f0aff38cab04610c1bead31fd32f6599e9a26 Mon Sep 17 00:00:00 2001 From: mlkt <365690226@qq.com> Date: Sun, 26 May 2024 06:46:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3AIProxy=E8=AE=BF=E9=97=AEgpt-?= =?UTF-8?q?4o=E7=9A=84max=5Ftokens=E5=8F=82=E6=95=B0=E5=BF=85=E5=A1=AB?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- relay/controller/text.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/relay/controller/text.go b/relay/controller/text.go index 6ed19b1d..2b1dfe73 100644 --- a/relay/controller/text.go +++ b/relay/controller/text.go @@ -15,9 +15,14 @@ import ( "github.com/songquanpeng/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/model" "io" + "math" "net/http" + "os" + "strings" ) +var fixAIProxyGpt4oMaxTokens = os.Getenv("FIX_AI_PROXY_GPT4O_MAX_TOKENS") == "1" + func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode { ctx := c.Request.Context() meta := meta.GetByContext(c) @@ -57,8 +62,18 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode { var requestBody io.Reader if meta.APIType == apitype.OpenAI { // no need to convert request for openai - shouldResetRequestBody := isModelMapped || meta.ChannelType == channeltype.Baichuan // frequency_penalty 0 is not acceptable for baichuan + shouldResetRequestBody := isModelMapped || + meta.ChannelType == channeltype.Baichuan /*frequency_penalty 0 is not acceptable for baichuan*/ || + (meta.ChannelType == channeltype.AIProxy && fixAIProxyGpt4oMaxTokens && strings.HasPrefix(textRequest.Model, "gpt-4o")) if shouldResetRequestBody { + if meta.ChannelType == channeltype.AIProxy { + maxTokens := textRequest.MaxTokens + maxTokens = int(math.Min(float64(maxTokens), 4096)) + if maxTokens == 0 { + maxTokens = 4096 + } + textRequest.MaxTokens = maxTokens + } jsonStr, err := json.Marshal(textRequest) if err != nil { return openai.ErrorWrapper(err, "json_marshal_failed", http.StatusInternalServerError)