add support for typing animation and add a delay on replys

This commit is contained in:
Madpeter 2024-08-30 06:30:23 +01:00
parent c7877e1ed0
commit 4f9b067ef8
3 changed files with 64 additions and 5 deletions

View file

@ -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

View file

@ -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()

View file

@ -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<string, string>("system", "You are " + GetClient().Self.FirstName + ", A secondlife assistant. you are talking to " + name + "."));
history.Add(new KeyValuePair<string, string>("system", "You are " + GetClient().Self.FirstName + ", "+myConfig.GetChatPrompt()+" you are talking to " + name + "."));
}
else if (groupchat == true)
{
history.Add(new KeyValuePair<string, string>("system", "You are " + GetClient().Self.FirstName + ", A secondlife assistant. you are talking to a group of people."));
history.Add(new KeyValuePair<string, string>("system", "You are " + GetClient().Self.FirstName + ", "+myConfig.GetChatPrompt()+" you are talking to a group of people."));
}
else
{
history.Add(new KeyValuePair<string, string>("system", "You are " + GetClient().Self.FirstName + ", A secondlife assistant. you are talking to people in a public place."));
history.Add(new KeyValuePair<string, string>("system", "You are " + GetClient().Self.FirstName + ", "+myConfig.GetChatPrompt()+" you are talking to people in a public place."));
}
}
history.Add(new KeyValuePair<string, string>("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]");