修改流式错误时的结构,兼容业务

This commit is contained in:
haochun
2024-10-10 10:53:18 +08:00
parent 9267c5f12e
commit 8726729ade
2 changed files with 40 additions and 12 deletions

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