From 601bb80adec1ef0aad93e63cd0870786ca931855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Liberoff=20V=C3=A1zquez?= Date: Tue, 25 Jun 2024 14:19:38 +0200 Subject: [PATCH] The following dependencies (packages) were updated: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `Azure.AI.Vision.ImageAnalysis` from version `1.0.0-beta.2` to `1.0.0-beta.3`. - `Azure.Identity` from version `1.11.4` to `1.12.0`. - `Azure.Storage.Blobs` from version `12.18.0` to `12.20.0`. - `Microsoft.CognitiveServices.Speech` from version `1.36.0` to `1.38.0`. - `Azure.AI.OpenAI` from version `1.0.0-beta.13` to `2.0.0-beta.2`. ► This versions introduces a lot of breaking changes that affected the following use cases: - Brand Analyzer - Call Center Analytics - Document Comparison - Form Analyzer Finally, some minor boy scouting tweaks and improvements. --- src/AIHub/AIHub.csproj | 14 ++-- .../AudioTranscriptionController.cs | 16 +--- .../Controllers/BrandAnalyzerController.cs | 65 +++++++--------- src/AIHub/Controllers/CallCenterController.cs | 63 +++++++-------- .../DocumentComparisonController.cs | 77 +++++++------------ .../Controllers/FormAnalyzerController.cs | 76 ++++++++---------- 6 files changed, 125 insertions(+), 186 deletions(-) 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