Updated Games module

This commit is contained in:
Toastie (DCS Team) 2024-07-07 20:35:25 +12:00
parent 499c2a9a5f
commit f91771a8db
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 22 additions and 11 deletions

View file

@ -88,14 +88,16 @@ public class ChatterBotService : IExecOnMessage
public string PrepareMessage(IUserMessage msg) public string PrepareMessage(IUserMessage msg)
{ {
var nadekoId = _client.CurrentUser.Id; var ellieId = _client.CurrentUser.Id;
var normalMention = $"<@{nadekoId}> "; var normalMention = $"<@{ellieId}> ";
var nickMention = $"<@!{nadekoId}> "; var nickMention = $"<@!{ellieId}> ";
string message; string message;
if (msg.Content.StartsWith(normalMention, StringComparison.InvariantCulture)) if (msg.Content.StartsWith(normalMention, StringComparison.InvariantCulture))
message = msg.Content[normalMention.Length..].Trim(); message = msg.Content[normalMention.Length..].Trim();
else if (msg.Content.StartsWith(nickMention, StringComparison.InvariantCulture)) else if (msg.Content.StartsWith(nickMention, StringComparison.InvariantCulture))
message = msg.Content[nickMention.Length..].Trim(); message = msg.Content[nickMention.Length..].Trim();
else if (msg.ReferencedMessage?.Author.Id == ellieId)
message = msg.Content;
else else
return null; return null;

View file

@ -3,10 +3,12 @@ using Newtonsoft.Json;
using OneOf.Types; using OneOf.Types;
using System.Net.Http.Json; using System.Net.Http.Json;
using SharpToken; using SharpToken;
using System.CodeDom;
using System.Text.RegularExpressions;
namespace EllieBot.Modules.Games.Common.ChatterBot; namespace EllieBot.Modules.Games.Common.ChatterBot;
public class OfficialGptSession : IChatterBotSession public partial class OfficialGptSession : IChatterBotSession
{ {
private string Uri private string Uri
=> $"https://api.openai.com/v1/chat/completions"; => $"https://api.openai.com/v1/chat/completions";
@ -16,7 +18,7 @@ public class OfficialGptSession : IChatterBotSession
private readonly int _maxHistory; private readonly int _maxHistory;
private readonly int _maxTokens; private readonly int _maxTokens;
private readonly int _minTokens; private readonly int _minTokens;
private readonly string _nadekoUsername; private readonly string _ellieUsername;
private readonly GptEncoding _encoding; private readonly GptEncoding _encoding;
private List<GPTMessage> messages = new(); private List<GPTMessage> messages = new();
private readonly IHttpClientFactory _httpFactory; private readonly IHttpClientFactory _httpFactory;
@ -29,7 +31,7 @@ public class OfficialGptSession : IChatterBotSession
int maxTokens, int maxTokens,
int minTokens, int minTokens,
string personality, string personality,
string nadekoUsername, string ellieUsername,
IHttpClientFactory factory) IHttpClientFactory factory)
{ {
_apiKey = apiKey; _apiKey = apiKey;
@ -45,24 +47,31 @@ public class OfficialGptSession : IChatterBotSession
_maxHistory = chatHistory; _maxHistory = chatHistory;
_maxTokens = maxTokens; _maxTokens = maxTokens;
_minTokens = minTokens; _minTokens = minTokens;
_nadekoUsername = nadekoUsername; _ellieUsername = UsernameCleaner().Replace(ellieUsername, "");
_encoding = GptEncoding.GetEncodingForModel(_model); _encoding = GptEncoding.GetEncodingForModel(_model);
messages.Add(new() messages.Add(new()
{ {
Role = "system", Role = "system",
Content = personality, Content = personality,
Name = _nadekoUsername Name = _ellieUsername
}); });
} }
[GeneratedRegex("[^a-zA-Z0-9_-]")]
private static partial Regex UsernameCleaner();
public async Task<OneOf.OneOf<ThinkResult, Error<string>>> Think(string input, string username) public async Task<OneOf.OneOf<ThinkResult, Error<string>>> Think(string input, string username)
{ {
username = UsernameCleaner().Replace(username, "");
messages.Add(new() messages.Add(new()
{ {
Role = "user", Role = "user",
Content = input, Content = input,
Name = username Name = username
}); });
while (messages.Count > _maxHistory + 2) while (messages.Count > _maxHistory + 2)
{ {
messages.RemoveAt(1); messages.RemoveAt(1);
@ -103,8 +112,8 @@ public class OfficialGptSession : IChatterBotSession
try try
{ {
var response = JsonConvert.DeserializeObject<OpenAiCompletionResponse>(dataString); var response = JsonConvert.DeserializeObject<OpenAiCompletionResponse>(dataString);
Log.Information("Received response: {response}", dataString); Log.Information("Received response: {response} ", dataString);
var res = response?.Choices?[0]; var res = response?.Choices?[0];
var message = res?.Message?.Content; var message = res?.Message?.Content;
@ -117,7 +126,7 @@ public class OfficialGptSession : IChatterBotSession
{ {
Role = "assistant", Role = "assistant",
Content = message, Content = message,
Name = _nadekoUsername Name = _ellieUsername
}); });
return new ThinkResult() return new ThinkResult()