Skip to content

Commit

Permalink
feat: support add timer to monitor answer slow (default: 3 * min)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Feb 6, 2024
1 parent 6f15daa commit bc9e9bb
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion commands/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,34 @@ func CreateMessageCommand(
return
}

// too long to answer
answerTimeout := time.AfterFunc(3*time.Minute, func() {
msgType, content, err := mc.
NewContent().
Post(&mc.ContentTypePost{
ZhCN: &mc.ContentTypePostBody{
Content: [][]mc.ContentTypePostBodyItem{
{
{
Tag: "text",
UnEscape: true,
Text: "(思考有点慢,可能是网络或者模型等原因,请稍等 ...)",
},
},
},
},
}).
Build()
if err != nil {
logger.Errorf("failed to build content: %v", err)
return
}
if err := reply(string(content), msgType); err != nil {
logger.Errorf("failed to reply: %v", err)
return
}
})

conversation, err := chatgptClient.GetOrCreateConversation(request.ChatID(), &chatgpt.ConversationConfig{
MaxMessages: 50,
Model: cfg.OpenAIModel,
Expand All @@ -122,7 +150,6 @@ func CreateMessageCommand(

var answer []byte
err = retry.Retry(func() error {

answer, err = conversation.Ask([]byte(question), &chatgpt.ConversationAskConfig{
ID: request.Event.Message.MessageID,
User: user.User.Name,
Expand Down Expand Up @@ -152,6 +179,7 @@ func CreateMessageCommand(
}
return
}
answerTimeout.Stop()

logger.Debugf("ChatGPT 答 %s:%s", user.User.Name, answer)

Expand Down

0 comments on commit bc9e9bb

Please sign in to comment.