126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using ChatServerSpace.Models;
|
|
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
|
using Microsoft.SemanticKernel;
|
|
using Microsoft.SemanticKernel.ChatCompletion;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace ChatServerSpace.Services
|
|
{
|
|
public class ServerSpaceChatService : IChatService
|
|
{
|
|
private readonly IChatHistoryService _historyService;
|
|
private readonly Kernel _kernel;
|
|
private readonly ILogger<ServerSpaceChatService> _logger;
|
|
|
|
public ServerSpaceChatService(IChatHistoryService historyService, IConfiguration config, ILogger<ServerSpaceChatService> logger, HttpClient httpClient)
|
|
{
|
|
_historyService = historyService;
|
|
_logger = logger;
|
|
|
|
var apiKey = config["ChatServer:ApiKey"] ?? throw new ArgumentNullException("ServeSpace:ApiKey configuration is missing");
|
|
var modelId = config["ChatServer:ModelId"] ?? "anthropic/claude-3.7-sonnet";
|
|
var endpoint = config["ChatServer:Url"] ?? "https://api.deepinfra.com/v1/openai/chat/completions";
|
|
|
|
|
|
_kernel = Kernel.CreateBuilder()
|
|
.AddOpenAIChatCompletion(modelId, new Uri(endpoint), apiKey)
|
|
.Build();
|
|
}
|
|
|
|
public async Task<ChatResponse> ProcessMessageAsync(ChatRequest request)
|
|
{
|
|
try
|
|
{
|
|
// Save user message to history
|
|
var userMessage = new ChatMessage
|
|
{
|
|
|
|
SessionId = request.SessionId,
|
|
Role = "user",
|
|
Content = request.Message
|
|
};
|
|
await _historyService.SaveMessageAsync(userMessage);
|
|
|
|
// Get chat history
|
|
var history = await _historyService.GetSessionHistoryAsync(request.SessionId);
|
|
|
|
// Create chat history for Semantic Kernel
|
|
var chatHistory = new Microsoft.SemanticKernel.ChatCompletion.ChatHistory();
|
|
|
|
foreach (var message in history)
|
|
{
|
|
if (message.Role.Equals("user", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
chatHistory.AddUserMessage(message.Content);
|
|
}
|
|
else if (message.Role.Equals("assistant", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
chatHistory.AddAssistantMessage(message.Content);
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
// Get response from Gemini
|
|
var chatCompletionService = _kernel.GetRequiredService<IChatCompletionService>();
|
|
|
|
var exec = new PromptExecutionSettings
|
|
{
|
|
ExtensionData = new Dictionary<string, object>
|
|
{
|
|
{ "maxOutputTokens", 8192 },
|
|
{ "temperature", 0.8 },
|
|
{ "topP", 0.95 },
|
|
//{ "stopSequences", new List<string>() }
|
|
}
|
|
};
|
|
var result = await chatCompletionService.GetChatMessageContentAsync(chatHistory, exec);
|
|
var responseText = result.ToString();
|
|
|
|
// Save assistant message to history
|
|
var assistantMessage = new ChatMessage
|
|
{
|
|
SessionId = request.SessionId,
|
|
Role = "assistant",
|
|
Content = responseText
|
|
};
|
|
await _historyService.SaveMessageAsync(assistantMessage);
|
|
|
|
await Task.Delay(2000);
|
|
|
|
return new ChatResponse
|
|
{
|
|
SessionId = request.SessionId,
|
|
Response = responseText
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error getting response from Gemini");
|
|
throw;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Erro na parada!", ex);
|
|
}
|
|
}
|
|
|
|
private class ServerSpaceResponse
|
|
{
|
|
public List<ServerSpaceChoice>? Choices { get; set; }
|
|
}
|
|
|
|
private class ServerSpaceChoice
|
|
{
|
|
public ServerSpaceMessage? Message { get; set; }
|
|
}
|
|
|
|
private class ServerSpaceMessage
|
|
{
|
|
public string? Content { get; set; }
|
|
}
|
|
}
|
|
} |