Skip to content

Commit

Permalink
Merge pull request #76 from teknologi-umum/ai-overhaul
Browse files Browse the repository at this point in the history
Stricter rate limiting
  • Loading branch information
ronnygunawan authored Dec 3, 2023
2 parents a1f2efb + 0da6cd9 commit 9f3b54d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BotNet.Services.MarkdownV2;
Expand Down Expand Up @@ -765,7 +766,8 @@ await botClient.SendTextMessageAsync(
}
}

private static readonly RateLimiter IMAGE_GENERATION_RATE_LIMITER = RateLimiter.PerUser(1, TimeSpan.FromMinutes(3));
private static readonly RateLimiter IMAGE_GENERATION_PER_USER_RATE_LIMITER = RateLimiter.PerUser(1, TimeSpan.FromMinutes(10));
private static readonly RateLimiter IMAGE_GENERATION_PER_CHAT_RATE_LIMITER = RateLimiter.PerUser(2, TimeSpan.FromMinutes(3));
public static async Task StreamChatWithFriendlyBotAsync(
ITelegramBotClient botClient,
IServiceProvider serviceProvider,
Expand Down Expand Up @@ -829,7 +831,11 @@ await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
);
break;
case ChatIntent.ImageGeneration:
IMAGE_GENERATION_RATE_LIMITER.ValidateActionRate(
IMAGE_GENERATION_PER_USER_RATE_LIMITER.ValidateActionRate(
chatId: message.Chat.Id,
userId: message.From.Id
);
IMAGE_GENERATION_PER_CHAT_RATE_LIMITER.ValidateActionRate(
chatId: message.Chat.Id,
userId: message.From.Id
);
Expand Down Expand Up @@ -881,6 +887,13 @@ await botClient.SendTextMessageAsync(
),
cancellationToken: cancellationToken);
}
} catch (HttpRequestException exc) when (exc.StatusCode == HttpStatusCode.TooManyRequests) {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "<code>Too many requests.</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch (OperationCanceledException) {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
Expand Down

0 comments on commit 9f3b54d

Please sign in to comment.