Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.6.9 alpha 1008 #1871

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion relay/adaptor/ali/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -199,6 +216,19 @@ 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 != "" {
response := streamResponseAli2OpenAI(&aliResponse)

err = render.ObjectData(c, response)
if err != nil {
logger.SysError(err.Error())
}
render.Done(c)
return nil, nil
}

if aliResponse.Usage.OutputTokens != 0 {
usage.PromptTokens = aliResponse.Usage.InputTokens
usage.CompletionTokens = aliResponse.Usage.OutputTokens
Expand Down Expand Up @@ -243,6 +273,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{
Expand All @@ -254,6 +286,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)
Expand Down
20 changes: 20 additions & 0 deletions relay/adaptor/openai/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down