mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-23 06:15:09 +00:00
Clean
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using Discord;
|
using System.Text;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Lunaris2.Handler.MusicPlayer;
|
using Lunaris2.Handler.MusicPlayer;
|
||||||
using Lunaris2.SlashCommand;
|
using Lunaris2.SlashCommand;
|
||||||
@@ -6,39 +6,59 @@ using MediatR;
|
|||||||
using OllamaSharp;
|
using OllamaSharp;
|
||||||
using OllamaSharp.Models;
|
using OllamaSharp.Models;
|
||||||
|
|
||||||
namespace Lunaris2.Handler.ChatCommand;
|
namespace Lunaris2.Handler.ChatCommand
|
||||||
|
{
|
||||||
public record ChatCommand(SocketSlashCommand Message) : IRequest;
|
public record ChatCommand(SocketSlashCommand Message) : IRequest;
|
||||||
|
|
||||||
public class ChatHandler : IRequestHandler<ChatCommand>
|
public class ChatHandler : IRequestHandler<ChatCommand>
|
||||||
{
|
{
|
||||||
private readonly Uri _uri = new("http://192.168.50.54:11434");
|
private readonly Uri _uri = new("http://192.168.50.54:11434");
|
||||||
private OllamaApiClient _ollama;
|
private readonly OllamaApiClient _ollama;
|
||||||
private SocketSlashCommand _context;
|
private SocketSlashCommand _context;
|
||||||
|
|
||||||
public ChatHandler()
|
public ChatHandler()
|
||||||
{
|
{
|
||||||
_ollama = new OllamaApiClient(_uri);
|
_ollama = new OllamaApiClient(_uri)
|
||||||
_ollama.SelectedModel = "lunaris";
|
{
|
||||||
|
SelectedModel = "lunaris"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(ChatCommand command, CancellationToken cancellationToken)
|
public async Task Handle(ChatCommand command, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_context = command.Message;
|
_context = command.Message;
|
||||||
|
|
||||||
var userMessage = _context.GetOptionValueByName(Option.Input);
|
var userMessage = _context.GetOptionValueByName(Option.Input);
|
||||||
var setTyping = command.Message.Channel.EnterTypingState();
|
|
||||||
|
using var setTyping = command.Message.Channel.EnterTypingState();
|
||||||
await command.Message.DeferAsync();
|
await command.Message.DeferAsync();
|
||||||
|
|
||||||
var response = "";
|
if (string.IsNullOrWhiteSpace(userMessage))
|
||||||
ConversationContext chatContext = null;
|
|
||||||
|
|
||||||
async void Streamer(GenerateCompletionResponseStream stream)
|
|
||||||
{
|
{
|
||||||
response += stream.Response;
|
await command.Message.ModifyOriginalResponseAsync(properties => properties.Content = "Am I expected to read your mind?");
|
||||||
|
setTyping.Dispose();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = await GenerateResponse(userMessage, cancellationToken);
|
||||||
await command.Message.ModifyOriginalResponseAsync(properties => properties.Content = response);
|
await command.Message.ModifyOriginalResponseAsync(properties => properties.Content = response);
|
||||||
}
|
}
|
||||||
|
|
||||||
chatContext = await _ollama.StreamCompletion(userMessage, chatContext, Streamer, cancellationToken: cancellationToken);
|
private async Task<string> GenerateResponse(string userMessage, CancellationToken cancellationToken)
|
||||||
setTyping.Dispose();
|
{
|
||||||
|
var response = new StringBuilder();
|
||||||
|
ConversationContext? chatContext = null;
|
||||||
|
|
||||||
|
chatContext = await _ollama.StreamCompletion(
|
||||||
|
userMessage,
|
||||||
|
chatContext,
|
||||||
|
Streamer,
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
return response.ToString();
|
||||||
|
|
||||||
|
void Streamer(GenerateCompletionResponseStream stream) =>
|
||||||
|
response.Append(stream.Response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user