mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-19 00:46:37 +08:00
fix: 流模式网络错误导致0补
This commit is contained in:
parent
6eb30ec3e6
commit
2d36dee17c
@ -75,26 +75,44 @@ func OpenaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*d
|
|||||||
err := json.Unmarshal(common.StringToByteSlice(streamResp), &streamResponses)
|
err := json.Unmarshal(common.StringToByteSlice(streamResp), &streamResponses)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("error unmarshalling stream response: " + err.Error())
|
common.SysError("error unmarshalling stream response: " + err.Error())
|
||||||
return // just ignore the error
|
for _, item := range streamItems {
|
||||||
|
var streamResponse dto.ChatCompletionsStreamResponseSimple
|
||||||
|
err := json.Unmarshal(common.StringToByteSlice(item), &streamResponse)
|
||||||
|
if err == nil {
|
||||||
|
for _, choice := range streamResponse.Choices {
|
||||||
|
responseTextBuilder.WriteString(choice.Delta.Content)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for _, streamResponse := range streamResponses {
|
for _, streamResponse := range streamResponses {
|
||||||
for _, choice := range streamResponse.Choices {
|
for _, choice := range streamResponse.Choices {
|
||||||
responseTextBuilder.WriteString(choice.Delta.Content)
|
responseTextBuilder.WriteString(choice.Delta.Content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
case relayconstant.RelayModeCompletions:
|
case relayconstant.RelayModeCompletions:
|
||||||
var streamResponses []dto.CompletionsStreamResponse
|
var streamResponses []dto.CompletionsStreamResponse
|
||||||
err := json.Unmarshal(common.StringToByteSlice(streamResp), &streamResponses)
|
err := json.Unmarshal(common.StringToByteSlice(streamResp), &streamResponses)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("error unmarshalling stream response: " + err.Error())
|
common.SysError("error unmarshalling stream response: " + err.Error())
|
||||||
return // just ignore the error
|
for _, item := range streamItems {
|
||||||
|
var streamResponse dto.CompletionsStreamResponse
|
||||||
|
err := json.Unmarshal(common.StringToByteSlice(item), &streamResponse)
|
||||||
|
if err == nil {
|
||||||
|
for _, choice := range streamResponse.Choices {
|
||||||
|
responseTextBuilder.WriteString(choice.Text)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for _, streamResponse := range streamResponses {
|
for _, streamResponse := range streamResponses {
|
||||||
for _, choice := range streamResponse.Choices {
|
for _, choice := range streamResponse.Choices {
|
||||||
responseTextBuilder.WriteString(choice.Text)
|
responseTextBuilder.WriteString(choice.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if len(dataChan) > 0 {
|
if len(dataChan) > 0 {
|
||||||
// wait data out
|
// wait data out
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
|
Loading…
Reference in New Issue
Block a user