Skip to content

Commit

Permalink
Add global rate limit for image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 3, 2023
1 parent 0da6cd9 commit 01f0776
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,8 @@ await botClient.SendTextMessageAsync(
}

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));
private static readonly RateLimiter IMAGE_GENERATION_PER_CHAT_RATE_LIMITER = RateLimiter.PerChat(2, TimeSpan.FromMinutes(5));
private static readonly RateLimiter IMAGE_GENERATION_GLOBAL_RATE_LIMITER = RateLimiter.PerChat(1, TimeSpan.FromMinutes(1));
public static async Task StreamChatWithFriendlyBotAsync(
ITelegramBotClient botClient,
IServiceProvider serviceProvider,
Expand Down Expand Up @@ -839,6 +840,10 @@ await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
chatId: message.Chat.Id,
userId: message.From.Id
);
IMAGE_GENERATION_GLOBAL_RATE_LIMITER.ValidateActionRate(
chatId: 0,
userId: 0
);
Message busyMessage = await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "Generating image… ⏳",
Expand Down

0 comments on commit 01f0776

Please sign in to comment.