They can now answer

This commit is contained in:
Samuele Lorefice
2024-12-26 20:19:59 +01:00
parent 124a4c66fe
commit 773203127f
6 changed files with 79 additions and 46 deletions

View File

@@ -1,13 +1,15 @@
using OpenAI.Chat;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;using TelegramBot;
using Telegram.Bot.Types.Enums;
using TelegramBot;
using File = System.IO.File;
string baseUrl = Env.Get("OPENAI_BASE_URL");
string apiKey = Env.Get("OPENAI_API_KEY");
string model = Env.Get("OPENAI_MODEL");
var oaiAgent = new OpenAiAgent(baseUrl, apiKey, model);
string baseUrl = Env.Get("OPENAI_BASE_URL");
string apiKey = Env.Get("OPENAI_API_KEY");
string model = Env.Get("OPENAI_MODEL");
var oaiAgent = new OpenAiAgent(baseUrl, apiKey, model);
oaiAgent.ContextSize = Int32.Parse(Env.Get("CONTEXT_SIZE"));
Console.WriteLine("Starting the bot...");
@@ -43,7 +45,9 @@ Agent Nemesis = new(
nemProfile.Result.Id,
nemProfile.Result.FirstName,
nemProfile.Result.Username!,
nemesisBot
nemesisBot,
nemesisPrompt,
oaiAgent.GetTokenLenght(nemesisPrompt)
);
var kroProfile = krolikBot.GetMe();
@@ -52,7 +56,9 @@ Agent Krolik = new(
kroProfile.Result.Id,
kroProfile.Result.FirstName,
kroProfile.Result.Username!,
krolikBot
krolikBot,
krolikPrompt,
oaiAgent.GetTokenLenght(krolikPrompt)
);
nemesisBot.OnMessage += OnNemMessage;
@@ -72,14 +78,14 @@ krocts.Cancel(); // stop krobot
async Task OnNemMessage(Message msg, UpdateType type) {
//Discard any message that is not a text message
if (msg.Type != MessageType.Text) return;
await OnMessage(msg, Nemesis);
}
async Task OnKroMessage(Message msg, UpdateType type) {
//Discard any message that is not a text message
if (msg.Type != MessageType.Text) return;
await OnMessage(msg, Krolik);
}
@@ -87,45 +93,46 @@ async Task OnKroMessage(Message msg, UpdateType type) {
// We should also take in account the last x messages in groups to add more context
async Task OnMessage(Message msg, Agent agent) {
//Check if the message contains the bot's username or a reply to a message sent by the bot or a private chat
var chatid = msg.Chat.Id;
var tokenlenght = oaiAgent.GetTokenLenght(msg.Text);
Console.WriteLine(
$"""
{agent.Name} has received message from {chatid} TokenLenght: {tokenlenght}
Message: {msg.Text}
""");
//Add the message to the chat history
oaiAgent.ChatHistoryAppend(Actor.User, chatid, "User: "+msg.Text);
//Check if the message is a reset command
if (msg.Text == "/reset" || msg.Text == "/reset@"+agent.Username) {
oaiAgent.ResetChat(chatid);
await agent.Bot.SendMessage(chatid, "Chat context has been reset");
return;
}
// Otherwise process it normally
var chatid = msg.Chat.Id;
var tokenlenght = oaiAgent.GetTokenLenght(msg.Text);
Console.WriteLine(
$"""
{agent.Name} has received message from {chatid} TokenLenght: {tokenlenght}
Message: {msg.Text}
""");
//Add the message to the chat history
oaiAgent.ChatHistoryAppend(Actor.User, chatid, "User: " + msg.Text);
//Check if the message is a reset command
if (msg.Text == "/reset" || msg.Text == "/reset@" + agent.Username) {
oaiAgent.ResetChat(chatid);
await agent.Bot.SendMessage(chatid, "Chat context has been reset");
return;
}
// Otherwise process it normally
if (msg.Text!.Contains(agent.Name, StringComparison.OrdinalIgnoreCase) ||
msg.ReplyToMessage?.From?.Id == agent.TelegramId || msg.Chat.Type == ChatType.Private) {
msg.ReplyToMessage?.From?.Id == agent.TelegramId ||
msg.Chat.Type == ChatType.Private) {
//Check if the chat (group) is rate limited
if (IsRateLimited(chatid)) {
/*if (IsRateLimited(chatid)) {
Console.WriteLine("No response due to ratelimit.");
return;
}
await AnswerChat(chatid, msg.Text, tokenlenght, agent.Actor);
*/
await AnswerChat(chatid, agent);
}
}
async Task AnswerChat(long chatId, string input, int tokenLenght, Actor actor) {
//Limit the message to 1024 characters to avoid out of context jump
string text = input;
if (input.Length > 1024) text = input.Substring(0, 1024);
async Task AnswerChat(long chatId, Agent agent) {
//Get the response from the OpenAI API
var result = oaiAgent.GetChatResponse(chatId, actor);
var result = oaiAgent.GetChatResponse(chatId, agent);
Console.WriteLine(
$"""
{agent.Name} has responded with: {result}
""");
//Send the response to the user
await krolikBot.SendMessage(chatId, result);
await agent.Bot.SendMessage(chatId, result);
}