aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Controllers/ChatController.cs
blob: 396651e3ffa6b94180e3235329f0f915e0f1eb8c (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System.Data;
using System.Text.Json;
using Tango.Portal.Chat.Web.Models;
using Tango.Portal.Chat.Web.Services;
using Kusto.Data.Data;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
using System.Collections;
using Tango.Portal.Chat.Web.Utils;

namespace Tango.Portal.Chat.Web.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public sealed class ChatController : ControllerBase
    {
        private readonly SchemaRegistry _schema;
        private readonly KqlGuard _guard;
        private readonly KustoQueryService _adx;
        private readonly LlmClient _llm;
        private readonly ChatMessageLogger _logger;

        public ChatController(SchemaRegistry schema, KqlGuard guard, KustoQueryService adx, LlmClient llm, ChatMessageLogger logger)
        {
            _schema = schema;
            _guard = guard;
            _adx = adx;
            _llm = llm;
            _logger = logger;
        }

        [HttpPost("ask")]
        public async Task<ActionResult<ChatResponse>> Ask([FromBody] ChatRequest req, CancellationToken ct, LlmProvider? provider = null)
        {
            try
            {
                if (!SessionUtils.IsUserAuthenticated(HttpContext))
                {
                    return new ChatResponse
                    {
                        Answer = "User is not authenticated or session expired",
                        ThreadId = req.ThreadId
                    };
                }

                var sessionUser = SessionUtils.GetSessionUser(HttpContext);
                var sessionId = HttpContext.Session.Id;

                // Log the question
                _ = Task.Run(async () =>
                {
                    try
                    {
                        await _logger.LogQuestionAsync(
                            sessionId,
                            sessionUser?.Email ?? "unknown",
                            sessionUser?.FullName ?? "unknown",
                            req.Question,
                            ct);
                    }
                    catch
                    {
                        // Ignore logging failures
                    }
                }, ct);

                var schemaJson = _schema.GetSchemaJson();
                var plannerPrompt = _schema.GetPlannerPrompt();
                var plotySample = _schema.GetPlotySample();

                // 1) Ask the model for KQL

                ProposeKqlResult? plan = null;

                if (provider == LlmProvider.Claude)
                {
                    plan = await _llm.ProposeKqlWithClaudeAsync(plannerPrompt, plotySample, req.Question, schemaJson, req.History, ct);
                }
                else
                {
                    plan = await _llm.ProposeKqlAsync(plannerPrompt, plotySample, req.Question, schemaJson, req.History, ct);
                }

                ChatResponse response;
                if (plan.Assistant == "data" || plan.Assistant == "ploty")
                {
                    response = await AnswerWithDataAssistant(req, plan, ct);
                }
                else if (plan.Assistant == "docs")
                {
                    response = await AnswerWithDocsAssistant(req, plan, ct);
                }
                else
                {
                    response = AnswerWithPlannerConversation(req, plan);
                }

                // Log the answer
                _ = Task.Run(async () =>
                {
                    try
                    {
                        await _logger.LogAnswerAsync(
                            sessionId,
                            sessionUser?.Email ?? "unknown",
                            sessionUser?.FullName ?? "unknown",
                            response,
                            plan.Assistant,
                            plan.Provider.ToString(),
                            ct);
                    }
                    catch
                    {
                        // Ignore logging failures
                    }
                }, ct);

                return response;
            }
            catch (Exception ex)
            {
                var errorResponse = new ChatResponse
                {
                    Answer = $"Ooops something went wrong...\n{ex.Message}",
                    ThreadId = req.ThreadId
                };

                // Log the error response
                var sessionUser = SessionUtils.GetSessionUser(HttpContext);
                var sessionId = req.ThreadId ?? Guid.NewGuid().ToString();
                _ = Task.Run(async () =>
                {
                    try
                    {
                        await _logger.LogAnswerAsync(
                            sessionId,
                            sessionUser?.Email ?? "unknown",
                            sessionUser?.FullName ?? "unknown",
                            errorResponse,
                            "error",
                            "Unknown",
                            ct);
                    }
                    catch
                    {
                        // Ignore logging failures
                    }
                }, ct);

                return errorResponse;
            }
        }

        private ChatResponse AnswerWithPlannerConversation(ChatRequest req, ProposeKqlResult plan)
        {
            return new ChatResponse
            {
                Answer = plan.ConversationAnswer,
                ThreadId = req.ThreadId
            };
        }

        private async Task<ChatResponse> AnswerWithDocsAssistant(ChatRequest req, ProposeKqlResult plan, CancellationToken ct)
        {
            // AFTER
            var run = await _llm.AnswerWithAssistantAsync(
                LlmClient.AssistantType.Docs,
                req.Question,
                string.Empty,
                plan.Kql,
                req.ThreadId,   // <-- reuse if provided
                ct);

            return new ChatResponse
            {
                Answer = run.Answer,
                ThreadId = run.ThreadId
            };
        }

        private async Task<ChatResponse> AnswerWithDataAssistant(ChatRequest req, ProposeKqlResult plan, CancellationToken ct)
        {
            // 2) Guardrail validation
            var val = _guard.Validate(plan.Kql);
            if (!val.IsOk)
            {
                // Return error to the client so they can iterate
                return new ChatResponse
                {
                    Answer = $"The generated kusto query contains invalid tokens..\n{val.Error}",
                    ThreadId = req.ThreadId,
                    UsedKql = plan.Kql
                };
            }

            // 4) Execute in ADX
            DataTable table;
            try
            {
                table = await _adx.QueryAsync(plan.Kql, plan.Parameters, ct);
            }
            catch (Exception ex)
            {
                if (plan.Provider == LlmProvider.OpenAI)
                {
                    var fallbackResult = await Ask(req, ct, LlmProvider.Claude);
                    return fallbackResult.Value ?? new ChatResponse
                    {
                        Answer = "Fallback to Claude failed",
                        ThreadId = req.ThreadId
                    };
                }
                else
                {
                    // Return error to the client so they can iterate
                    return new ChatResponse
                    {
                        Answer = $"Seems like my kusto query ran into some issue..\n{ex.Message}",
                        ThreadId = req.ThreadId,
                        UsedKql = plan.Kql
                    };
                }
            }

            if (table.Columns.Contains("ploty") && table.Rows.Count > 0)
            {
                return AnswerWithPloty(req, plan, table);
            }

            // 5) Build compact facts (limit rows/cols)

            if (table.Rows.Count <= 200)
            {
                return await AnswerWithDataAssistantInternal(req, plan, table, ct);
            }
            else
            {
                return AnswerWithMarkdownTable(req, plan, table);
            }
        }

        private async Task<ChatResponse> AnswerWithDataAssistantInternal(ChatRequest req, ProposeKqlResult plan, DataTable table, CancellationToken ct)
        {
            var preview = DataHelper.ToPreview(table, 200);
            var facts = JsonSerializer.Serialize(preview);

            // 6) Ask model for final answer
            //var answer = await _llm.AnswerFromFactsAsync(req.Question, facts, plan.Kql, ct);

            var run = await _llm.AnswerWithAssistantAsync(
                LlmClient.AssistantType.Data,
                req.Question,
                facts,
                plan.Kql,
                req.ThreadId,   // <-- reuse if provided
                ct);

            return new ChatResponse
            {
                Answer = run.Answer,
                UsedKql = plan.Kql,
                Preview = preview,
                ThreadId = run.ThreadId   // <-- echo back the thread id used/created
            };
        }

        private ChatResponse AnswerWithMarkdownTable(ChatRequest req, ProposeKqlResult plan, DataTable table)
        {
            var markdown = DataHelper.ToMarkdownTable(table);

            return new ChatResponse
            {
                Answer = $"The result set was too big for me to analyze so I'm just going to drop the entire result set.\n{markdown}",
                UsedKql = plan.Kql,
                Preview = markdown,
                ThreadId = req.ThreadId   // <-- echo back the thread id used/created
            };
        }

        private ChatResponse AnswerWithPloty(ChatRequest req, ProposeKqlResult plan, DataTable table)
        {
            String? ploty = table.Rows[0]["ploty"]?.ToString();

            return new ChatResponse
            {
                Answer = plan.ConversationAnswer,
                ThreadId = req.ThreadId,
                UsedKql = plan.Kql,
                Ploty = ploty ?? String.Empty
            };
        }
    }
}