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; #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 { [Authorize] [ApiController] [Route("[controller]")] public class ChatController : ControllerBase { private readonly ILogger _logger; private readonly TextFilter _textFilter; private readonly ResponseFactory _responseFactory; private readonly ClassifierPersistence _classifierPersistence; public ChatController( ILogger logger, TextFilter textFilter, ResponseFactory responseFactory, ClassifierPersistence classifierPersistence) { _logger = logger; _textFilter = textFilter; _responseFactory = responseFactory; _classifierPersistence = classifierPersistence; } [HttpGet(Name = "Response")] public async Task Get([FromQuery] ChatRequest chatRequest) { var userData = UserData.Create(User); var textClassifier = new TextClassifier(_textFilter, _classifierPersistence); var textType = await textClassifier.ClassifyQuestion(chatRequest.SessionId, chatRequest.Message); var responseText = _responseFactory.GetService(textType); var response = await responseText.GetResponse(HttpContext, userData, chatRequest.SessionId, chatRequest.Message); return response; } [HttpPost(Name = "LoadDBData")] public async Task SaveData([FromQuery] DBLoadRequest loadRequest) { //string readText = System.IO.File.ReadAllText("C:\\vscode\\ChatApi\\bin\\Debug\\net8.0\\Servicos.txt"); string readText = loadRequest.Content; await SalvarTextoComEmbeddingNoMongoDB(readText); } async Task SalvarTextoComEmbeddingNoMongoDB(string textoCompleto) { var textoArray = new List(); string[] textolinhas = textoCompleto.Split( new string[] { "\n" }, StringSplitOptions.None ); var title = textolinhas[0]; var builder = new StringBuilder(); foreach (string line in textolinhas) { if (line.StartsWith("**") || line.StartsWith("\r**")) { if (builder.Length > 0) { textoArray.Add(title.Replace("**", "").Replace("\r", "") + ": " + Environment.NewLine + builder.ToString()); builder = new StringBuilder(); title = line; } } else { builder.AppendLine(line); } } } } } #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.