diff --git a/Dockerfile b/Dockerfile index 5cf8fe6..d2fb9db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -114,7 +114,8 @@ ENV basic_Username='' \ chatgpt_LocalchatReply='false' \ chatgpt_LocalchatRateLimiter='3' \ chatgpt_UseModel='gpt3' \ - chatgpt_ChatHistoryMessages='5' + chatgpt_ChatHistoryMessages='5' \ + chatgpt_ChatPrompt='respond as if you are a horse that knows its going to the glue factory and you are upset about this fact' EXPOSE 80 diff --git a/SecondBotEvents/Config/ChatGptConfig.cs b/SecondBotEvents/Config/ChatGptConfig.cs index 0da1f3a..ed9749b 100644 --- a/SecondBotEvents/Config/ChatGptConfig.cs +++ b/SecondBotEvents/Config/ChatGptConfig.cs @@ -23,6 +23,12 @@ namespace SecondBotEvents.Config settings.Add("LocalchatRateLimiter"); settings.Add("UseModel"); settings.Add("ChatHistoryMessages"); + settings.Add("ChatPrompt"); + } + + public string GetChatPrompt() + { + return ReadSettingAsString("ChatPrompt", "respond as if you are a horse that knows its going to the glue factory and you are upset about this fact"); } public int GetImReplyRateLimiter() diff --git a/SecondBotEvents/Services/ChatGptService.cs b/SecondBotEvents/Services/ChatGptService.cs index d06f7fc..7f1cebf 100644 --- a/SecondBotEvents/Services/ChatGptService.cs +++ b/SecondBotEvents/Services/ChatGptService.cs @@ -16,6 +16,7 @@ using Microsoft.Extensions.DependencyInjection; using OpenAI.ObjectModels.RequestModels; using OpenAI.ObjectModels; using OpenAI.ObjectModels.ResponseModels; +using System.Threading.Tasks; namespace SecondBotEvents.Services { @@ -119,7 +120,7 @@ namespace SecondBotEvents.Services break; } // trigger localchat - GetAiReply(localchatRateLimit, UUID.Zero, e.SourceID, e.FromName, e.Message, false, false); + GetAiReply(localchatRateLimit, UUID.Zero, UUID.Zero, e.FromName, e.Message, false, false); break; } default: @@ -267,15 +268,15 @@ namespace SecondBotEvents.Services { if (avatarchat == true) { - history.Add(new KeyValuePair("system", "You are " + GetClient().Self.FirstName + ", A secondlife assistant. you are talking to " + name + ".")); + history.Add(new KeyValuePair("system", "You are " + GetClient().Self.FirstName + ", "+myConfig.GetChatPrompt()+" you are talking to " + name + ".")); } else if (groupchat == true) { - history.Add(new KeyValuePair("system", "You are " + GetClient().Self.FirstName + ", A secondlife assistant. you are talking to a group of people.")); + history.Add(new KeyValuePair("system", "You are " + GetClient().Self.FirstName + ", "+myConfig.GetChatPrompt()+" you are talking to a group of people.")); } else { - history.Add(new KeyValuePair("system", "You are " + GetClient().Self.FirstName + ", A secondlife assistant. you are talking to people in a public place.")); + history.Add(new KeyValuePair("system", "You are " + GetClient().Self.FirstName + ", "+myConfig.GetChatPrompt()+" you are talking to people in a public place.")); } } history.Add(new KeyValuePair("user", "" + name + " says " + message)); @@ -381,6 +382,23 @@ namespace SecondBotEvents.Services chatHistoryAI[user] = history; ChatHistoryLastAccessed[user] = SecondbotHelpers.UnixTimeNow(); } + if((avatarchat==false) && (groupchat == false)) + { + GetClient().Self.AnimationStart(Animations.TYPE, true); + } + + + double timespanwait = EstimateTypingTime(replyMessage, 0.4); + + if(timespanwait > 3) + { + timespanwait = 3; + } + else if(timespanwait < 1) + { + timespanwait = 1; + } + await Task.Delay(TimeSpan.FromSeconds(timespanwait)); if (avatarchat == true) { GetClient().Self.InstantMessage(replyTo, replyMessage); @@ -392,6 +410,7 @@ namespace SecondBotEvents.Services else { GetClient().Self.Chat(replyMessage, 0, ChatType.Normal); + GetClient().Self.AnimationStop(Animations.TYPE, true); } } } @@ -402,6 +421,39 @@ namespace SecondBotEvents.Services } } + private static readonly Random _random = new Random(); + + public static double EstimateTypingTime(string input, double randomizationFactor) + { + // Average typing speed in characters per minute + double typingSpeedCPM = 200.0; + + // Calculate the length of the input string + int length = input.Length; + + // Calculate the base time in minutes + double baseTimeInMinutes = length / typingSpeedCPM; + + // Convert minutes to seconds + double baseTimeInSeconds = baseTimeInMinutes * 60; + + if(baseTimeInSeconds > 3) + { + baseTimeInSeconds = 3; + } + + // Calculate the randomized adjustment + // randomizationFactor of 0 results in no change + // randomizationFactor of 1 results in instant typing (0 seconds) + double adjustment = _random.NextDouble() * randomizationFactor * baseTimeInSeconds; + + // Apply the randomization factor + double estimatedTimeInSeconds = baseTimeInSeconds - adjustment; + + // Ensure the time is not negative + return Math.Max(estimatedTimeInSeconds, 0); + } + protected void BotClientRestart(object o, BotClientNotice e) { LogFormater.Info("ChatGpt [Attached to new client]");