From c3c7e5349a447e8940ecbbeae6641241bc822c37 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Sat, 14 Oct 2023 16:31:08 +0100 Subject: [PATCH] feat(#5): Add ask controller and WhoCanHelpWithX endpoint --- WhoAtX.sln | 25 +++++++++++++ WhoAtX/Controllers/AskController.cs | 58 +++++++++++++++++++++++++++++ WhoAtX/WhoAtX.csproj | 3 +- WhoAtX/appsettings.json | 5 +++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 WhoAtX.sln create mode 100644 WhoAtX/Controllers/AskController.cs diff --git a/WhoAtX.sln b/WhoAtX.sln new file mode 100644 index 0000000..1c31557 --- /dev/null +++ b/WhoAtX.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WhoAtX", "WhoAtX\WhoAtX.csproj", "{45C8EE64-AE4C-4B6A-ABD5-2D0C1517A42D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {45C8EE64-AE4C-4B6A-ABD5-2D0C1517A42D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {45C8EE64-AE4C-4B6A-ABD5-2D0C1517A42D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {45C8EE64-AE4C-4B6A-ABD5-2D0C1517A42D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {45C8EE64-AE4C-4B6A-ABD5-2D0C1517A42D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BAE8E8E9-EE55-4544-954C-638B3A2E62C3} + EndGlobalSection +EndGlobal diff --git a/WhoAtX/Controllers/AskController.cs b/WhoAtX/Controllers/AskController.cs new file mode 100644 index 0000000..6511cc1 --- /dev/null +++ b/WhoAtX/Controllers/AskController.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.SemanticKernel; + +namespace WhoAtX.Controllers; + +public class AskController : Controller +{ + private readonly IConfiguration _config; + private readonly IKernel _kernel; + private readonly ILogger _logger; + + public AskController(ILogger logger, IConfiguration config) + { + _logger = logger; + _config = config; + + // N.B. Swap this if not using Using Azure + _kernel = new KernelBuilder() + .WithAzureChatCompletionService( + _config["OpenAi:DeploymentName"] ?? throw new Exception("DeploymentName is null"), + _config["OpenAi:Endpoint"] ?? throw new Exception("Endpoint is null"), + _config["OpenAi:ApiKey"] ?? throw new Exception("ApiKey is null") + ) + // .WithOpenAIChatCompletionService() + .Build(); + } + + public async Task WhoCanHelpWithX(string queryText) + { + // Define the semantic function + const string promptTemplate = """ + Who at my organisation can help me with my query? + + ===== QUERY ===== + {{$input}} + ===== END QUERY ===== + + Please ensure that you respond in a professional and courteous manner, making full use of the information about relevant employees. This includes: + - Name + - Team + - Link to their user profile (using the Id) + - Areas of knowledge + - Projects + + If the query is not about finding someone within my organisation to then please only respond to tell me that you can only help with queries about finding someone within my organisation. + + Engage. + """; + var whoCanHelpWithXFunction = _kernel.CreateSemanticFunction(promptTemplate); + + // Perform the ask + var result = (await whoCanHelpWithXFunction.InvokeAsync(queryText, _kernel)) + .GetValue(); + + // TODO(#15) - add the NL2SQL plugin to retrieve data from the SQL database, enabling this query to access the relevant data. It must be run using a read-only user to ensure that it cannot change data. + return Json(result); + } +} \ No newline at end of file diff --git a/WhoAtX/WhoAtX.csproj b/WhoAtX/WhoAtX.csproj index 43cfd22..09def4d 100644 --- a/WhoAtX/WhoAtX.csproj +++ b/WhoAtX/WhoAtX.csproj @@ -13,7 +13,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + + diff --git a/WhoAtX/appsettings.json b/WhoAtX/appsettings.json index 55d53c8..cfee71c 100644 --- a/WhoAtX/appsettings.json +++ b/WhoAtX/appsettings.json @@ -8,5 +8,10 @@ "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Data Source=UserDatabase.sqlite" + }, + "OpenAi": { + "ApiKey": "", + "DeploymentName": "", + "Endpoint": "" } }