Codettev3 / MyBot.cs
Raiff1982's picture
Upload 20 files
19a14ad verified
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
public class MyBot
{
private readonly HttpClient _httpClient;
private readonly ILogger<MyBot> _logger;
private readonly Dictionary<string, List<Dictionary<string, object>>> _context;
private readonly SentimentAnalysis _sentimentAnalysis;
public MyBot(ILogger<MyBot> logger, IHttpClientFactory httpClientFactory, SentimentAnalysis sentimentAnalysis)
{
_httpClient = httpClientFactory.CreateClient();
_logger = logger;
_context = new Dictionary<string, List<Dictionary<string, object>>>();
_sentimentAnalysis = sentimentAnalysis;
}
public async Task<string> GenerateResponse(string text, string userId)
{
try
{
_logger.LogInformation($"Generating response for user_id: {userId} with text: {text}");
var messages = new List<Dictionary<string, string>>
{
new Dictionary<string, string> { { "role", "system" }, { "content", "You are a helpful assistant." } },
new Dictionary<string, string> { { "role", "user" }, { "content", text } }
};
var response = await AzureChatCompletionRequest(messages);
_logger.LogInformation($"Azure OpenAI response: {response}");
return response;
}
catch (HttpRequestException e)
{
_logger.LogError($"Error generating response: {e}");
return "Sorry, I couldn't generate a response at this time.";
}
catch (Exception e)
{
_logger.LogError($"Unexpected error: {e}");
return "An unexpected error occurred. Please try again later.";
}
}
private async Task<string> AzureChatCompletionRequest(List<Dictionary<string, string>> messages)
{
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
var payload = new
{
model = "gpt-4",
messages = messages
};
var requestContent = new StringContent(System.Text.Json.JsonSerializer.Serialize(payload), System.Text.Encoding.UTF8, "application/json");
_httpClient.DefaultRequestHeaders.Add("api-key", apiKey);
var response = await _httpClient.PostAsync(endpoint, requestContent);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
var responseObject = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(responseContent);
var choices = responseObject["choices"] as List<Dictionary<string, object>>;
var message = choices[0]["message"] as Dictionary<string, string>;
return message["content"];
}
public void EnhanceContextAwareness(string userId, string text)
{
var sentiment = _sentimentAnalysis.Predict(text);
if (!_context.ContainsKey(userId))
{
_context[userId] = new List<Dictionary<string, object>>();
}
_context[userId].Add(new Dictionary<string, object> { { "text", text }, { "sentiment", sentiment } });
}
public void ProactiveLearning(string userId, string feedback)
{
if (!_context.ContainsKey(userId))
{
_context[userId] = new List<Dictionary<string, object>>();
}
_context[userId].Add(new Dictionary<string, object> { { "feedback", feedback } });
}
public void EthicalDecisionMaking(string userId, string decision)
{
var ethicalDecision = $"Considering ethical principles, the decision is: {decision}";
if (!_context.ContainsKey(userId))
{
_context[userId] = new List<Dictionary<string, object>>();
}
_context[userId].Add(new Dictionary<string, object> { { "ethical_decision", ethicalDecision } });
}
public string EmotionalIntelligence(string userId, string text)
{
var sentiment = _sentimentAnalysis.Predict(text);
var response = $"I sense that you are feeling {sentiment.Probability}. How can I assist you further?";
if (!_context.ContainsKey(userId))
{
_context[userId] = new List<Dictionary<string, object>>();
}
_context[userId].Add(new Dictionary<string, object> { { "emotional_response", response } });
return response;
}
public string TransparencyAndExplainability(string userId, string decision)
{
var explanation = $"The decision was made based on the following context: {_context[userId]}";
if (!_context.ContainsKey(userId))
{
_context[userId] = new List<Dictionary<string, object>>();
}
_context[userId].Add(new Dictionary<string, object> { { "explanation", explanation } });
return explanation;
}
}