diff --git a/Bot/Handler/ChatCommand/ChatHandler.cs b/Bot/Handler/ChatCommand/ChatHandler.cs new file mode 100644 index 0000000..583769c --- /dev/null +++ b/Bot/Handler/ChatCommand/ChatHandler.cs @@ -0,0 +1,44 @@ +using Discord; +using Discord.WebSocket; +using Lunaris2.Handler.MusicPlayer; +using Lunaris2.SlashCommand; +using MediatR; +using OllamaSharp; +using OllamaSharp.Models; + +namespace Lunaris2.Handler.ChatCommand; + +public record ChatCommand(SocketSlashCommand Message) : IRequest; + +public class ChatHandler : IRequestHandler +{ + private readonly Uri _uri = new("http://192.168.50.54:11434"); + private OllamaApiClient _ollama; + private SocketSlashCommand _context; + + public ChatHandler() + { + _ollama = new OllamaApiClient(_uri); + _ollama.SelectedModel = "lunaris"; + } + + public async Task Handle(ChatCommand command, CancellationToken cancellationToken) + { + _context = command.Message; + var userMessage = _context.GetOptionValueByName(Option.Input); + var setTyping = command.Message.Channel.EnterTypingState(); + await command.Message.DeferAsync(); + + var response = ""; + ConversationContext chatContext = null; + + async void Streamer(GenerateCompletionResponseStream stream) + { + response += stream.Response; + await command.Message.ModifyOriginalResponseAsync(properties => properties.Content = response); + } + + chatContext = await _ollama.StreamCompletion(userMessage, chatContext, Streamer, cancellationToken: cancellationToken); + setTyping.Dispose(); + } +} \ No newline at end of file diff --git a/Bot/Handler/MessageReceivedHandler.cs b/Bot/Handler/MessageReceivedHandler.cs index b1e744f..6eecaeb 100644 --- a/Bot/Handler/MessageReceivedHandler.cs +++ b/Bot/Handler/MessageReceivedHandler.cs @@ -29,6 +29,9 @@ public class MessageReceivedHandler(ISender mediator) : INotificationHandler + diff --git a/Bot/SlashCommand/Command.cs b/Bot/SlashCommand/Command.cs index 1c7cd3a..ff2cde6 100644 --- a/Bot/SlashCommand/Command.cs +++ b/Bot/SlashCommand/Command.cs @@ -21,6 +21,23 @@ public static class Command public const string Description = "Say goodbye to the bot!"; } + public static class Chat + { + public const string Name = "chat"; + public const string Description = "Chat with the bot!"; + + public static readonly List? Options = new() + { + new SlashCommandOptionBuilder + { + Name = "message", + Description = "Chat with Lunaris", + Type = ApplicationCommandOptionType.String, + IsRequired = true + } + }; + } + public static class Join { public const string Name = "join"; diff --git a/Bot/SlashCommand/SlashCommandRegistration.cs b/Bot/SlashCommand/SlashCommandRegistration.cs index 26331e9..7770141 100644 --- a/Bot/SlashCommand/SlashCommandRegistration.cs +++ b/Bot/SlashCommand/SlashCommandRegistration.cs @@ -13,6 +13,7 @@ public static class SlashCommandRegistration RegisterCommand(client, Command.Skip.Name, Command.Skip.Description); RegisterCommand(client, Command.Play.Name, Command.Play.Description, Command.Play.Options); RegisterCommand(client, Command.Stop.Name, Command.Stop.Description); + RegisterCommand(client, Command.Chat.Name, Command.Chat.Description, Command.Chat.Options); } private static void RegisterCommand(