using ChatApi.Models; using Microsoft.SemanticKernel.ChatCompletion; using OllamaSharp.Models.Chat; using System.Diagnostics; namespace ChatApi.Services.ResponseService { public class ResponseChatService : IResponseService { private readonly ChatHistoryService _chatHistoryService; private readonly IChatCompletionService _chatCompletionService; public ResponseChatService( ChatHistoryService chatHistoryService, IChatCompletionService chatCompletionService) { this._chatHistoryService = chatHistoryService; this._chatCompletionService = chatCompletionService; } public EnumClassification Classification => EnumClassification.Chat; public async Task 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.Get(sessionId); history.AddUserMessage(question); var response = await _chatCompletionService.GetChatMessageContentAsync(history); history.AddMessage(response.Role, response.Content ?? ""); _chatHistoryService.UpdateHistory(sessionId, history); stopWatch.Stop(); return $"{response.Content ?? ""}\n\nTempo: {stopWatch.ElapsedMilliseconds / 1000}s"; } } }