fix: scanner issue

This commit is contained in:
ckt1031 2023-07-15 23:05:43 +08:00
parent 0c175b4e44
commit 68abcd48ab

View File

@ -122,29 +122,29 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
var done = false var done = false
var streamResponseText = "" var streamResponseText = ""
scanner := bufio.NewScanner(resp.Body) if channel.Type != common.ChannelTypeChatGPTWeb {
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { scanner := bufio.NewScanner(resp.Body)
if atEOF && len(data) == 0 { scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := strings.Index(string(data), "\n"); i >= 0 {
return i + 2, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil return 0, nil, nil
} })
for scanner.Scan() {
data := scanner.Text()
if len(data) < 6 { // must be something wrong!
continue
}
if i := strings.Index(string(data), "\n\n"); i >= 0 {
return i + 2, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
})
for scanner.Scan() {
data := scanner.Text()
if len(data) < 6 { // must be something wrong!
common.SysError("invalid stream response: " + data)
continue
}
if channel.Type != common.ChannelTypeChatGPTWeb {
// If data has event: event content inside, remove it, it can be prefix or inside the data // If data has event: event content inside, remove it, it can be prefix or inside the data
if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") { if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") {
// Remove event: event in the front or back // Remove event: event in the front or back
@ -185,27 +185,28 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
done = true done = true
break break
} }
} else if channel.Type == common.ChannelTypeChatGPTWeb { }
scanner := bufio.NewScanner(resp.Body) } else if channel.Type == common.ChannelTypeChatGPTWeb {
go func() { scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
var chatResponse ChatGptWebChatResponse
err = json.Unmarshal(scanner.Bytes(), &chatResponse)
if err != nil { go func() {
log.Println("error unmarshal chat response: " + err.Error()) for scanner.Scan() {
continue var chatResponse ChatGptWebChatResponse
} err = json.Unmarshal(scanner.Bytes(), &chatResponse)
// if response role is assistant and contains delta, append the content to streamResponseText if err != nil {
if chatResponse.Role == "assistant" && chatResponse.Detail != nil { log.Println("error unmarshal chat response: " + err.Error())
for _, choice := range chatResponse.Detail.Choices { continue
streamResponseText += choice.Delta.Content }
}
// 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 {
streamResponseText += choice.Delta.Content
} }
} }
}() }
} }()
} }
defer resp.Body.Close() defer resp.Body.Close()