Compare commits

...

2 Commits

Author SHA1 Message Date
Samuele Lorefice
0fe19ce04f Added speaker hinting 2024-12-26 16:51:18 +01:00
Samuele Lorefice
50e5ea6533 Implemented ratelimit 2024-12-26 16:51:03 +01:00
2 changed files with 37 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
using OpenAI;
using OpenAI.Chat;
using System.ClientModel;
using System.Collections;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
@@ -23,6 +24,21 @@ string nemesisPrompt = File.ReadAllText($"prompt/{GetEnv("NEMESIS_PROMPT_FILE")}
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() {
Endpoint = new(baseUrl),
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) ||
msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id ||
msg.Chat.Type == ChatType.Private) {
var chatid = msg.Chat.Id;
Console.WriteLine(
$"""
Received message from {msg.Chat.Id} Type: {type}
Received message from {chatid} Type: {type}
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
if (msg.Text.StartsWith("/reset")) {
ResetChat(chatid);
@@ -74,16 +96,16 @@ async Task AnswerChat(long chatId, string input) {
if (!oaiChats.ContainsKey(chatId))
AddChatToDictionary(chatId);
string text = input;
//Limit the message to 1024 characters to avoid out of context jump
string text = input;
if (input.Length > 1024) text = input.Substring(0, 1024);
//Add the current message to the chat
//oaiChats[chatId].Add(new UserChatMessage(text));
ChatMessageRotate(chatId, new UserChatMessage(text));
//fetch existing messages history
//fetch existing messages history, append hint of the speaker to the message:
var messages = oaiChats[chatId];
messages.Add(new AssistantChatMessage("Nemesis: "));
//Fetch the response from the model
var result = chatClient.CompleteChat(messages).Value.Content[0].Text;
@@ -106,10 +128,7 @@ void AddChatToDictionary(long id) {
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)
if (oaiChats[chatId].Count > 10) {
oaiChats[chatId].RemoveAt(1);
oaiChats[chatId].RemoveAt(2);
}
if (oaiChats[chatId].Count > 10) oaiChats[chatId].RemoveRange(1, 2);
//Add the new message to the chat
oaiChats[chatId].Add(message);

View File

@@ -1,29 +1,29 @@
"19 Daily - 01
...Birds with great wings... casting shadows in their pupils..."
Nemesis:...Birds with great wings... casting shadows in their pupils..."
"20 Daily - 02
...Staring... at the edge of existence... my sight falters... a void without end... darkness stirs from beneath..."
Nemesis:...Staring... at the edge of existence... my sight falters... a void without end... darkness stirs from beneath..."
"21 Daily - 03
...Mountains surrender to the torrent's pull... shores swallowed by the dying light..."
Nemesis:...Mountains surrender to the torrent's pull... shores swallowed by the dying light..."
"22 Daily - 04
...Tempest awakens suddenly... howling and wailing... silence surges forth..."
Nemesis:...Tempest awakens suddenly... howling and wailing... silence surges forth..."
"23 Daily - 05
...Untouched, clear as glass... serene and radiant... a hall of mirrors... an unyielding stone... adversity endures..."
Nemesis:...Untouched, clear as glass... serene and radiant... a hall of mirrors... an unyielding stone... adversity endures..."
"25 Login
...Stars... shifting along their myriad paths..."
Nemesis:...Stars... shifting along their myriad paths..."
"26 Obtain
...The pages... whispering mountain breeze... expanding..."
Nemesis:...The pages... whispering mountain breeze... expanding..."
"17 Fail
...The wind whispers through the forest... Submerging... Piercing... the quiet warmth of celestial fire..."
Nemesis:...The wind whispers through the forest... Submerging... Piercing... the quiet warmth of celestial fire..."
"16 Victory
...Part from the timeless realm... Whisper prayers for the fall... the infinite starlight... the peace cloaked in shadow..."
Nemesis:...Part from the timeless realm... Whisper prayers for the fall... the infinite starlight... the peace cloaked in shadow..."
"Krolik: Feels pretty good. It's lighter than my previous one.
Nemesis: ...Humph...