File size: 5,115 Bytes
19a14ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
    }
}