fix: improved checking for chatgptweb
This commit is contained in:
parent
5b8a826cf9
commit
4e31c3991d
@ -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() {
|
||||||
|
var chatResponse ChatGptWebChatResponse
|
||||||
|
err = json.Unmarshal(scanner.Bytes(), &chatResponse)
|
||||||
|
|
||||||
// remove all spaces and newlines outside of json objects
|
if err != nil {
|
||||||
jsonObjs := strings.Split(data, "\n") // Split the data into multiple JSON objects
|
log.Println("error unmarshal chat response: " + err.Error())
|
||||||
for _, jsonObj := range jsonObjs {
|
continue
|
||||||
if jsonObj == "" {
|
}
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var chatResponse ChatGptWebChatResponse
|
// if response role is assistant and contains delta, append the content to streamResponseText
|
||||||
err = json.Unmarshal([]byte(jsonObj), &chatResponse)
|
if chatResponse.Role == "assistant" && chatResponse.Detail != nil {
|
||||||
if err != nil {
|
for _, choice := range chatResponse.Detail.Choices {
|
||||||
// Print the body in string
|
streamResponseText += choice.Delta.Content
|
||||||
buf := new(bytes.Buffer)
|
}
|
||||||
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 chatResponse.Role == "assistant" && chatResponse.Detail != nil {
|
|
||||||
for _, choice := range chatResponse.Detail.Choices {
|
|
||||||
log.Print(choice.Delta.Content)
|
|
||||||
streamResponseText += choice.Delta.Content
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user