ChatApi/Controllers/ChatController.cs
2024-12-22 15:49:43 -03:00

78 lines
3.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Connectors.InMemory;
using System.Globalization;
using System.Text;
using MongoDB.Driver;
using System.IO;
using System.Text.RegularExpressions;
using ChatApi.Services;
using ChatApi.Services.ResponseService;
using ChatApi.Services.Classifier;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using ChatApi.Models;
using ChatApi.Data;
#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.Controllers
{
[ApiController]
[Route("[controller]")]
[Authorize]
public class ChatController : ControllerBase
{
private readonly ILogger<ChatController> _logger;
private readonly TextFilter _textFilter;
private readonly ResponseFactory _responseFactory;
private readonly ClassifierPersistence _classifierPersistence;
private readonly UserDataRepository _userDataRepository;
private readonly TextData _textData;
public ChatController(
ILogger<ChatController> logger,
TextFilter textFilter,
ResponseFactory responseFactory,
ClassifierPersistence classifierPersistence,
UserDataRepository userDataRepository,
TextData textData)
{
_logger = logger;
_textFilter = textFilter;
_responseFactory = responseFactory;
_classifierPersistence = classifierPersistence;
_userDataRepository = userDataRepository;
_textData = textData;
}
[HttpGet]
[Route("response")]
public async Task<string?> GetResponse([FromQuery] ChatRequest chatRequest)
{
//var userData = UserData.Create(User);
var textClassifier = new TextClassifier(_textFilter, _classifierPersistence);
var textType = await textClassifier.ClassifyQuestion(chatRequest.SessionId, chatRequest.Message);
var needsRestart = textClassifier.NeedsRestart();
var responseText = _responseFactory.GetService(textType);
var userData = await _userDataRepository.GeByToekntAsync(AppDomain.CurrentDomain.GetData("Token") as string);
var response = await responseText.GetResponse(HttpContext, userData, chatRequest.SessionId, chatRequest.Message, needsRestart);
return response;
}
[HttpPost]
[Route("loaddata")]
//public async Task SaveData([FromQuery] DBLoadRequest loadRequest)
public async Task SaveData()
{
string readText = System.IO.File.ReadAllText("C:\\vscode\\ChatApi\\bin\\Debug\\net8.0\\Servicos.txt");
//string readText = loadRequest.Content;
await _textData.SalvarTextoComEmbeddingNoMongoDB(readText);
}
}
}
#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.