diff --git a/.gitignore b/.gitignore index 482385d..8a38a17 100644 --- a/.gitignore +++ b/.gitignore @@ -420,3 +420,11 @@ local.settings.json # Ignore hidden folder for AI Hub Installation **/.aihub + +# appsettings.json +appsettings.*.json +!appsettings.json +!appsettings.Development.json +!appsettings.Production.json +!appsettings.Staging.json +!appsettings.Test.json diff --git a/src/AIHub/AIHub.csproj b/src/AIHub/AIHub.csproj index eab49e2..c74ba51 100644 --- a/src/AIHub/AIHub.csproj +++ b/src/AIHub/AIHub.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -9,11 +9,11 @@ - - - - - + + + + + @@ -24,7 +24,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/AIHub/Controllers/AudioTranscriptionController.cs b/src/AIHub/Controllers/AudioTranscriptionController.cs index 6b9cd83..5d0f88a 100644 --- a/src/AIHub/Controllers/AudioTranscriptionController.cs +++ b/src/AIHub/Controllers/AudioTranscriptionController.cs @@ -158,22 +158,14 @@ public IActionResult Error() return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } - private bool CheckNullValues(IFormFile? imageFile) + private static bool CheckNullValues(IFormFile? imageFile) { - if (imageFile == null) - { - return true; - } - return false; + return imageFile == null; } - private bool CheckImageExtension(string blobUri) + private static bool CheckImageExtension(string blobUri) { string uri_lower = blobUri; - if (uri_lower.Contains(".mp3", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - return true; + return !uri_lower.Contains(@".mp3", StringComparison.OrdinalIgnoreCase); } } \ No newline at end of file diff --git a/src/AIHub/Controllers/BrandAnalyzerController.cs b/src/AIHub/Controllers/BrandAnalyzerController.cs index 5d9b9f3..785080d 100644 --- a/src/AIHub/Controllers/BrandAnalyzerController.cs +++ b/src/AIHub/Controllers/BrandAnalyzerController.cs @@ -1,3 +1,5 @@ +using OpenAI.Chat; + namespace MVCWeb.Controllers; public class BrandAnalyzerController : Controller @@ -76,42 +78,33 @@ public async Task AnalyzeCompany() try { + Uri aoaiEndpointUri = new(AOAIendpoint); + + AzureOpenAIClient azureClient = string.IsNullOrEmpty(AOAIsubscriptionKey) + ? new(aoaiEndpointUri, new DefaultAzureCredential()) + : new(aoaiEndpointUri, new AzureKeyCredential(AOAIsubscriptionKey)); + + ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName); - OpenAIClient aoaiClient; - if (string.IsNullOrEmpty(AOAIsubscriptionKey)) + var messages = new ChatMessage[] { - aoaiClient = new OpenAIClient( - new Uri(AOAIendpoint), - new DefaultAzureCredential()); - } - else + new SystemChatMessage($"I will provide a list results from opinions on the Internet about {model.CompanyName} Bing search. If {model.CompanyName} is not a company what the user is asking for, answer to provide a new Company name. The user will ask you what they want to get from the company opinions. \n Results: {input_context}"), + new UserChatMessage(model.Prompt), + }; + + ChatCompletionOptions chatCompletionOptions = new() { - aoaiClient = new OpenAIClient( - new Uri(AOAIendpoint), - new AzureKeyCredential(AOAIsubscriptionKey)); - } + + Temperature = 0.7f, + MaxTokens = 1000, + TopP = 0.95f, + FrequencyPenalty = 0, + PresencePenalty = 0, + }; + + ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions); - // If streaming is not selected - Response responseWithoutStream = await aoaiClient.GetChatCompletionsAsync( - new ChatCompletionsOptions() - { - DeploymentName = AOAIDeploymentName, - Messages = - { - new ChatRequestSystemMessage(@"I will provide a list results from opinons on the internet about "+model.CompanyName+" Bing search. If "+model.CompanyName+" is not a company what the user is asking for, answer to provide a new Company name. The user will ask you what they want to get from the compay opinions. \n Results: "+ input_context), - new ChatRequestUserMessage(model.Prompt), - }, - Temperature = (float)0.7, - MaxTokens = 1000, - NucleusSamplingFactor = (float)0.95, - FrequencyPenalty = 0, - PresencePenalty = 0, - }); - - ChatCompletions completions = responseWithoutStream.Value; - ChatChoice results_analisis = completions.Choices[0]; - model.Message = results_analisis.Message.Content; - ViewBag.Message = results_analisis.Message.Content; + ViewBag.Message = completion.Content[0].Text; } catch (RequestFailedException) { @@ -127,12 +120,8 @@ public IActionResult Error() return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } - private bool CheckNullValues(string? companyName) + private static bool CheckNullValues(string? companyName) { - if (string.IsNullOrEmpty(companyName)) - { - return true; - } - return false; + return string.IsNullOrEmpty(companyName); } } \ No newline at end of file diff --git a/src/AIHub/Controllers/CallCenterController.cs b/src/AIHub/Controllers/CallCenterController.cs index 22a024a..7403e67 100644 --- a/src/AIHub/Controllers/CallCenterController.cs +++ b/src/AIHub/Controllers/CallCenterController.cs @@ -1,3 +1,5 @@ +using OpenAI.Chat; + namespace MVCWeb.Controllers; public class CallCenterController : Controller @@ -34,43 +36,32 @@ public async Task AnalyzeCall() } try { - OpenAIClient aoaiClient; - if (string.IsNullOrEmpty(subscriptionKey)) + Uri aoaiEndpointUri = new(endpoint); + + AzureOpenAIClient azureClient = string.IsNullOrEmpty(subscriptionKey) + ? new(aoaiEndpointUri, new DefaultAzureCredential()) + : new(aoaiEndpointUri, new AzureKeyCredential(subscriptionKey)); + + ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName); + + var messages = new ChatMessage[] { - aoaiClient = new OpenAIClient( - new Uri(endpoint), - new DefaultAzureCredential()); - } - else + new SystemChatMessage(model.Prompt), + new UserChatMessage(@"Call transcript: "+model.Transcript), + }; + + ChatCompletionOptions chatCompletionOptions = new() { - aoaiClient = new OpenAIClient( - new Uri(endpoint), - new AzureKeyCredential(subscriptionKey)); - } - - // If streaming is not selected - Response responseWithoutStream = await aoaiClient.GetChatCompletionsAsync( - new ChatCompletionsOptions() - { - DeploymentName = AOAIDeploymentName, - Messages = - { - new ChatRequestSystemMessage(model.Prompt), - new ChatRequestUserMessage(@"Call transcript: "+model.Transcript), - }, - Temperature = (float)0.1, - MaxTokens = 1000, - NucleusSamplingFactor = (float)0.95, - FrequencyPenalty = 0, - PresencePenalty = 0, - }); - - ChatCompletions completions = responseWithoutStream.Value; - ChatChoice results_analisis = completions.Choices[0]; - System.Console.WriteLine(results_analisis); - ViewBag.Message = - results_analisis.Message.Content - ; + MaxTokens = 1000, + Temperature = 0.1f, + FrequencyPenalty = 0, + PresencePenalty = 0, + TopP = 0.95f, + }; + + ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions); + + ViewBag.Message = completion.Content[0].Text; } catch (RequestFailedException) { @@ -86,7 +77,7 @@ public IActionResult Error() return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } - private bool CheckNullValues(string? companyName, string? prompt) + private static bool CheckNullValues(string? companyName, string? prompt) { if (string.IsNullOrEmpty(companyName)) { diff --git a/src/AIHub/Controllers/DocumentComparisonController.cs b/src/AIHub/Controllers/DocumentComparisonController.cs index 3183c69..5228307 100644 --- a/src/AIHub/Controllers/DocumentComparisonController.cs +++ b/src/AIHub/Controllers/DocumentComparisonController.cs @@ -1,3 +1,5 @@ +using OpenAI.Chat; + namespace MVCWeb.Controllers; public class DocumentComparisonController : Controller @@ -60,41 +62,33 @@ public async Task DocumentComparison(string[] document_urls, stri try { - OpenAIClient aoaiClient; - if (string.IsNullOrEmpty(AOAIsubscriptionKey)) + Uri aoaiEndpointUri = new(AOAIendpoint); + + AzureOpenAIClient azureClient = string.IsNullOrEmpty(AOAIsubscriptionKey) + ? new(aoaiEndpointUri, new DefaultAzureCredential()) + : new(aoaiEndpointUri, new AzureKeyCredential(AOAIsubscriptionKey)); + + ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName); + + var messages = new ChatMessage[] { - aoaiClient = new OpenAIClient( - new Uri(AOAIendpoint), - new DefaultAzureCredential()); - } - else + new SystemChatMessage($@"You are specialized in analyze different versions of the same PDF document. The first Document OCR result is: <<<{output_result[0]}>>> and the second Document OCR result is: <<<{output_result[1]}>>>"), + new UserChatMessage($@"User question: {prompt}"), + }; + + ChatCompletionOptions chatCompletionOptions = new() { - aoaiClient = new OpenAIClient( - new Uri(AOAIendpoint), - new AzureKeyCredential(AOAIsubscriptionKey)); - } + MaxTokens = 1000, + Temperature = 0.7f, + FrequencyPenalty = 0, + PresencePenalty = 0, + TopP = 0.95f + }; - // ### If streaming is not selected - Response responseWithoutStream = await aoaiClient.GetChatCompletionsAsync( - new ChatCompletionsOptions() - { - DeploymentName = AOAIDeploymentName, - Messages = - { - new ChatRequestSystemMessage(@"You are specialized in analyze different versions of the same PDF document. The first Document OCR result is: <<<"+output_result[0]+">>> and the second Document OCR result is: <<<"+output_result[1]+">>>"), - new ChatRequestUserMessage(@"User question: "+prompt ), - }, - Temperature = (float)0.7, - MaxTokens = 1000, - NucleusSamplingFactor = (float)0.95, - FrequencyPenalty = 0, - PresencePenalty = 0, - }); - - ChatCompletions completions = responseWithoutStream.Value; - ChatChoice results_analisis = completions.Choices[0]; - model.Message = results_analisis.Message.Content; - ViewBag.Message = results_analisis.Message.Content; + ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions); + + model.Message = completion.Content[0].Text; + ViewBag.Message = completion.Content[0].Text; } catch (RequestFailedException) { @@ -122,7 +116,7 @@ public async Task UploadFile() { if (CheckImageExtension(documentFile.FileName.ToString())) { - ViewBag.Message = "You must upload pdf documpents only"; + ViewBag.Message = "You must upload pdf documents only"; return View("DocumentComparison", model); } } @@ -156,21 +150,8 @@ public IActionResult Error() return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } - private bool CheckNullValues(IFormFile filename) - { - if (filename == null) - { - return true; - } - return false; - } - - private bool CheckImageExtension(string filename) + private static bool CheckImageExtension(string filename) { - if (filename.Contains(".pdf", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - return true; + return !filename.Contains(@".pdf", StringComparison.OrdinalIgnoreCase); } } \ No newline at end of file diff --git a/src/AIHub/Controllers/FormAnalyzerController.cs b/src/AIHub/Controllers/FormAnalyzerController.cs index 06587c9..6940c01 100644 --- a/src/AIHub/Controllers/FormAnalyzerController.cs +++ b/src/AIHub/Controllers/FormAnalyzerController.cs @@ -1,3 +1,5 @@ +using OpenAI.Chat; + namespace MVCWeb.Controllers; public class FormAnalyzerController : Controller @@ -47,42 +49,34 @@ public async Task AnalyzeForm(string image_url, string prompt) var operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", new Uri(image)); var result = operation.Value.Content; - OpenAIClient aoaiClient; - if (string.IsNullOrEmpty(AOAIsubscriptionKey)) - { - aoaiClient = new OpenAIClient( - new Uri(AOAIendpoint), - new DefaultAzureCredential()); - } - else + Uri aoaiEndpointUri = new(AOAIendpoint); + + AzureOpenAIClient azureClient = string.IsNullOrEmpty(AOAIsubscriptionKey) + ? new(aoaiEndpointUri, new DefaultAzureCredential()) + : new(aoaiEndpointUri, new AzureKeyCredential(AOAIsubscriptionKey)); + + ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName); + + var messages = new ChatMessage[] { - aoaiClient = new OpenAIClient( - new Uri(AOAIendpoint), - new AzureKeyCredential(AOAIsubscriptionKey)); - } + new SystemChatMessage($@"You are specialized in understanding PDFs and answering questions about it. Document OCR result is: {result}"), + new UserChatMessage($@"User question: {prompt}"), + }; - // If streaming is not selected - Response responseWithoutStream = await aoaiClient.GetChatCompletionsAsync( - new ChatCompletionsOptions() - { - DeploymentName = AOAIDeploymentName, - Messages = - { - new ChatRequestSystemMessage($"You are specialized in understanding PDFs and answering questions about it. Document OCR result is: {result}"), - new ChatRequestUserMessage($"User question: {prompt}"), - }, - Temperature = (float)0.7, - MaxTokens = 1000, - NucleusSamplingFactor = (float)0.95, - FrequencyPenalty = 0, - PresencePenalty = 0, - }); - - ChatCompletions completions = responseWithoutStream.Value; - ChatChoice results_analisis = completions.Choices[0]; - model.Message = results_analisis.Message.Content; - ViewBag.Message = results_analisis.Message.Content; + ChatCompletionOptions chatCompletionOptions = new() + { + MaxTokens = 1000, + Temperature = 0.7f, + FrequencyPenalty = 0, + PresencePenalty = 0, + TopP = 0.95f + }; + ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions); + + model.Message = completion.Content[0].Text; + ViewBag.Message = completion.Content[0].Text; + model.Image = image; return Ok(model); @@ -133,22 +127,14 @@ public IActionResult Error() return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } - private bool CheckNullValues(IFormFile imageFile) + private static bool CheckNullValues(IFormFile imageFile) { - if (imageFile == null) - { - return true; - } - return false; + return imageFile == null; } - private bool CheckImageExtension(string blobUri) + private static bool CheckImageExtension(string blobUri) { string uri_lower = blobUri; - if (uri_lower.Contains(".pdf", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - return true; + return !uri_lower.Contains(@".pdf", StringComparison.OrdinalIgnoreCase); } } \ No newline at end of file diff --git a/src/AIHub/Program.cs b/src/AIHub/Program.cs index faecbe3..af5111a 100644 --- a/src/AIHub/Program.cs +++ b/src/AIHub/Program.cs @@ -1,8 +1,17 @@ -using Azure; -using Azure.AI.ContentSafety; - var builder = WebApplication.CreateBuilder(args); +builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()); + +/* Load Configuration */ +if (Debugger.IsAttached) +{ + builder.Configuration.AddJsonFile(@"appsettings.debug.json", optional: true, reloadOnChange: true); +} + +builder.Configuration.AddJsonFile($@"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddJsonFile($@"appsettings.{Environment.UserName}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + // Add services to the container. builder.Services.AddControllersWithViews(); @@ -29,4 +38,4 @@ name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); -app.Run(); +await app.RunAsync(); diff --git a/src/AIHub/appsettings.template.json b/src/AIHub/appsettings.template.json index 8d6c353..d779543 100644 --- a/src/AIHub/appsettings.template.json +++ b/src/AIHub/appsettings.template.json @@ -23,7 +23,7 @@ "DeploymentName": "gpt-35-turbo" }, "AudioTranscription": { - "SpeechLocation": "westeurope", + "SpeechLocation": "", "SpeechSubscriptionKey": "", "ContainerName": "audio-files" }, @@ -45,7 +45,7 @@ }, "FormAnalyzer": { "FormRecogEndpoint": "
", - "FormRecogSubscriptionKey": "Form Recog Key", + "FormRecogSubscriptionKey": "", "OpenAIEndpoint": "", "OpenAISubscriptionKey": "", "ContainerName": "form-analyzer", @@ -62,9 +62,6 @@ "ChatOnYourData": { "Link": "" }, - "PBIReport": { - "Link": "" - }, "Storage": { "ConnectionString": "", "ContainerName": "image-moderator"