Compare commits

...

9 Commits

Author SHA1 Message Date
haochun
7b3a33175a Merge 8726729ade into 3915ce9814 2024-12-27 23:35:49 +08:00
JustSong
3915ce9814 chore: update ci yaml
Some checks failed
CI / Unit tests (push) Has been cancelled
CI / commit_lint (push) Has been cancelled
2024-12-27 22:01:37 +08:00
JustSong
999defc88b chore: update readme 2024-12-27 21:59:38 +08:00
JustSong
b51c47bc77 docs: update README.md 2024-12-27 21:45:51 +08:00
JustSong
4f25cde132 fix: add branch check 2024-12-27 20:41:20 +08:00
JustSong
d89e9d7e44 fix: add branch limitation and drop pull_request trigger for ci.yml 2024-12-27 20:34:04 +08:00
haochun
8726729ade 修改流式错误时的结构,兼容业务 2024-10-10 10:53:18 +08:00
haochun
9267c5f12e 修改流式的实现,按照流式的格式去输出,不直接return 2024-10-09 14:46:03 +08:00
haochun
5e3042752e fix: 修复阿里云官方的绿网响应未正常处理,导致业务则中断的问题,做了兼容处理 2024-10-08 10:51:44 +08:00
4 changed files with 57 additions and 6 deletions

View File

@@ -12,8 +12,6 @@ name: CI
# would trigger our jobs twice on pull requests (once from "push" event and once
# from "pull_request->synchronize")
on:
pull_request:
types: [opened, reopened, synchronize]
push:
branches:
- 'main'

View File

@@ -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

View File

@@ -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
}
@@ -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
@@ -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{
@@ -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)

View File

@@ -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"`