Skip to content

Commit

Permalink
Shared group chat AI rate limiter to reduce group chat noise
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Feb 23, 2024
1 parent 70059a2 commit 5e4c4ce
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 45 deletions.
52 changes: 36 additions & 16 deletions BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotNet.Commands;
using BotNet.CommandHandlers.AI.RateLimit;
using BotNet.Commands;
using BotNet.Commands.AI.Gemini;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;
Expand Down Expand Up @@ -34,27 +35,46 @@ ILogger<GeminiTextPromptHandler> logger
private readonly ILogger<GeminiTextPromptHandler> _logger = logger;

public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationToken) {
try {
if (textPrompt.Command.Chat is HomeGroupChat
|| textPrompt.Command.Sender is VIPSender) {
VIP_CHAT_RATE_LIMITER.ValidateActionRate(
if (textPrompt.Command.Chat is GroupChat) {
try {
AIRateLimiters.GROUP_CHAT_RATE_LIMITER.ValidateActionRate(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
);
} else {
CHAT_RATE_LIMITER.ValidateActionRate(
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown} atau lanjutkan di private chat.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl("Private chat 💬", "t.me/TeknumBot")
),
cancellationToken: cancellationToken
);
}
} else {
try {
if (textPrompt.Command.Sender is VIPSender) {
VIP_CHAT_RATE_LIMITER.ValidateActionRate(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
);
} else {
CHAT_RATE_LIMITER.ValidateActionRate(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
);
}
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
cancellationToken: cancellationToken
);
}
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
cancellationToken: cancellationToken
);
}

// Fire and forget
Expand Down
51 changes: 36 additions & 15 deletions BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotNet.Commands;
using BotNet.CommandHandlers.AI.RateLimit;
using BotNet.Commands;
using BotNet.Commands.AI.OpenAI;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;
Expand Down Expand Up @@ -30,20 +31,40 @@ ILogger<AskCommandHandler> logger
private readonly ILogger<AskCommandHandler> _logger = logger;

public async Task Handle(AskCommand askCommand, CancellationToken cancellationToken) {
try {
OpenAITextPromptHandler.CHAT_RATE_LIMITER.ValidateActionRate(
chatId: askCommand.Command.Chat.Id,
userId: askCommand.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
chatId: askCommand.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: askCommand.Command.MessageId,
cancellationToken: cancellationToken
);
return;
if (askCommand.Command.Chat is GroupChat) {
try {
AIRateLimiters.GROUP_CHAT_RATE_LIMITER.ValidateActionRate(
chatId: askCommand.Command.Chat.Id,
userId: askCommand.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
chatId: askCommand.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown} atau lanjutkan di private chat.</code>",
parseMode: ParseMode.Html,
replyToMessageId: askCommand.Command.MessageId,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl("Private chat 💬", "t.me/TeknumBot")
),
cancellationToken: cancellationToken
);
}
} else {
try {
OpenAITextPromptHandler.CHAT_RATE_LIMITER.ValidateActionRate(
chatId: askCommand.Command.Chat.Id,
userId: askCommand.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
chatId: askCommand.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: askCommand.Command.MessageId,
cancellationToken: cancellationToken
);
return;
}
}

// Fire and forget
Expand Down
49 changes: 35 additions & 14 deletions BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotNet.CommandHandlers.Art;
using BotNet.CommandHandlers.AI.RateLimit;
using BotNet.CommandHandlers.Art;
using BotNet.Commands;
using BotNet.Commands.AI.OpenAI;
using BotNet.Commands.AI.Stability;
Expand Down Expand Up @@ -36,19 +37,39 @@ ILogger<OpenAITextPromptHandler> logger
private readonly ILogger<OpenAITextPromptHandler> _logger = logger;

public Task Handle(OpenAITextPrompt textPrompt, CancellationToken cancellationToken) {
try {
CHAT_RATE_LIMITER.ValidateActionRate(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
cancellationToken: cancellationToken
);
if (textPrompt.Command.Chat is GroupChat) {
try {
AIRateLimiters.GROUP_CHAT_RATE_LIMITER.ValidateActionRate(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown} atau lanjutkan di private chat.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl("Private chat 💬", "t.me/TeknumBot")
),
cancellationToken: cancellationToken
);
}
} else {
try {
CHAT_RATE_LIMITER.ValidateActionRate(
chatId: textPrompt.Command.Chat.Id,
userId: textPrompt.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
cancellationToken: cancellationToken
);
}
}

// Fire and forget
Expand Down
7 changes: 7 additions & 0 deletions BotNet.CommandHandlers/AI/RateLimit/AIRateLimiters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using BotNet.Services.RateLimit;

namespace BotNet.CommandHandlers.AI.RateLimit {
internal static class AIRateLimiters {
internal static readonly RateLimiter GROUP_CHAT_RATE_LIMITER = RateLimiter.PerUserPerChat(4, TimeSpan.FromMinutes(15));
}
}

0 comments on commit 5e4c4ce

Please sign in to comment.