96 lines
4.1 KiB
C#
96 lines
4.1 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, bool needsRestart = false)
|
|
{
|
|
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, needsRestart);
|
|
var resposta = "";
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(resp) && resp != "REINICIAR" && ( question.ToUpper() != "REINICIAR" && question.ToUpper() != "SAIR"))
|
|
{
|
|
resposta = await _chatBotCall.GetNextQuestion();
|
|
|
|
history.AddUserMessage(question);
|
|
history.AddMessage(AuthorRole.Assistant, resposta ?? question);
|
|
|
|
_chatHistoryService.UpdateHistory(sessionId, history);
|
|
|
|
if (!_chatBotCall.HasNextQuestion())
|
|
{
|
|
resposta = await _chatBotCall.CallFinalAction();
|
|
_classifierPersistence.DeleteState(sessionId);
|
|
_chatBotCall.SetAnswer(sessionId, "REINICIAR", true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!needsRestart && resp == "REINICIAR" || (question.ToUpper() != "REINICIAR" || question.ToUpper() != "SAIR"))
|
|
{
|
|
_classifierPersistence.DeleteState(sessionId);
|
|
_chatBotCall.SetAnswer(sessionId, "REINICIAR", true);
|
|
resp = "Ok! Parece que você não quer continuar e/ou eu não entendi sua resposta. Tudo bem! Se quiser, tente novamente depois...";
|
|
}
|
|
else
|
|
{
|
|
resp = "Quero abrir uma nova solicitação ao RH.";
|
|
await GetResponse(context, userData, sessionId, resp, true);
|
|
}
|
|
|
|
resposta = resp;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
resposta = "Tivemos um problema. Teremos que reiniciar as perguntas se vc disser que precisa de uma solicitação de rh novamente. Desculpe!";
|
|
_classifierPersistence.DeleteState(sessionId);
|
|
}
|
|
finally
|
|
{
|
|
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.
|