feat: slow down message updates to avoid rate limit
This commit is contained in:
parent
206c1326f6
commit
5e65a2ce32
16
assistant.go
16
assistant.go
|
@ -108,11 +108,13 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR
|
||||||
logger.Warnw("assistant: failed to get response", "error", err)
|
logger.Warnw("assistant: failed to get response", "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
nErrs := 0
|
var nErrs, nUpdates int
|
||||||
go func() {
|
go func() {
|
||||||
respBuilder := strings.Builder{}
|
respBuilder := strings.Builder{}
|
||||||
minWait := time.After(time.Second)
|
minWait := time.After(time.Second)
|
||||||
for {
|
for {
|
||||||
|
nUpdates += 1
|
||||||
|
|
||||||
var (
|
var (
|
||||||
nNewChunk int
|
nNewChunk int
|
||||||
finished bool
|
finished bool
|
||||||
|
@ -148,13 +150,17 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
respoText := respBuilder.String() + assistantWritingSign
|
// Renew the timer.
|
||||||
minWait = time.After(time.Second) // renew the timer
|
// The Telegram API rate limit for group messages is 20 per minute. So we cannot update messages too frequently.
|
||||||
|
minWaitDurSecs := lo.Min([]int{nUpdates, 4}) + nErrs*3
|
||||||
|
minWait = time.After(time.Duration(minWaitDurSecs) * time.Second)
|
||||||
|
|
||||||
if err := cb(respoText, false); err != nil {
|
// send the partial message
|
||||||
|
respText := respBuilder.String() + assistantWritingSign
|
||||||
|
if err := cb(respText, false); err != nil {
|
||||||
logger.Warnw("failed to send partial update", "error", err)
|
logger.Warnw("failed to send partial update", "error", err)
|
||||||
nErrs += 1
|
nErrs += 1
|
||||||
if nErrs > 3 {
|
if nErrs > 5 {
|
||||||
logger.Warnw("too many errors, aborting")
|
logger.Warnw("too many errors, aborting")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue