fix: json issue

This commit is contained in:
ckt1031 2023-08-20 01:24:59 +08:00
parent 9d15776712
commit d5dfacd8a6
2 changed files with 10 additions and 12 deletions

View File

@ -111,15 +111,14 @@ func testChannel(channel *model.Channel, request ChatRequest) (error, *OpenAIErr
if !strings.HasPrefix(data, "data:") {
continue
}
data = data[6:]
data = strings.TrimPrefix(data, "data:")
if !strings.HasPrefix(data, "[DONE]") {
var streamResponse ChatCompletionsStreamResponse
err := json.Unmarshal([]byte(data), &streamResponse)
if err != nil {
return err, nil
}
for _, choice := range streamResponse.Choices {
responseText += choice.Delta.Content
if err == nil {
for _, choice := range streamResponse.Choices {
responseText += choice.Delta.Content
}
}
}
}

View File

@ -57,19 +57,18 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
}
}
dataChan <- data
data = data[6:]
data = strings.TrimPrefix(data, "data:")
if !strings.HasPrefix(data, "[DONE]") {
switch relayMode {
case RelayModeChatCompletions:
var streamResponse ChatCompletionsStreamResponse
err := json.Unmarshal([]byte(data), &streamResponse)
if err != nil {
common.SysError("error unmarshalling stream response: " + err.Error())
if err == nil {
for _, choice := range streamResponse.Choices {
responseText += choice.Delta.Content
}
continue // just ignore the error
}
for _, choice := range streamResponse.Choices {
responseText += choice.Delta.Content
}
case RelayModeCompletions:
var streamResponse CompletionsStreamResponse
err := json.Unmarshal([]byte(data), &streamResponse)