Implemented ratelimit
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using OpenAI;
|
using OpenAI;
|
||||||
using OpenAI.Chat;
|
using OpenAI.Chat;
|
||||||
using System.ClientModel;
|
using System.ClientModel;
|
||||||
|
using System.Collections;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
@@ -23,6 +24,21 @@ string nemesisPrompt = File.ReadAllText($"prompt/{GetEnv("NEMESIS_PROMPT_FILE")}
|
|||||||
|
|
||||||
Dictionary<long, List<ChatMessage>> oaiChats = new();
|
Dictionary<long, List<ChatMessage>> oaiChats = new();
|
||||||
|
|
||||||
|
//Ratelimit
|
||||||
|
TimeSpan rateLimit = new(0, 0, 0, 30);
|
||||||
|
Dictionary<long, DateTime> lastMessage = new();
|
||||||
|
|
||||||
|
bool IsRateLimited(long chatId) {
|
||||||
|
if (lastMessage.ContainsKey(chatId)) {
|
||||||
|
if (DateTime.Now - lastMessage[chatId] < rateLimit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastMessage[chatId] = DateTime.Now;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var options = new OpenAIClientOptions() {
|
var options = new OpenAIClientOptions() {
|
||||||
Endpoint = new(baseUrl),
|
Endpoint = new(baseUrl),
|
||||||
NetworkTimeout = new TimeSpan(0, 0, 30)
|
NetworkTimeout = new TimeSpan(0, 0, 30)
|
||||||
@@ -52,12 +68,18 @@ async Task OnMessage(Message msg, UpdateType type)
|
|||||||
if (msg.Text!.Contains(me.Result.FirstName!, StringComparison.OrdinalIgnoreCase) ||
|
if (msg.Text!.Contains(me.Result.FirstName!, StringComparison.OrdinalIgnoreCase) ||
|
||||||
msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id ||
|
msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id ||
|
||||||
msg.Chat.Type == ChatType.Private) {
|
msg.Chat.Type == ChatType.Private) {
|
||||||
|
var chatid = msg.Chat.Id;
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
$"""
|
$"""
|
||||||
Received message from {msg.Chat.Id} Type: {type}
|
Received message from {chatid} Type: {type}
|
||||||
Message: {msg.Text}
|
Message: {msg.Text}
|
||||||
""");
|
""");
|
||||||
var chatid = msg.Chat.Id;
|
//Check if the chat (group) is rate limited
|
||||||
|
if (IsRateLimited(chatid)) {
|
||||||
|
Console.WriteLine("No response due to ratelimit.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Check if the message is a reset command
|
//Check if the message is a reset command
|
||||||
if (msg.Text.StartsWith("/reset")) {
|
if (msg.Text.StartsWith("/reset")) {
|
||||||
ResetChat(chatid);
|
ResetChat(chatid);
|
||||||
@@ -106,10 +128,7 @@ void AddChatToDictionary(long id) {
|
|||||||
|
|
||||||
void ChatMessageRotate(long chatId, ChatMessage message){
|
void ChatMessageRotate(long chatId, ChatMessage message){
|
||||||
//Remove the first message from the chat if the chat has more than 5 couples of messages (0 is our prompt)
|
//Remove the first message from the chat if the chat has more than 5 couples of messages (0 is our prompt)
|
||||||
if (oaiChats[chatId].Count > 10) {
|
if (oaiChats[chatId].Count > 10) oaiChats[chatId].RemoveRange(1, 2);
|
||||||
oaiChats[chatId].RemoveAt(1);
|
|
||||||
oaiChats[chatId].RemoveAt(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Add the new message to the chat
|
//Add the new message to the chat
|
||||||
oaiChats[chatId].Add(message);
|
oaiChats[chatId].Add(message);
|
||||||
|
|||||||
Reference in New Issue
Block a user