fix: improved checking for chatgptweb

This commit is contained in:
ckt1031 2023-07-15 22:16:52 +08:00
parent 5b8a826cf9
commit 4e31c3991d

View File

@ -186,34 +186,25 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
break break
} }
} else if channel.Type == common.ChannelTypeChatGPTWeb { } else if channel.Type == common.ChannelTypeChatGPTWeb {
// data may contain multiple json objects, so we need to split them scanner := bufio.NewScanner(resp.Body)
// they are "{....}{....}{....}" or "{....}\n{....}\n{....}" or "{....}" go func() {
for scanner.Scan() {
// remove all spaces and newlines outside of json objects
jsonObjs := strings.Split(data, "\n") // Split the data into multiple JSON objects
for _, jsonObj := range jsonObjs {
if jsonObj == "" {
continue
}
var chatResponse ChatGptWebChatResponse var chatResponse ChatGptWebChatResponse
err = json.Unmarshal([]byte(jsonObj), &chatResponse) err = json.Unmarshal(scanner.Bytes(), &chatResponse)
if err != nil { if err != nil {
// Print the body in string log.Println("error unmarshal chat response: " + err.Error())
buf := new(bytes.Buffer) continue
buf.ReadFrom(resp.Body)
common.SysError("error unmarshalling chat response: " + err.Error() + " " + buf.String())
return err
} }
// if response role is assistant and contains delta, append the content to streamResponseText // if response role is assistant and contains delta, append the content to streamResponseText
if chatResponse.Role == "assistant" && chatResponse.Detail != nil { if chatResponse.Role == "assistant" && chatResponse.Detail != nil {
for _, choice := range chatResponse.Detail.Choices { for _, choice := range chatResponse.Detail.Choices {
log.Print(choice.Delta.Content)
streamResponseText += choice.Delta.Content streamResponseText += choice.Delta.Content
} }
} }
} }
}()
} }
} }