Updated Games module
This commit is contained in:
parent
499c2a9a5f
commit
f91771a8db
2 changed files with 22 additions and 11 deletions
|
@ -88,14 +88,16 @@ public class ChatterBotService : IExecOnMessage
|
|||
|
||||
public string PrepareMessage(IUserMessage msg)
|
||||
{
|
||||
var nadekoId = _client.CurrentUser.Id;
|
||||
var normalMention = $"<@{nadekoId}> ";
|
||||
var nickMention = $"<@!{nadekoId}> ";
|
||||
var ellieId = _client.CurrentUser.Id;
|
||||
var normalMention = $"<@{ellieId}> ";
|
||||
var nickMention = $"<@!{ellieId}> ";
|
||||
string message;
|
||||
if (msg.Content.StartsWith(normalMention, StringComparison.InvariantCulture))
|
||||
message = msg.Content[normalMention.Length..].Trim();
|
||||
else if (msg.Content.StartsWith(nickMention, StringComparison.InvariantCulture))
|
||||
message = msg.Content[nickMention.Length..].Trim();
|
||||
else if (msg.ReferencedMessage?.Author.Id == ellieId)
|
||||
message = msg.Content;
|
||||
else
|
||||
return null;
|
||||
|
||||
|
|
|
@ -3,10 +3,12 @@ using Newtonsoft.Json;
|
|||
using OneOf.Types;
|
||||
using System.Net.Http.Json;
|
||||
using SharpToken;
|
||||
using System.CodeDom;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace EllieBot.Modules.Games.Common.ChatterBot;
|
||||
|
||||
public class OfficialGptSession : IChatterBotSession
|
||||
public partial class OfficialGptSession : IChatterBotSession
|
||||
{
|
||||
private string Uri
|
||||
=> $"https://api.openai.com/v1/chat/completions";
|
||||
|
@ -16,7 +18,7 @@ public class OfficialGptSession : IChatterBotSession
|
|||
private readonly int _maxHistory;
|
||||
private readonly int _maxTokens;
|
||||
private readonly int _minTokens;
|
||||
private readonly string _nadekoUsername;
|
||||
private readonly string _ellieUsername;
|
||||
private readonly GptEncoding _encoding;
|
||||
private List<GPTMessage> messages = new();
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
|
@ -29,7 +31,7 @@ public class OfficialGptSession : IChatterBotSession
|
|||
int maxTokens,
|
||||
int minTokens,
|
||||
string personality,
|
||||
string nadekoUsername,
|
||||
string ellieUsername,
|
||||
IHttpClientFactory factory)
|
||||
{
|
||||
_apiKey = apiKey;
|
||||
|
@ -45,24 +47,31 @@ public class OfficialGptSession : IChatterBotSession
|
|||
_maxHistory = chatHistory;
|
||||
_maxTokens = maxTokens;
|
||||
_minTokens = minTokens;
|
||||
_nadekoUsername = nadekoUsername;
|
||||
_ellieUsername = UsernameCleaner().Replace(ellieUsername, "");
|
||||
_encoding = GptEncoding.GetEncodingForModel(_model);
|
||||
messages.Add(new()
|
||||
{
|
||||
Role = "system",
|
||||
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)
|
||||
{
|
||||
username = UsernameCleaner().Replace(username, "");
|
||||
|
||||
messages.Add(new()
|
||||
{
|
||||
Role = "user",
|
||||
Content = input,
|
||||
Name = username
|
||||
});
|
||||
|
||||
while (messages.Count > _maxHistory + 2)
|
||||
{
|
||||
messages.RemoveAt(1);
|
||||
|
@ -103,8 +112,8 @@ public class OfficialGptSession : IChatterBotSession
|
|||
try
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<OpenAiCompletionResponse>(dataString);
|
||||
|
||||
Log.Information("Received response: {response}", dataString);
|
||||
|
||||
Log.Information("Received response: {response} ", dataString);
|
||||
var res = response?.Choices?[0];
|
||||
var message = res?.Message?.Content;
|
||||
|
||||
|
@ -117,7 +126,7 @@ public class OfficialGptSession : IChatterBotSession
|
|||
{
|
||||
Role = "assistant",
|
||||
Content = message,
|
||||
Name = _nadekoUsername
|
||||
Name = _ellieUsername
|
||||
});
|
||||
|
||||
return new ThinkResult()
|
||||
|
|
Loading…
Reference in a new issue