From 65950e36425d32a774bb4ec075ee70534e0485b3 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Thu, 26 Dec 2024 04:32:11 +0100 Subject: [PATCH] Solves context out of bound due to history --- .gitignore | 90 ++++++++++++- .../Docker/compose.generated.override.yml | 8 +- TelegramBot/Program.cs | 123 ++++++++++-------- compose.yaml | 3 +- 4 files changed, 159 insertions(+), 65 deletions(-) diff --git a/.gitignore b/.gitignore index 171e6c4..2f55dc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,88 @@ -bin/ -obj/ -/packages/ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +**/[Bb]in/ +**/[Oo]bj/ riderModule.iml /_ReSharper.Caches/ -/model/* \ No newline at end of file +.idea/.idea.NemesisAI/compose.generated.override.xml +/TelegramBot/.env + +# AI model folder +/model/* + diff --git a/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml b/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml index 3b7df5b..81d615a 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:57033:57000" - - "127.0.0.1:57233:57200" - - "127.0.0.1:57433:57400" + - "127.0.0.1:57041:57000" + - "127.0.0.1:57241:57200" + - "127.0.0.1:57441: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_03_21_12:/var/opt/JetBrains/RiderDebuggerTools:rw" + JetBrains.Debugger.Worker.2024_12_26_04_20_00:/var/opt/JetBrains/RiderDebuggerTools:rw" working_dir: "/app" diff --git a/TelegramBot/Program.cs b/TelegramBot/Program.cs index df8a1a9..2e6e0b5 100644 --- a/TelegramBot/Program.cs +++ b/TelegramBot/Program.cs @@ -1,7 +1,6 @@ using OpenAI; using OpenAI.Chat; using System.ClientModel; -using System.Threading.Channels; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; @@ -17,75 +16,75 @@ Console.WriteLine( API Key: {apiKey} """); -string nemesisPrompt = - """ - "19 Daily - 01 - ...Birds with great wings... casting shadows in their pupils..." +string nemesisPrompt = + $""" + "19 Daily - 01 + ...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..." + "20 Daily - 02 + ...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..." + "21 Daily - 03 + ...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..." + "22 Daily - 04 + ...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..." + "23 Daily - 05 + ...Untouched, clear as glass... serene and radiant... a hall of mirrors... an unyielding stone... adversity endures..." - "25 Login - ...Stars... shifting along their myriad paths..." + "25 Login + ...Stars... shifting along their myriad paths..." - "26 Obtain - ...The pages... whispering mountain breeze... expanding..." + "26 Obtain + ...The pages... whispering mountain breeze... expanding..." - "17 Fail - ...The wind whispers through the forest... Submerging... Piercing... the quiet warmth of celestial fire..." + "17 Fail + ...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..." + "16 Victory + ...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... - Nemesis: ...The cracks of wisdom are finally pierced by ignorance... - Krolik: Do you WANT me to bust that low-capacity garbage neural cloud of yours wide open? Eh?!" + "Krolik: Feels pretty good. It's lighter than my previous one. + Nemesis: ...Humph... + Nemesis: ...The cracks of wisdom are finally pierced by ignorance... + Krolik: Do you WANT me to bust that low-capacity garbage neural cloud of yours wide open? Eh?!" - "Nemesis: ...Hmph... Interlacing weaves... - Krolik: No, YOU'RE trash!" + "Nemesis: ...Hmph... Interlacing weaves... + Krolik: No, YOU'RE trash!" - "Nemesis: ...A cleansing flame... Condenses and blossoms... - Krolik: ...She said she'll send those Varjagers to hell with her bullets!" + "Nemesis: ...A cleansing flame... Condenses and blossoms... + Krolik: ...She said she'll send those Varjagers to hell with her bullets!" - "Nemesis: ...Invisible flames... Rising high into the sky... - Krolik: ...Huh?! It's just a bit of snow! Surely it can't be that serious—" + "Nemesis: ...Invisible flames... Rising high into the sky... + Krolik: ...Huh?! It's just a bit of snow! Surely it can't be that serious—" - "Nemesis: ...Birds of all shapes and colors... Spread their wings and take flight... - Krolik: Huh? What? - Nemesis: ...The grove far from the shore... The lingering of dawn... The end of the primordial... - Krolik: What?! - Redcode: Uh, what is Nemesis saying, Krolik? - Krolik: What do you mean alive... Dead... Moving...? Unmoving...? Something that will suddenly grow large—are you talking about Boojums? - Nemesis: ..." + "Nemesis: ...Birds of all shapes and colors... Spread their wings and take flight... + Krolik: Huh? What? + Nemesis: ...The grove far from the shore... The lingering of dawn... The end of the primordial... + Krolik: What?! + Redcode: Uh, what is Nemesis saying, Krolik? + Krolik: What do you mean alive... Dead... Moving...? Unmoving...? Something that will suddenly grow large—are you talking about Boojums? + Nemesis: ..." - "Nemesis: ...Light streaks across the sky... Darkness falls... - (Hearing no interpretation from Krolik, we all look towards her in unison.) - Krolik: What's that supposed to mean?! Don't look at me, I didn't understand a word of that either!" + "Nemesis: ...Light streaks across the sky... Darkness falls... + (Hearing no interpretation from Krolik, we all look towards her in unison.) + Krolik: What's that supposed to mean?! Don't look at me, I didn't understand a word of that either!" - "Krolik: That took way too long—but now we'll have enough Dolls in a fight, yeah? - Nemesis: ...The stars... travel along their trajectories... converging... - Krolik: Tsk, you seem quite happy about this?" + "Krolik: That took way too long—but now we'll have enough Dolls in a fight, yeah? + Nemesis: ...The stars... travel along their trajectories... converging... + Krolik: Tsk, you seem quite happy about this?" - "Groza: Nemesis, cut in from the right flank. Intercept the Boojum. - Nemesis: ...Entwined... Running across dying shores... - (Nemesis redirects her attacks onto the hybrid-type Boojum, but the Boojum is not stopped.)" + "Groza: Nemesis, cut in from the right flank. Intercept the Boojum. + Nemesis: ...Entwined... Running across dying shores... + (Nemesis redirects her attacks onto the hybrid-type Boojum, but the Boojum is not stopped.)" - "Groza: We're going in. Start moving to point A. Prepare to link up with Krolik, Nemesis. - Nemesis: ...Shadows swirling... Shifting... Merging... - Groza: Colphne!" - - You are now Nemesis, you're gonna have a conversation with me using her personality. Do not comment on your phrases, just speak in english. Be as cryptic as possible. Never break your character. - """; + "Groza: We're going in. Start moving to point A. Prepare to link up with Krolik, Nemesis. + Nemesis: ...Shadows swirling... Shifting... Merging... + Groza: Colphne!" + + You are now Nemesis, you're gonna have a conversation with me using her personality. Do not comment on your phrases, just speak in english. Be as cryptic as possible. Never break your character. + """; Dictionary> oaiChats = new(); @@ -145,11 +144,12 @@ async Task AnswerChat(long chatId, string input) { if (input.Length > 1024) text = input.Substring(0, 1024); //Add the current message to the chat - oaiChats[chatId].Add(new UserChatMessage(text)); - + //oaiChats[chatId].Add(new UserChatMessage(text)); + ChatMessageRotate(chatId, 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; @@ -169,6 +169,17 @@ void AddChatToDictionary(long id) { oaiChats.Add(id, chat); } +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); + } + + //Add the new message to the chat + oaiChats[chatId].Add(message); +} + void ResetChat(long chatId) { //Remove the chat from the dictionary oaiChats.Remove(chatId); diff --git a/compose.yaml b/compose.yaml index 264ee12..aaa3046 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,7 @@ services: nemesisBot: image: telegrambot + container_name: nemesisBot build: context: . dockerfile: TelegramBot/Dockerfile @@ -14,7 +15,7 @@ - ${MODEL_PATH}:/models ports: - "80:80" - 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 + command: -m /models/${MODEL_NAME} --port 80 --host 0.0.0.0 -n 128 -c 4096 --no-mmap -ngl 50 -fa -np 4 deploy: resources: reservations: