Working chatbot

This commit is contained in:
Myx
2024-06-01 12:34:03 +02:00
parent 3d7655a902
commit 87b35a2203
5 changed files with 66 additions and 0 deletions

View File

@@ -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<ChatCommand>
{
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();
}
}

View File

@@ -29,6 +29,9 @@ public class MessageReceivedHandler(ISender mediator) : INotificationHandler<Mes
case Command.Skip.Name:
await mediator.Send(new SkipCommand(notification.Message), cancellationToken);
break;
case Command.Chat.Name:
await mediator.Send(new ChatCommand.ChatCommand(notification.Message), cancellationToken);
break;
}
}
}

View File

@@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="OllamaSharp" Version="1.1.10" />
<PackageReference Include="Victoria" Version="6.0.23.324" />
</ItemGroup>

View File

@@ -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<SlashCommandOptionBuilder>? Options = new()
{
new SlashCommandOptionBuilder
{
Name = "message",
Description = "Chat with Lunaris",
Type = ApplicationCommandOptionType.String,
IsRequired = true
}
};
}
public static class Join
{
public const string Name = "join";

View File

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