stream.Recv()会导致cpu 100的问题,升级gpt-3依赖库

This commit is contained in:
cookeem
2023-02-13 11:05:52 +08:00
parent f09b7c8d95
commit d7dfa7b216
4 changed files with 42 additions and 63 deletions

View File

@@ -96,7 +96,6 @@ func (api *Api) wsPingMsg(conn *websocket.Conn, chClose, chIsCloseSet chan int)
func (api *Api) GetChatMessage(conn *websocket.Conn, cli *gogpt.Client, mutex *sync.Mutex, requestMsg string) {
var err error
var strResp string
var end bool
req := gogpt.CompletionRequest{
Model: gogpt.GPT3TextDavinci003,
MaxTokens: api.Config.MaxLength,
@@ -109,23 +108,7 @@ func (api *Api) GetChatMessage(conn *websocket.Conn, cli *gogpt.Client, mutex *s
PresencePenalty: 0.1,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(api.Config.TimeoutSeconds))
defer func() {
if !end {
err = fmt.Errorf("[ERROR] context timeout")
chatMsg := Message{
Kind: "error",
Msg: err.Error(),
MsgId: uuid.New().String(),
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
}
mutex.Lock()
_ = conn.WriteJSON(chatMsg)
mutex.Unlock()
api.Logger.LogError(err.Error())
}
cancel()
}()
ctx := context.Background()
stream, err := cli.CreateCompletionStream(ctx, req)
if err != nil {
@@ -142,53 +125,53 @@ func (api *Api) GetChatMessage(conn *websocket.Conn, cli *gogpt.Client, mutex *s
api.Logger.LogError(err.Error())
return
}
defer stream.Close()
defer func() {
stream.Close()
}()
id := uuid.New().String()
var i int
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
end = true
var s string
var kind string
if i == 0 {
s = "[ERROR] NO RESPONSE, PLEASE RETRY"
kind = "retry"
if err != nil {
if errors.Is(err, io.EOF) {
var s string
var kind string
if i == 0 {
s = "[ERROR] NO RESPONSE, PLEASE RETRY"
kind = "retry"
} else {
s = "\n\n###### [END] ######"
kind = "chat"
}
chatMsg := Message{
Kind: kind,
Msg: s,
MsgId: id,
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
}
mutex.Lock()
_ = conn.WriteJSON(chatMsg)
mutex.Unlock()
if kind == "retry" {
api.Logger.LogError(s)
}
break
} else {
s = "\n\n###### [END] ######"
kind = "chat"
err = fmt.Errorf("[ERROR] receive chatGPT stream error: %s", err.Error())
chatMsg := Message{
Kind: "error",
Msg: err.Error(),
MsgId: uuid.New().String(),
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
}
mutex.Lock()
_ = conn.WriteJSON(chatMsg)
mutex.Unlock()
api.Logger.LogError(err.Error())
break
}
chatMsg := Message{
Kind: kind,
Msg: s,
MsgId: id,
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
}
mutex.Lock()
_ = conn.WriteJSON(chatMsg)
mutex.Unlock()
if kind == "retry" {
api.Logger.LogError(s)
}
break
} else if err != nil {
end = true
err = fmt.Errorf("[ERROR] receive chatGPT stream error: %s", err.Error())
chatMsg := Message{
Kind: "error",
Msg: err.Error(),
MsgId: id,
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
}
mutex.Lock()
_ = conn.WriteJSON(chatMsg)
mutex.Unlock()
api.Logger.LogError(err.Error())
break
}
if len(response.Choices) > 0 {
} else if len(response.Choices) > 0 {
var s string
if i == 0 {
s = fmt.Sprintf(`%s# %s`, s, requestMsg)

View File

@@ -6,5 +6,4 @@ type Config struct {
IntervalSeconds int `yaml:"intervalSeconds" json:"intervalSeconds" bson:"intervalSeconds" validate:"required"`
MaxLength int `yaml:"maxLength" json:"maxLength" bson:"maxLength" validate:"required"`
Cors bool `yaml:"cors" json:"cors" bson:"cors" validate:""`
TimeoutSeconds int `yaml:"timeoutSeconds" json:"timeoutSeconds" bson:"timeoutSeconds" validate:"required"`
}

View File

@@ -8,6 +8,3 @@ intervalSeconds: 5
maxLength: 2000
# 是否允许cors跨域
cors: true
# 问题反馈的超时时间,单位:秒
timeoutSeconds: 180

2
go.mod
View File

@@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.8.2
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/sashabaranov/go-gpt3 v1.0.0
github.com/sashabaranov/go-gpt3 v1.0.1
github.com/sirupsen/logrus v1.9.0
gopkg.in/yaml.v3 v3.0.1
)