mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-11 15:19:53 +00:00
Add chat functionality (#1)
* Working chatbot * Clean * Working LLM chatbot --------- Co-authored-by: Myx <info@azaaxin.com>
This commit is contained in:
62
Bot/Handler/ChatCommand/ChatHandler.cs
Normal file
62
Bot/Handler/ChatCommand/ChatHandler.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Text;
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OllamaSharp;
|
||||
|
||||
namespace Lunaris2.Handler.ChatCommand
|
||||
{
|
||||
public record ChatCommand(SocketMessage Message, string FilteredMessage) : IRequest;
|
||||
|
||||
public class ChatHandler : IRequestHandler<ChatCommand>
|
||||
{
|
||||
private readonly OllamaApiClient _ollama;
|
||||
private readonly Dictionary<ulong, Chat?> _chatContexts = new();
|
||||
|
||||
public ChatHandler(IOptions<ChatSettings> chatSettings)
|
||||
{
|
||||
var uri = new Uri(chatSettings.Value.Url);
|
||||
|
||||
_ollama = new OllamaApiClient(uri)
|
||||
{
|
||||
SelectedModel = chatSettings.Value.Model
|
||||
};
|
||||
}
|
||||
|
||||
public async Task Handle(ChatCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var channelId = command.Message.Channel.Id;
|
||||
_chatContexts.TryAdd(channelId, null);
|
||||
|
||||
var userMessage = command.FilteredMessage;
|
||||
|
||||
using var setTyping = command.Message.Channel.EnterTypingState();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(userMessage))
|
||||
{
|
||||
await command.Message.Channel.SendMessageAsync("Am I expected to read your mind?");
|
||||
setTyping.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
var response = await GenerateResponse(userMessage, channelId, cancellationToken);
|
||||
await command.Message.Channel.SendMessageAsync(response);
|
||||
|
||||
setTyping.Dispose();
|
||||
}
|
||||
|
||||
private async Task<string> GenerateResponse(string userMessage, ulong channelId, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new StringBuilder();
|
||||
|
||||
if (_chatContexts[channelId] == null)
|
||||
{
|
||||
_chatContexts[channelId] = _ollama.Chat(stream => response.Append(stream.Message?.Content ?? ""));
|
||||
}
|
||||
|
||||
await _chatContexts[channelId].Send(userMessage, cancellationToken);
|
||||
|
||||
return response.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Bot/Handler/ChatCommand/ChatSettings.cs
Normal file
7
Bot/Handler/ChatCommand/ChatSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Lunaris2.Handler.ChatCommand;
|
||||
|
||||
public class ChatSettings
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public string Model { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user