ChatApi/Services/ResponseService/ResponseBotRHService.cs
2024-11-28 22:49:03 -03:00

77 lines
3.0 KiB
C#

using ChatApi.Models;
using ChatApi.Services.Bot;
using ChatApi.Services.Bot.Structs;
using ChatApi.Services.Classifier;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Embeddings;
using OllamaSharp.Models.Chat;
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
namespace ChatApi.Services.ResponseService
{
public class ResponseBotRHService : IResponseService
{
private readonly ChatHistoryService _chatHistoryService;
private readonly TextFilter _textFilter;
private readonly ChatBotRHCall _chatBotCall;
private readonly ClassifierPersistence _classifierPersistence;
public ResponseBotRHService(
ChatHistoryService chatHistoryService,
TextFilter textFilter,
ChatBotRHCall chatBotCall,
ClassifierPersistence classifierPersistence)
{
_chatHistoryService = chatHistoryService;
_textFilter = textFilter;
_chatBotCall = chatBotCall;
_classifierPersistence = classifierPersistence;
}
public EnumClassification Classification => EnumClassification.BotRHCall;
public async Task<string> GetResponse(HttpContext context, UserData userData, string sessionId, string question)
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
SessionIdStore sessionIdStore = new SessionIdStore(context);
ChatHistory history = _chatHistoryService.GetSumarizer(sessionId);
_chatBotCall.UserData = userData;
var resp = _chatBotCall.SetAnswer(sessionId, question);
var resposta = "";
if (string.IsNullOrEmpty(resp) && resp!="REINICIAR")
{
resposta = await _chatBotCall.GetNextQuestion();
history.AddUserMessage(question);
history.AddMessage(AuthorRole.Assistant, resposta ?? "");
_chatHistoryService.UpdateHistory(sessionId, history);
if (!_chatBotCall.HasNextQuestion())
{
_classifierPersistence.DeleteState(sessionId);
}
}
else
{
if (resp == "REINICIAR")
{
_classifierPersistence.DeleteState(sessionId);
resp = "Ok! Parece que você não quer continuar e/ou eu não entendi sua resposta. Tudo bem! Se quiser, tente novamente depois...";
}
resposta = resp;
}
stopWatch.Stop();
return $"{resposta ?? ""}\n\nTempo: {stopWatch.ElapsedMilliseconds / 1000}s";
}
}
}
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.