Skip to content

Commit

Permalink
extract cooldown duration and set it to 5m
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jul 23, 2024
1 parent 0ea07f2 commit c2fafcc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/bot/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type OpenAI struct {
lastDT time.Time
}

const cooldownDuration = 5 * time.Minute

// NewOpenAI makes a bot for ChatGPT
func NewOpenAI(params Params, httpClient *http.Client, superUser bot.SuperUser) *OpenAI {
log.Printf("[INFO] OpenAI bot with github.com/sashabaranov/go-openai, Prompt=%s, max=%d. Auto response is %v",
Expand Down Expand Up @@ -125,7 +127,7 @@ func (o *OpenAI) OnMessage(msg bot.Message) (response bot.Response) {
}

log.Printf("[DEBUG] next request to ChatGPT can be made after %s, in %d minutes",
o.lastDT.Add(30*time.Minute), int(30-time.Since(o.lastDT).Minutes()))
o.lastDT.Add(cooldownDuration), int(cooldownDuration.Minutes()-time.Since(o.lastDT).Minutes()))
return bot.Response{
Text: responseAI,
Send: true,
Expand Down Expand Up @@ -158,10 +160,10 @@ func (o *OpenAI) checkRequest(msg bot.Message, text string) (ok bool, banMessage
return false, fmt.Sprintf("%s\n%s получает бан на 1 час.", reason, username)
}

if o.nowFn().Sub(o.lastDT) < 30*time.Minute {
if o.nowFn().Sub(o.lastDT) < cooldownDuration {
log.Printf("[WARN] OpenAI bot is too busy, last request was %s ago, %s banned", time.Since(o.lastDT), username)
reason := fmt.Sprintf("Слишком много запросов, следующий запрос можно будет сделать через %d минут.",
int(30-time.Since(o.lastDT).Minutes()))
int(cooldownDuration.Minutes()-time.Since(o.lastDT).Minutes()))

return false, fmt.Sprintf("%s\n%s получает бан на 1 час.", reason, username)
}
Expand Down

0 comments on commit c2fafcc

Please sign in to comment.