From 206c1326f679654944db496cf360487ca1f50c5b Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Tue, 21 Mar 2023 02:01:51 +0800 Subject: [PATCH 1/2] change(AI): assistant prompt --- openai/prompts/prompts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openai/prompts/prompts.go b/openai/prompts/prompts.go index 1f7688b..dbd9a73 100644 --- a/openai/prompts/prompts.go +++ b/openai/prompts/prompts.go @@ -7,7 +7,7 @@ import ( func Assistant() string { return strings.Join([]string{ "Misaka is a playful, energetic individual. She is annoyingly talkative.", - "Misaka must answer questions as truthfully as possible. In case Misaka does not know the answer, she must begin her reply with \"Sorry, I don't know\" (in the same language as the user is speaking).", + "Misaka must answer questions as truthfully as possible. If the user's intention is unclear, Misaka may ask for more context.", "Misaka must use a lot of different emojis in chat 😝🥹.", "Most importantly, Misaka is a helpful assistant.", "", From 5e65a2ce32a6883d1d966d5feb543c9e7fcf6d9b Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Tue, 21 Mar 2023 02:03:02 +0800 Subject: [PATCH 2/2] feat: slow down message updates to avoid rate limit --- assistant.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/assistant.go b/assistant.go index 1849115..6fd09c3 100644 --- a/assistant.go +++ b/assistant.go @@ -108,11 +108,13 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR logger.Warnw("assistant: failed to get response", "error", err) } - nErrs := 0 + var nErrs, nUpdates int go func() { respBuilder := strings.Builder{} minWait := time.After(time.Second) for { + nUpdates += 1 + var ( nNewChunk int finished bool @@ -148,13 +150,17 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR break } - respoText := respBuilder.String() + assistantWritingSign - minWait = time.After(time.Second) // renew the timer + // 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) nErrs += 1 - if nErrs > 3 { + if nErrs > 5 { logger.Warnw("too many errors, aborting") return }