mirror of
https://github.com/coaidev/coai.git
synced 2025-06-03 04:10:21 +09:00
fix sse break line error
This commit is contained in:
parent
23ca9d2ef6
commit
e9f107b561
@ -4,6 +4,7 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
"chat/globals"
|
"chat/globals"
|
||||||
"chat/utils"
|
"chat/utils"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"strings"
|
"strings"
|
||||||
@ -36,7 +37,7 @@ func (c *ChatInstance) GetChatBody(props *ChatProps, stream bool) interface{} {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ChatInstance) ProcessLine(data string) string {
|
func (c *ChatInstance) ProcessLine(buf, data string) (string, error) {
|
||||||
rep := strings.NewReplacer(
|
rep := strings.NewReplacer(
|
||||||
"data: {",
|
"data: {",
|
||||||
"\"data\": {",
|
"\"data\": {",
|
||||||
@ -50,24 +51,28 @@ func (c *ChatInstance) ProcessLine(data string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if item == "{data: [DONE]}" || item == "{data: [DONE]}}" || item == "{[DONE]}" {
|
if item == "{data: [DONE]}" || item == "{data: [DONE]}}" || item == "{[DONE]}" {
|
||||||
return ""
|
return "", nil
|
||||||
} else if item == "{data:}" || item == "{data:}}" {
|
} else if item == "{data:}" || item == "{data:}}" {
|
||||||
return ""
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var form *ChatStreamResponse
|
var form *ChatStreamResponse
|
||||||
if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil {
|
if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil {
|
||||||
if form = utils.UnmarshalForm[ChatStreamResponse](item[:len(item)-1]); form == nil {
|
if form = utils.UnmarshalForm[ChatStreamResponse](item[:len(item)-1]); form == nil {
|
||||||
|
if len(buf) > 0 {
|
||||||
|
return c.ProcessLine("", buf+item)
|
||||||
|
}
|
||||||
|
|
||||||
globals.Warn(fmt.Sprintf("chatgpt error: cannot parse response: %s", item))
|
globals.Warn(fmt.Sprintf("chatgpt error: cannot parse response: %s", item))
|
||||||
return ""
|
return data, errors.New("cannot parse response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(form.Data.Choices) == 0 {
|
if len(form.Data.Choices) == 0 {
|
||||||
return ""
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return form.Data.Choices[0].Delta.Content
|
return form.Data.Choices[0].Delta.Content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateChatRequest is the native http request body for chatgpt
|
// CreateChatRequest is the native http request body for chatgpt
|
||||||
@ -93,13 +98,24 @@ func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
|
|||||||
|
|
||||||
// CreateStreamChatRequest is the stream response body for chatgpt
|
// CreateStreamChatRequest is the stream response body for chatgpt
|
||||||
func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, callback globals.Hook) error {
|
func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, callback globals.Hook) error {
|
||||||
|
buf := ""
|
||||||
|
|
||||||
return utils.EventSource(
|
return utils.EventSource(
|
||||||
"POST",
|
"POST",
|
||||||
c.GetChatEndpoint(),
|
c.GetChatEndpoint(),
|
||||||
c.GetHeader(),
|
c.GetHeader(),
|
||||||
c.GetChatBody(props, true),
|
c.GetChatBody(props, true),
|
||||||
func(data string) error {
|
func(data string) error {
|
||||||
if data := c.ProcessLine(data); data != "" {
|
data, err := c.ProcessLine(buf, data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// error when break line
|
||||||
|
buf = buf + data
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = ""
|
||||||
|
if data != "" {
|
||||||
if err := callback(data); err != nil {
|
if err := callback(data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user