mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-17 11:47:16 +00:00
Working chatbot
This commit is contained in:
44
Bot/Handler/ChatCommand/ChatHandler.cs
Normal file
44
Bot/Handler/ChatCommand/ChatHandler.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,6 +29,9 @@ public class MessageReceivedHandler(ISender mediator) : INotificationHandler<Mes
|
|||||||
case Command.Skip.Name:
|
case Command.Skip.Name:
|
||||||
await mediator.Send(new SkipCommand(notification.Message), cancellationToken);
|
await mediator.Send(new SkipCommand(notification.Message), cancellationToken);
|
||||||
break;
|
break;
|
||||||
|
case Command.Chat.Name:
|
||||||
|
await mediator.Send(new ChatCommand.ChatCommand(notification.Message), cancellationToken);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" 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" />
|
<PackageReference Include="Victoria" Version="6.0.23.324" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,23 @@ public static class Command
|
|||||||
public const string Description = "Say goodbye to the bot!";
|
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 static class Join
|
||||||
{
|
{
|
||||||
public const string Name = "join";
|
public const string Name = "join";
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public static class SlashCommandRegistration
|
|||||||
RegisterCommand(client, Command.Skip.Name, Command.Skip.Description);
|
RegisterCommand(client, Command.Skip.Name, Command.Skip.Description);
|
||||||
RegisterCommand(client, Command.Play.Name, Command.Play.Description, Command.Play.Options);
|
RegisterCommand(client, Command.Play.Name, Command.Play.Description, Command.Play.Options);
|
||||||
RegisterCommand(client, Command.Stop.Name, Command.Stop.Description);
|
RegisterCommand(client, Command.Stop.Name, Command.Stop.Description);
|
||||||
|
RegisterCommand(client, Command.Chat.Name, Command.Chat.Description, Command.Chat.Options);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterCommand(
|
private static void RegisterCommand(
|
||||||
|
|||||||
Reference in New Issue
Block a user