From 5e3042752e174a9bb97c8a285320f91133705627 Mon Sep 17 00:00:00 2001 From: haochun <759869747@qq.com> Date: Tue, 8 Oct 2024 10:51:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E5=AE=98=E6=96=B9=E7=9A=84=E7=BB=BF=E7=BD=91=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E6=9C=AA=E6=AD=A3=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=B8=9A=E5=8A=A1=E5=88=99=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E5=81=9A=E4=BA=86=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- relay/adaptor/ali/main.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/relay/adaptor/ali/main.go b/relay/adaptor/ali/main.go index f9039dbe49..bd0d3344c2 100644 --- a/relay/adaptor/ali/main.go +++ b/relay/adaptor/ali/main.go @@ -199,6 +199,20 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC logger.SysError("error unmarshalling stream response: " + err.Error()) continue } + + // Check for known error codes and handle accordingly + if aliResponse.Code != "" { + return &model.ErrorWithStatusCode{ + Error: model.Error{ + Message: aliResponse.Message, + Type: aliResponse.Code, + Param: aliResponse.RequestId, + Code: aliResponse.Code, + }, + StatusCode: resp.StatusCode, + }, nil + } + if aliResponse.Usage.OutputTokens != 0 { usage.PromptTokens = aliResponse.Usage.InputTokens usage.CompletionTokens = aliResponse.Usage.OutputTokens @@ -243,6 +257,8 @@ func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, * if err != nil { return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil } + + // Check for known error codes and handle accordingly if aliResponse.Code != "" { return &model.ErrorWithStatusCode{ Error: model.Error{ @@ -254,6 +270,7 @@ func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, * StatusCode: resp.StatusCode, }, nil } + fullTextResponse := responseAli2OpenAI(&aliResponse) fullTextResponse.Model = "qwen" jsonResponse, err := json.Marshal(fullTextResponse) From 9267c5f12e3a324181e26371bd76a5b2c9d3fd7f Mon Sep 17 00:00:00 2001 From: haochun <759869747@qq.com> Date: Wed, 9 Oct 2024 14:46:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E7=8E=B0=EF=BC=8C=E6=8C=89=E7=85=A7=E6=B5=81?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E6=A0=BC=E5=BC=8F=E5=8E=BB=E8=BE=93=E5=87=BA?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E7=9B=B4=E6=8E=A5return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 6 +++--- relay/adaptor/ali/main.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index ade561e408..60ef0302fb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,15 +5,15 @@ COPY ./VERSION . COPY ./web . WORKDIR /web/default -RUN npm install +RUN npm config set registry https://mirrors.huaweicloud.com/repository/npm/ && npm install RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build WORKDIR /web/berry -RUN npm install +RUN npm config set registry https://mirrors.huaweicloud.com/repository/npm/ && npm install RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build WORKDIR /web/air -RUN npm install +RUN npm config set registry https://mirrors.huaweicloud.com/repository/npm/ && npm install RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build FROM golang:alpine AS builder2 diff --git a/relay/adaptor/ali/main.go b/relay/adaptor/ali/main.go index bd0d3344c2..a77da8c788 100644 --- a/relay/adaptor/ali/main.go +++ b/relay/adaptor/ali/main.go @@ -202,7 +202,8 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC // Check for known error codes and handle accordingly if aliResponse.Code != "" { - return &model.ErrorWithStatusCode{ + + errorResponse := &model.ErrorWithStatusCode{ Error: model.Error{ Message: aliResponse.Message, Type: aliResponse.Code, @@ -210,7 +211,14 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC Code: aliResponse.Code, }, StatusCode: resp.StatusCode, - }, nil + } + + err = render.ObjectData(c, errorResponse) + if err != nil { + logger.SysError(err.Error()) + } + render.Done(c) + return nil, nil } if aliResponse.Usage.OutputTokens != 0 { From 8726729ade006d41018b046825a527a35462e144 Mon Sep 17 00:00:00 2001 From: haochun <759869747@qq.com> Date: Thu, 10 Oct 2024 10:53:18 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=97=B6=E7=9A=84=E7=BB=93=E6=9E=84=EF=BC=8C?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=B8=9A=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- relay/adaptor/ali/main.go | 32 ++++++++++++++++++++------------ relay/adaptor/openai/model.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/relay/adaptor/ali/main.go b/relay/adaptor/ali/main.go index a77da8c788..43198c482b 100644 --- a/relay/adaptor/ali/main.go +++ b/relay/adaptor/ali/main.go @@ -147,7 +147,24 @@ func responseAli2OpenAI(response *ChatResponse) *openai.TextResponse { return &fullTextResponse } -func streamResponseAli2OpenAI(aliResponse *ChatResponse) *openai.ChatCompletionsStreamResponse { +func streamResponseAli2OpenAI(aliResponse *ChatResponse) interface{} { + if aliResponse.Code != "" { + var choice openai.ChatCompletionsStreamResponseChoice + choice.Index = 0 + choice.Delta = model.Message{ + Role: "assistant", + Content: "", + } + response := openai.ChatCompletionsErrorStreamResponse{ + Id: aliResponse.RequestId, + Object: "chat.completion.chunk", + Created: helper.GetTimestamp(), + Model: "qwen", + ErrorCode: aliResponse.Code, + Choices: []openai.ChatCompletionsStreamResponseChoice{choice}, + } + return &response + } if len(aliResponse.Output.Choices) == 0 { return nil } @@ -202,18 +219,9 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC // Check for known error codes and handle accordingly if aliResponse.Code != "" { + response := streamResponseAli2OpenAI(&aliResponse) - errorResponse := &model.ErrorWithStatusCode{ - Error: model.Error{ - Message: aliResponse.Message, - Type: aliResponse.Code, - Param: aliResponse.RequestId, - Code: aliResponse.Code, - }, - StatusCode: resp.StatusCode, - } - - err = render.ObjectData(c, errorResponse) + err = render.ObjectData(c, response) if err != nil { logger.SysError(err.Error()) } diff --git a/relay/adaptor/openai/model.go b/relay/adaptor/openai/model.go index 4c974de4ad..d45bd316fe 100644 --- a/relay/adaptor/openai/model.go +++ b/relay/adaptor/openai/model.go @@ -97,6 +97,16 @@ type TextResponse struct { model.Usage `json:"usage"` } +type ErrorTextResponse struct { + Id string `json:"id"` + Model string `json:"model,omitempty"` + Object string `json:"object"` + ErrorCode string `json:"error_code"` + Created int64 `json:"created"` + Choices []TextResponseChoice `json:"choices"` + model.Usage `json:"usage"` +} + type EmbeddingResponseItem struct { Object string `json:"object"` Index int `json:"index"` @@ -137,6 +147,16 @@ type ChatCompletionsStreamResponse struct { Usage *model.Usage `json:"usage,omitempty"` } +type ChatCompletionsErrorStreamResponse struct { + Id string `json:"id"` + Object string `json:"object"` + Created int64 `json:"created"` + ErrorCode string `json:"error_code"` + Model string `json:"model"` + Choices []ChatCompletionsStreamResponseChoice `json:"choices"` + Usage *model.Usage `json:"usage,omitempty"` +} + type CompletionsStreamResponse struct { Choices []struct { Text string `json:"text"`