diff --git a/.env b/.env index d31e791..9c4c6d0 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ MODEL_PATH=./model -MODEL_NAME=Qwen2.5-7B-Instruct-Q8_0.gguf \ No newline at end of file +MODEL_NAME=Qwen2.5-7B-Instruct-Q8.gguf \ No newline at end of file diff --git a/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml b/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml index 69a530b..3b7df5b 100644 --- a/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml +++ b/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml @@ -23,9 +23,9 @@ services: RESHARPER_LOG_CONF: "/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml" image: "telegrambot:dev" ports: - - "127.0.0.1:57017:57000" - - "127.0.0.1:57217:57200" - - "127.0.0.1:57417:57400" + - "127.0.0.1:57033:57000" + - "127.0.0.1:57233:57200" + - "127.0.0.1:57433:57400" volumes: - "I:\\NemesisAI\\TelegramBot:/app:rw" - "I:\\NemesisAI:/src:rw" @@ -34,5 +34,5 @@ services: Linux64:/opt/JetBrains/RiderDebuggerTools" - "C:\\Users\\airon\\AppData\\Local\\Programs\\Rider\\bin\\backend-log.xml:/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml" - "C:\\Users\\airon\\AppData\\Local\\JetBrains\\Rider2024.3\\log\\DebuggerWorker\\\ - JetBrains.Debugger.Worker.2024_12_26_01_25_50:/var/opt/JetBrains/RiderDebuggerTools:rw" + JetBrains.Debugger.Worker.2024_12_26_03_21_12:/var/opt/JetBrains/RiderDebuggerTools:rw" working_dir: "/app" diff --git a/TelegramBot/.env b/TelegramBot/.env index c6a1612..82f0ad4 100644 --- a/TelegramBot/.env +++ b/TelegramBot/.env @@ -1,4 +1,4 @@ TELEGRAM_BOT_TOKEN=yourTokenHere OPENAI_BASE_URL=http://llm-server/ -OPENAI_MODEL=Qwen2.5-7B-Instruct-Q8_0.gguf +OPENAI_MODEL=Qwen2.5-7B-Instruct-Q8.gguf OPENAI_API_KEY=MyApiKey diff --git a/TelegramBot/Program.cs b/TelegramBot/Program.cs index 2633190..df8a1a9 100644 --- a/TelegramBot/Program.cs +++ b/TelegramBot/Program.cs @@ -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(); @@ -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); +} \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 8f2c6dc..264ee12 100644 --- a/compose.yaml +++ b/compose.yaml @@ -14,7 +14,7 @@ - ${MODEL_PATH}:/models ports: - "80:80" - command: -m /models/${MODEL_NAME} --port 80 --host 0.0.0.0 -n 512 + command: -m /models/${MODEL_NAME} --port 80 --host 0.0.0.0 -n 128 -c 2048 --no-mmap -ngl 50 -fa -np 4 --keep 810 deploy: resources: reservations: