From 076ec68989cd1a0ad07d2ddfd20ec0c55c7c55f8 Mon Sep 17 00:00:00 2001 From: mlkt <365690226@qq.com> Date: Sun, 26 May 2024 02:52:22 +0800 Subject: [PATCH 1/4] fix: ali image model cannot be found and the error with the incorrect return format. --- relay/adaptor/ali/image.go | 7 ++++++- relay/adaptor/ali/main.go | 2 +- relay/controller/image.go | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/relay/adaptor/ali/image.go b/relay/adaptor/ali/image.go index 8261803d..2b34d17e 100644 --- a/relay/adaptor/ali/image.go +++ b/relay/adaptor/ali/image.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "github.com/gin-gonic/gin" + "github.com/songquanpeng/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/helper" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/relay/adaptor/openai" @@ -19,7 +20,11 @@ import ( func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) { apiKey := c.Request.Header.Get("Authorization") apiKey = strings.TrimPrefix(apiKey, "Bearer ") - responseFormat := c.GetString("response_format") + + var responseFormat string + if req, exists := c.Get(ctxkey.ConvertedRequest); exists { + responseFormat = req.(*ImageRequest).ResponseFormat + } var aliTaskResponse TaskResponse responseBody, err := io.ReadAll(resp.Body) diff --git a/relay/adaptor/ali/main.go b/relay/adaptor/ali/main.go index 0462c26b..bf8965e6 100644 --- a/relay/adaptor/ali/main.go +++ b/relay/adaptor/ali/main.go @@ -69,7 +69,7 @@ func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *EmbeddingReque func ConvertImageRequest(request model.ImageRequest) *ImageRequest { var imageRequest ImageRequest imageRequest.Input.Prompt = request.Prompt - imageRequest.Model = request.Model + imageRequest.Model = strings.TrimPrefix(request.Model, "ali-") imageRequest.Parameters.Size = strings.Replace(request.Size, "x", "*", -1) imageRequest.Parameters.N = request.N imageRequest.ResponseFormat = request.ResponseFormat diff --git a/relay/controller/image.go b/relay/controller/image.go index 691c7c0e..c6430a8d 100644 --- a/relay/controller/image.go +++ b/relay/controller/image.go @@ -91,6 +91,7 @@ func RelayImageHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus if err != nil { return openai.ErrorWrapper(err, "marshal_image_request_failed", http.StatusInternalServerError) } + c.Set(ctxkey.ConvertedRequest, finalRequest) requestBody = bytes.NewBuffer(jsonStr) } From e54abc0f89ac184eedcd3bc15a3ab05e40ec4df3 Mon Sep 17 00:00:00 2001 From: mlkt <365690226@qq.com> Date: Sun, 26 May 2024 05:57:05 +0800 Subject: [PATCH 2/4] =?UTF-8?q?aiproxy=E6=94=AF=E6=8C=81vip=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=EF=BC=88=E9=85=8D=E7=BD=AE=E6=B8=A0=E9=81=93=E6=97=B6?= =?UTF-8?q?=E5=9C=A8key=E5=90=8E=E5=8A=A0=E4=B8=8A=20#vip=20=E5=90=8E?= =?UTF-8?q?=E7=BC=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- relay/adaptor/aiproxy/adaptor.go | 11 +++++++++-- relay/adaptor/openai/adaptor.go | 11 ++++++++++- relay/billing/ratio/image.go | 2 -- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/relay/adaptor/aiproxy/adaptor.go b/relay/adaptor/aiproxy/adaptor.go index 42d49c0a..f2485727 100644 --- a/relay/adaptor/aiproxy/adaptor.go +++ b/relay/adaptor/aiproxy/adaptor.go @@ -9,6 +9,7 @@ import ( "github.com/songquanpeng/one-api/relay/model" "io" "net/http" + "strings" ) type Adaptor struct { @@ -20,12 +21,18 @@ func (a *Adaptor) Init(meta *meta.Meta) { } func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { - return fmt.Sprintf("%s/api/library/ask", meta.BaseURL), nil + baseURL := meta.BaseURL + if strings.HasSuffix(meta.APIKey, "#vip") { + baseURL = "https://apivip.aiproxy.io" + } + return fmt.Sprintf("%s/api/library/ask", baseURL), nil } func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error { adaptor.SetupCommonRequestHeader(c, req, meta) - req.Header.Set("Authorization", "Bearer "+meta.APIKey) + apiKey := meta.APIKey + apiKey = strings.TrimSuffix(apiKey, "#vip") + req.Header.Set("Authorization", "Bearer "+apiKey) return nil } diff --git a/relay/adaptor/openai/adaptor.go b/relay/adaptor/openai/adaptor.go index db569e4f..72440cc0 100644 --- a/relay/adaptor/openai/adaptor.go +++ b/relay/adaptor/openai/adaptor.go @@ -48,6 +48,11 @@ func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { return minimax.GetRequestURL(meta) case channeltype.Doubao: return doubao.GetRequestURL(meta) + case channeltype.AIProxy: + if strings.HasSuffix(meta.APIKey, "#vip") { + return GetFullRequestURL("https://apivip.aiproxy.io", meta.RequestURLPath, meta.ChannelType), nil + } + fallthrough default: return GetFullRequestURL(meta.BaseURL, meta.RequestURLPath, meta.ChannelType), nil } @@ -59,7 +64,11 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *me req.Header.Set("api-key", meta.APIKey) return nil } - req.Header.Set("Authorization", "Bearer "+meta.APIKey) + apiKey := meta.APIKey + if meta.ChannelType == channeltype.AIProxy { + apiKey = strings.TrimSuffix(apiKey, "#vip") + } + req.Header.Set("Authorization", "Bearer "+apiKey) if meta.ChannelType == channeltype.OpenRouter { req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api") req.Header.Set("X-Title", "One API") diff --git a/relay/billing/ratio/image.go b/relay/billing/ratio/image.go index ced0c667..640fb0af 100644 --- a/relay/billing/ratio/image.go +++ b/relay/billing/ratio/image.go @@ -51,6 +51,4 @@ var ImagePromptLengthLimitations = map[string]int{ } var ImageOriginModelName = map[string]string{ - "ali-stable-diffusion-xl": "stable-diffusion-xl", - "ali-stable-diffusion-v1.5": "stable-diffusion-v1.5", } 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 3/4] =?UTF-8?q?=E4=BF=AE=E6=AD=A3AIProxy=E8=AE=BF=E9=97=AE?= =?UTF-8?q?gpt-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) From c885953c6d1c82a3932cb670691e66a73448d0cc Mon Sep 17 00:00:00 2001 From: mlkt <365690226@qq.com> Date: Wed, 12 Jun 2024 17:52:01 +0800 Subject: [PATCH 4/4] =?UTF-8?q?docker=E6=9E=84=E5=BB=BA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8A=A0=E9=80=9F=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6743b139..ac3963e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,22 +5,24 @@ COPY ./VERSION . COPY ./web . WORKDIR /web/default +RUN npm config set registry https://registry.npmmirror.com RUN npm install RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build WORKDIR /web/berry -RUN npm install +RUN npm install --force RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build WORKDIR /web/air -RUN npm install +RUN npm install --force RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build FROM golang AS builder2 ENV GO111MODULE=on \ CGO_ENABLED=1 \ - GOOS=linux + GOOS=linux \ + GOPROXY=https://goproxy.cn,direct WORKDIR /build ADD go.mod go.sum ./