Added rop of pending updates on bot start, reset command, AnswerChat method, GPU offload, limit to response lenght, context reduced to 2048, flash attention, 4 parallel decode queues, --keep of the original 810 tokens (which is the starting prompt)
This commit is contained in:
@@ -103,6 +103,7 @@ Console.WriteLine("OpenAI Chat Client created");
|
||||
|
||||
using var cts = new CancellationTokenSource();
|
||||
var bot = new TelegramBotClient(token, cancellationToken:cts.Token);
|
||||
await bot.DropPendingUpdates();
|
||||
var me = bot.GetMe();
|
||||
bot.OnMessage += OnMessage;
|
||||
Console.WriteLine("Bot running");
|
||||
@@ -123,22 +124,43 @@ async Task OnMessage(Message msg, UpdateType type)
|
||||
Message: {msg.Text}
|
||||
""");
|
||||
var chatid = msg.Chat.Id;
|
||||
//Check if the chat is already in the dictionary
|
||||
if (!oaiChats.ContainsKey(chatid))
|
||||
AddChatToDictionary(chatid);
|
||||
//Add the current message to the chat
|
||||
oaiChats[chatid].Add(new UserChatMessage(msg.Text));
|
||||
//fetch existing messages history
|
||||
var messages = oaiChats[chatid];
|
||||
//Fetch the response from the model
|
||||
var result = chatClient.CompleteChat(messages).Value.Content[0].Text;
|
||||
//Add the response to the chat
|
||||
oaiChats[chatid].Add(new AssistantChatMessage(result));
|
||||
//Send the response to the user
|
||||
await bot.SendMessage(chatid, result);
|
||||
//Check if the message is a reset command
|
||||
if (msg.Text.StartsWith("/reset")) {
|
||||
ResetChat(chatid);
|
||||
await bot.SendMessage(chatid, "Chat context has been reset");
|
||||
return;
|
||||
}
|
||||
// Otherwise process it normally
|
||||
await AnswerChat(chatid, msg.Text);
|
||||
}
|
||||
}
|
||||
|
||||
async Task AnswerChat(long chatId, string input) {
|
||||
//Check if the chat is already in the dictionary
|
||||
if (!oaiChats.ContainsKey(chatId))
|
||||
AddChatToDictionary(chatId);
|
||||
|
||||
string text = input;
|
||||
//Limit the message to 1024 characters to avoid out of context jump
|
||||
if (input.Length > 1024) text = input.Substring(0, 1024);
|
||||
|
||||
//Add the current message to the chat
|
||||
oaiChats[chatId].Add(new UserChatMessage(text));
|
||||
|
||||
//fetch existing messages history
|
||||
var messages = oaiChats[chatId];
|
||||
|
||||
//Fetch the response from the model
|
||||
var result = chatClient.CompleteChat(messages).Value.Content[0].Text;
|
||||
|
||||
//Add the response to the chat
|
||||
Console.WriteLine("Replying with: " + result);
|
||||
oaiChats[chatId].Add(new AssistantChatMessage(result));
|
||||
|
||||
//Send the response to the user
|
||||
await bot.SendMessage(chatId, result);
|
||||
}
|
||||
|
||||
void AddChatToDictionary(long id) {
|
||||
//Create a new chat object
|
||||
var chat = new List<ChatMessage>();
|
||||
@@ -146,3 +168,10 @@ void AddChatToDictionary(long id) {
|
||||
//add the entry to the dictionary
|
||||
oaiChats.Add(id, chat);
|
||||
}
|
||||
|
||||
void ResetChat(long chatId) {
|
||||
//Remove the chat from the dictionary
|
||||
oaiChats.Remove(chatId);
|
||||
//Add the chat back to the dictionary
|
||||
AddChatToDictionary(chatId);
|
||||
}
|
||||
Reference in New Issue
Block a user