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

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

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